Ansible-Playbook

當ansible playbook中存在分區時如何編寫when條件

  • August 17, 2020

當 /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 不存在,請建議如何跳過

這是我為使其工作所做的工作:

---
- 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可用的伺服器 - 但它按預期工作。

一些參考資料:

引用自:https://serverfault.com/questions/1030009