Moridn
Moridn

Reputation: 43

Failure condition for free space check in Ansible

Working on writing a playbook to update the fleet of Cisco switches we run, but I am having trouble with a specific line I am using to verify there is sufficient free space on the stack.

Cut the relevant sections of the playbook here.

---
- hosts: Cisco2960
  vars:

    - firmware_image_size: "41555"
  tasks:
    - name: Checking for available free space...
      fail:
        msg: "Insufficient free space on the stack. Please check the flash: directory."
      when: ansible_net_filesystems_info[flash:]['spacefree_kb'] > firmware_image_size

The idea was that I would check the data from the ios_facts module, (run earlier in the playbook) and compare the value pulled to the explicitly defined variable.

After reviewing the documentation, it looks like the data type is exported from ansible_net_filesystems_info is in a dictionary. Would the problem be that I am comparing two not-like data types?

Upvotes: 0

Views: 753

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68074

Q: "Am I comparing two not-like data types?"

A: Yes. Remove the quotes from firmware_image_size

firmware_image_size: 41555

and fix the bracket notation

ansible_net_filesystems_info['flash:']['spacefree_kb']

Details: The documentation is only telling us that ansible_net_filesystems_info is a dictionary. From the code we can see that the attribute spacetotal_kb is an integer. The problem is that you declare firmware_image_size as a string

firmware_image_size: "41555"

If you compare an integer (fixed bracket notation) to a string

when: ansible_net_filesystems_info['flash:']['spacefree_kb'] > firmware_image_size

you'll see the condition failed:

fatal: ... ''>'' not supported between instances of ''int'' and ''AnsibleUnicode''


Example of a complete playbook for testing

- hosts: localhost
  vars:
    ansible_net_filesystems_info:
      'flash:':
        spacefree_kb: 50000
    firmware_image_size: 41555
  tasks:
    - assert:
        that:
          - ansible_net_filesystems_info['flash:'].spacefree_kb > firmware_image_size
        fail_msg: Insufficient free space on the stack. Please check the 'flash:' directory.

gives (abridged)

  msg: All assertions passed

Upvotes: 2

Related Questions