我会创建一个剧本,检查配置的文件系统是否符合 TSM 配置,我需要echo "${FS_TSM[@]}"
从中register: FS_TO_ADD
得出结论,必须设置一个条件来调试FS_TSM
[[ ! -z "${FS_TO_ADD}" ]] && { FS_TSM+=( "${FS_TO_ADD}" }
。我认为 When: statement1 than statement2
是好的,但我不知道如何设置它来实现它
- hosts: all
vars:
whitelist:
- '/bin'
- '/boot'
FS_TSM:
- '/'
tasks:
- set_fact:
mount_point: "{{ansible_facts.mounts | selectattr('fstype', 'match', '^xf+') | map(attribute='mount')}}"
- debug:
var: mount_point
loop: "{{ whitelist }}"
when: item in mount_point
register: FS_TO_ADD
剧本的最终结果是得到以下输出:
/
/boot
/home
/opt
/var
/var
/opt
/var/tmp
/var/log
/var/log/audit
答案1
不确定你要表达什么,无论如何,也许能给你一些启发:
- name: Test
hosts: all
vars:
whitelist:
- /bin
- /boot
FS_TSM:
- /
tasks:
- name: Debug
debug:
msg: "{{ ansible_facts.mounts | selectattr('fstype', 'match', '^xf+') | map(attribute='mount') | intersect(whitelist) | union(FS_TSM) | list }}"
在我的系统上产生:
ASK [Debug] ***********************************************************************************************************************************************************************************************
ok: [server] => {
"msg": [
"/boot",
"/"
]
}
答案2
@KrisVandenbergh 你的建议确实很有帮助,尤其是
intersect(whitelist) | union(FS_TSM)
效果很好。同时我修改了我的剧本,它给了我我想要的东西:
- set_fact:
mount_point: "{{ansible_facts.mounts | selectattr('fstype', 'match', '^ext+') | map(attribute='mount') | list }}"
vars:
query: "[?mount==whitelist].mount "
- debug:
var: mount_point