0

I'm trying to write to task when /var partition exists. I used assert variable

- name: Apply patch if /var exist and > 300MB
  yum:
    name: '*'
    state: latest
  loop: "{{ ansible_mounts }}"
  when: item.mount == "/var" and item.size_available > 300000000

It should fail or skip if /var does not exist. Which is not happening. Please suggest how to skip if /var does not exist

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
pugazhendhi
  • 27
  • 1
  • 3

1 Answers1

2

here's what I've done to make it work:

---
- 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) }}"

the commented section actually shows you the datatype you're dealing with. In this case it's a list.

To flatten the list I've used "{{ ansible_facts['mounts'] | flatten(levels=1) }}" since the list originally looks something like this:

        "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"
            }, 

I've tested the playbook on my /data* mount points as I do not have server with /varnow available - but it works as expected.

some references:

Roman Spiak
  • 583
  • 3
  • 11