我尝试在 /var 分区存在时写入任务。我使用了断言变量
- name: Apply patch if /var exist and > 300MB
yum:
name: '*'
state: latest
loop: "{{ ansible_mounts }}"
when: item.mount == "/var" and item.size_available > 300000000
如果 /var 不存在,则应该失败或跳过。但事实并非如此。请建议如果 /var 不存在,如何跳过
答案1
以下是我为使其发挥作用所做的事情:
---
- name: answer serverfault
hosts: all
become: yes
tasks:
- name: Apply patch if /var exist and > 300MB
debug:
msg:
#- " Data type of 'ansible_facts['mounts'] is {{ ansible_facts['mounts'] | type_debug }} "
- item mount is {{ item.mount }}
- item size_available is {{ item.size_available }}
when: item.mount == "/data" and item.size_available > 300000000
loop: "{{ ansible_facts['mounts'] | flatten(levels=1) }}"
注释部分实际上显示了你正在处理的数据类型。在本例中它是一个列表。
为了展平我使用的列表,"{{ ansible_facts['mounts'] | flatten(levels=1) }}"
因为列表最初看起来像这样:
"ansible_mounts": [
{
"block_available": 109019876,
"block_size": 4096,
"block_total": 961159932,
"block_used": 852140056,
"device": "/dev/sde1",
"fstype": "ext4",
"inode_available": 243543185,
"inode_total": 164195328,
"inode_used": 513143,
"mount": "/data",
"options": "rw,relatime",
"size_available": 444545412096,
"size_total": 3236911081442,
"uuid": "a35a0282-6eac-132d-abdf-fbea3a835522"
},
/data*
由于我现在没有可用的服务器,我已经在我的挂载点上测试了剧本/var
- 但它按预期工作。
一些参考: