我必须检查系统上是否有可用的安装点列表。
因此,我定义了一个包含挂载点列表的变量,然后从 Ansible 事实中提取可用的挂载点。
---
- hosts: all
vars:
required_mounts:
- /prom/data
- /prom/logs
tasks:
- name: debug mountpoint
set_fact:
mount_points: "{{ ansible_mounts|json_query('[].mount') }}"
- name: check fs
fail:
msg: 'mount point not found'
when: required_mounts not in mount_points
我被困在这里,我不知道如何比较变量required_mounts
与现有的安装点。
如果有任何项目在required_mounts
不在现有安装点中,任务应该失败。
任务check fs
即使安装点存在,也总是失败。
我必须一一循环吗?并逐项比较?如果是这样,我怎样才能实现这一目标?
您可以使用集合论为此,因为您正在寻找的只是之间的区别required_mounts
和ansible_mounts
.
另外,这里不需要 JMESPath 查询,这个简单的要求可以通过简单的实现map.
因此,这可以通过单独的任务来实现:
- fail:
msg: "Missing mounts: `{{ missing_mounts | join(', ') }}`"
when: missing_mounts | length > 0
vars:
missing_mounts: >-
{{
required_mounts
| difference(
ansible_mounts | map(attribute='mount')
)
}}
鉴于剧本:
- hosts: localhost
gather_facts: yes
vars:
required_mounts:
- /etc/hostname
- /etc/hosts
- /tmp/not_an_actual_mount
- /tmp/not_a_mount_either
tasks:
- fail:
msg: "Missing mounts: `{{ missing_mounts | join(', ') }}`"
when: missing_mounts | length > 0
vars:
missing_mounts: >-
{{
required_mounts
| difference(
ansible_mounts | map(attribute='mount')
)
}}
这产生:
TASK [Gathering Facts] *******************************************************
ok: [localhost]
TASK [fail] ******************************************************************
fatal: [localhost]: FAILED! => changed=false
msg: 'Missing mounts: `/tmp/not_an_actual_mount, /tmp/not_a_mount_either`'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)