1

I need some help with this playbook. I use assert module to assert that the disk space is less than 30% and send a slack notification with the fail msg. Assert module loops through all the FS in server(around 10 FS). the message sent on slack was not as expected. What I am trying to achieve is just to display only the failed item in the loop. Display only assertion failed item's msg.

tasks:
    - name: 
      assert:
        that: "{{ item.size_available > item.size_total | float * 0.30 }}"
        msg: "Filesystem: {{ item.mount }} has less than 30% space. Consider increasing the FS size"
        #success_msg: "Filesystem: {{ item.mount }} has more than 30% space. No action required"
      register: fs_space
      loop: "{{ ansible_mounts }}"
      loop_control:
        label: ""
      ignore_errors: true

    - debug:
        msg: "HOST {{ ansible_hostname }}: {{ fs_space.results | json_query('[*].msg') }}
      when: true in fs_space.results | json_query('[*].failed')
       

The end result looks like this:

HOST XYZ: [u'All assertions passed', u'All assertions passed', u'All assertions passed', u'All assertions passed', u'All assertions passed', u'Filesystem: /usr has less than 30% space. Consider increasing the FS size', u'All assertions passed', u'All assertions passed', u'All assertions passed', u'All assertions passed']

But I need the msg to be only this:

HOST XYZ: Filesystem: /usr has less than 30% space. Consider increasing the FS size'
3
  • In this scenario there are 10 FS on my server. Assert module loops through all the mounts under "ansible_mounts" and asserts if the size available is 30%. if the space is >= 30% the assert module reports true if less assert module fails with a message but i have added ignored errors to yes so the my next debug task runs and displays the message. my question is how do i make the debug msg to show only the failed message and not the success msg Jun 5, 2021 at 6:59
  • @VladimirBotka Sorry I am pretty new to Ansible. I am not able to get the crux of your question, excuse me for that. I am gonna assume "terminate" is equal to Assert task fails if the assertion is not met. So I want the task to terminate if any mount point fails to meet the assertion. Jun 5, 2021 at 7:12
  • Edit the question and put it there (I want the task to terminate if any mount point fails to meet the assertion.). Then delete the comments. Jun 5, 2021 at 7:27

1 Answer 1

0

Q: "I want the task to terminate if any mount point fails to meet the assertion."

A: Simplify the condition. For example

shell> cat playbook.yml
- hosts: localhost
  vars:
    my_mounts: [500, 600,700]
  tasks:
    - assert:
        that: mounts_all == mounts_ok
      vars:
        mounts_all: "{{ my_mounts|length }}"
        mounts_ok: "{{ my_mounts|select('gt', 400)|length }}"

gives

TASK [assert] ******************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

Mute the callback if you don't want to display the result. For example

shell> ANSIBLE_DISPLAY_OK_HOSTS=false ansible-playbook playbook.yml

For details about the callback see

shell> ansible-doc -t callback default

The play will fail if any of the items don't meet the condition. For example

    - assert:
        that: mounts_all == mounts_ok
      vars:
        mounts_all: "{{ my_mounts|length }}"
        mounts_ok: "{{ my_mounts|select('gt', 600)|length }}"

gives

TASK [assert] *******************************************************
fatal: [localhost]: FAILED! => {
    "assertion": "mounts_all == mounts_ok",
    "changed": false,
    "evaluated_to": false,
    "msg": "Assertion failed"
}

Q: "Display only assertion failed item's msg."

A: Add a debug task if you want to display failed mount points. For example

- hosts: localhost
  vars:
    my_mounts:
      - {dev: da0, size: 500}
      - {dev: da1, size: 600}
      - {dev: da2, size: 700}
  tasks:
    - debug:
        msg: >
          Filesystems: {{ mounts_fail }} failed.
          Consider increasing the FS size.
      when: mounts_fail|length > 0
      vars:
        mounts_fail: "{{ my_mounts|
                         selectattr('size', 'lt', 600)|
                         map(attribute='dev')|list }}"
    - assert:
        that: mounts_all == mounts_ok
      vars:
        mounts_all: "{{ my_mounts|length }}"
        mounts_ok: "{{ my_mounts|
                       selectattr('size', 'gt', 600)|length }}"

gives

TASK [debug] *******************************************************
ok: [localhost] => {
    "msg": "Filesystems: ['da0'] failed. Consider increasing the FS size.\n"
}

TASK [assert] ******************************************************
fatal: [localhost]: FAILED! => {
    "assertion": "mounts_all == mounts_ok",
    "changed": false,
    "evaluated_to": false,
    "msg": "Assertion failed"
}
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .