Quero que a tarefa termine se algum ponto de montagem não atender à afirmação

Quero que a tarefa termine se algum ponto de montagem não atender à afirmação

Preciso de ajuda com este manual. Eu uso o módulo assert para afirmar que o espaço em disco é inferior a 30% e envio uma notificação de folga com a mensagem de falha. O módulo Assert percorre todos os FS no servidor (cerca de 10 FS). a mensagem enviada no slack não foi a esperada. O que estou tentando conseguir é apenas exibir apenas o item com falha no loop. Exibir apenas a mensagem do item com falha na asserção.

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')
       

O resultado final é assim:

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']

Mas preciso que a msg seja apenas esta:

HOST XYZ: Filesystem: /usr has less than 30% space. Consider increasing the FS size'

Responder1

P:"Quero que a tarefa seja encerrada se algum ponto de montagem não atender à afirmação."

R: Simplifique a condição. Por exemplo

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

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

Silencie o retorno de chamada se não quiser exibir o resultado. Por exemplo

shell> ANSIBLE_DISPLAY_OK_HOSTS=false ansible-playbook playbook.yml

Para obter detalhes sobre o retorno de chamada, consulte

shell> ansible-doc -t callback default

A jogada falhará se algum dos itens não atender à condição. Por exemplo

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

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

P:"Exibir apenas a mensagem do item com falha na asserção."

R: Adicione uma tarefa de depuração se desejar exibir pontos de montagem com falha. Por exemplo

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

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

informação relacionada