admin管理员组文章数量:1302301
I have the following variables in my role defaults/main.yml
,
krb5_lib_defaults:
default_realm: "{{ krb5_fqdn | upper }}"
clockskew: 300
udp_preference_limit: null
krb5_app_defaults:
pam:
ticket_lifetime: 1d
renew_lifetime: 1d
forwardable: 'true'
proxiable: 'false'
minimum_uid: 1000
external: sshd
use_shmem: sshd
Then in my inventory I have the following:
krb5_client_config:
includedir: false
libdefaults:
ticket_lifetime: null
renew_lifetime: 604800
forwardable: 'true'
default_ccache_name: 'KEYRING:persistent:%{uid}'
dns_lookup_kdc: false
dns_lookup_realm: false
kdc_timeout: 3000
something: null
appdefaults:
pam:
ticket_lifetime: null
Then in my role vars/main.yml
I have the following, merging the inventory and role defaults, removing any values that are null, and making sure the inventory variables are overriding what I have in the defaults/main.yml
.
krb5_client_lib_defaults_merged: "{{ krb5_lib_defaults | combine(krb5_client_config.libdefaults | default({}, true), recursive=True)
| dict2items
| rejectattr('value', 'equalto', none)
| rejectattr('value', 'equalto', '')
| rejectattr('value', 'equalto', false)
| items2dict }}"
krb5_client_app_defaults_merged: "{{ krb5_app_defaults | combine(krb5_client_config.appdefaults | default({}, true), recursive=True)
| dict2items
| rejectattr('value', 'equalto', none)
| rejectattr('value', 'equalto', '')
| rejectattr('value', 'equalto', false)
| items2dict }}"
However, when I debug the variables, the output of the "merged" variables I see the "libdefaults" the null parameters and values are removed from the variable, however that is not the case for the "appdefaults" variable.
Ansible debug tasks:
- name: debug - libdefaults
debug:
msg: "{{ krb5_client_lib_defaults_merged }}"
- name: debug - appdefaults
debug:
msg: "{{ krb5_client_app_defaults_merged }}"
ansible output:
PLAY [all] **************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************************************************************************
ok: [dtest-eng-02]
TASK [krb : debug - libdefaults] ****************************************************************************************************************************************************************************************************
ok: [dtest-eng-02] =>
msg:
clockskew: 300
default_ccache_name: KEYRING:persistent:%{uid}
default_realm: ACUITY.COM
forwardable: 'true'
kdc_timeout: 3000
renew_lifetime: 604800
udp_preference_limit: 1
TASK [krb : debug - appdefaults] ****************************************************************************************************************************************************************************************************
ok: [dtest-eng-02] =>
msg:
pam:
external: sshd
forwardable: 'true'
minimum_uid: 1000
proxiable: 'false'
renew_lifetime: 1d
ticket_lifetime: null
use_shmem: sshd
I'm expecting to see the the following for the krb5_client_app_defaults_merged
output:
pam:
external: sshd
forwardable: 'true'
minimum_uid: 1000
proxiable: 'false'
renew_lifetime: 1d
use_shmem: sshd
I'm assuming this is how I have my inventory variables setup, and how the appdefaults is "nested" one level deeper than the libdefaults. but I'm not sure how to correct the krb5_client_app_defaults_merged
variable to account for that. Any help on how remove values when merging varibales when nested would be great.
I have the following variables in my role defaults/main.yml
,
krb5_lib_defaults:
default_realm: "{{ krb5_fqdn | upper }}"
clockskew: 300
udp_preference_limit: null
krb5_app_defaults:
pam:
ticket_lifetime: 1d
renew_lifetime: 1d
forwardable: 'true'
proxiable: 'false'
minimum_uid: 1000
external: sshd
use_shmem: sshd
Then in my inventory I have the following:
krb5_client_config:
includedir: false
libdefaults:
ticket_lifetime: null
renew_lifetime: 604800
forwardable: 'true'
default_ccache_name: 'KEYRING:persistent:%{uid}'
dns_lookup_kdc: false
dns_lookup_realm: false
kdc_timeout: 3000
something: null
appdefaults:
pam:
ticket_lifetime: null
Then in my role vars/main.yml
I have the following, merging the inventory and role defaults, removing any values that are null, and making sure the inventory variables are overriding what I have in the defaults/main.yml
.
krb5_client_lib_defaults_merged: "{{ krb5_lib_defaults | combine(krb5_client_config.libdefaults | default({}, true), recursive=True)
| dict2items
| rejectattr('value', 'equalto', none)
| rejectattr('value', 'equalto', '')
| rejectattr('value', 'equalto', false)
| items2dict }}"
krb5_client_app_defaults_merged: "{{ krb5_app_defaults | combine(krb5_client_config.appdefaults | default({}, true), recursive=True)
| dict2items
| rejectattr('value', 'equalto', none)
| rejectattr('value', 'equalto', '')
| rejectattr('value', 'equalto', false)
| items2dict }}"
However, when I debug the variables, the output of the "merged" variables I see the "libdefaults" the null parameters and values are removed from the variable, however that is not the case for the "appdefaults" variable.
Ansible debug tasks:
- name: debug - libdefaults
debug:
msg: "{{ krb5_client_lib_defaults_merged }}"
- name: debug - appdefaults
debug:
msg: "{{ krb5_client_app_defaults_merged }}"
ansible output:
PLAY [all] **************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************************************************************************
ok: [dtest-eng-02]
TASK [krb : debug - libdefaults] ****************************************************************************************************************************************************************************************************
ok: [dtest-eng-02] =>
msg:
clockskew: 300
default_ccache_name: KEYRING:persistent:%{uid}
default_realm: ACUITY.COM
forwardable: 'true'
kdc_timeout: 3000
renew_lifetime: 604800
udp_preference_limit: 1
TASK [krb : debug - appdefaults] ****************************************************************************************************************************************************************************************************
ok: [dtest-eng-02] =>
msg:
pam:
external: sshd
forwardable: 'true'
minimum_uid: 1000
proxiable: 'false'
renew_lifetime: 1d
ticket_lifetime: null
use_shmem: sshd
I'm expecting to see the the following for the krb5_client_app_defaults_merged
output:
pam:
external: sshd
forwardable: 'true'
minimum_uid: 1000
proxiable: 'false'
renew_lifetime: 1d
use_shmem: sshd
I'm assuming this is how I have my inventory variables setup, and how the appdefaults is "nested" one level deeper than the libdefaults. but I'm not sure how to correct the krb5_client_app_defaults_merged
variable to account for that. Any help on how remove values when merging varibales when nested would be great.
- 1 See the answer. If you want to proceed edit the question, make it minimal reproducible example. – Vladimir Botka Commented Feb 11 at 8:15
1 Answer
Reset to default 2Q: 'I see the "libdefaults" the null paremters and values are removed from the variable, however that is not the case for the "appdefaults" variable.'
A: There are two problems:
The dictionaries krb5_app_defaults and krb5_client_config.appdefaults are nested by the attribute pam. The rejectattr conditions don't work.
The below condition rejects boolean false. This has no effect on strings 'false'
rejectattr('value', 'equalto', false)
本文标签: ansible combine variables and remove null valuesStack Overflow
版权声明:本文标题:ansible combine variables and remove null values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741689653a2392646.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论