Ich möchte, dass die Aufgabe beendet wird, wenn ein Einhängepunkt die Behauptung nicht erfüllt

Ich möchte, dass die Aufgabe beendet wird, wenn ein Einhängepunkt die Behauptung nicht erfüllt

Ich brauche etwas Hilfe mit diesem Playbook. Ich verwende das Assert-Modul, um zu bestätigen, dass der Speicherplatz weniger als 30 % beträgt, und sende eine Slack-Benachrichtigung mit der Fehlermeldung. Das Assert-Modul durchläuft alle FS im Server (ca. 10 FS). Die über Slack gesendete Nachricht war nicht wie erwartet. Was ich erreichen möchte, ist, nur das fehlgeschlagene Element in der Schleife anzuzeigen. Nur die Meldung des fehlgeschlagenen Assert-Elements anzeigen.

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

Das Endergebnis sieht so aus:

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

Ich brauche aber nur diese Nachricht:

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

Antwort1

Q:"Ich möchte, dass die Aufgabe beendet wird, wenn ein Einhängepunkt die Bedingung nicht erfüllt."

A: Vereinfachen Sie die Bedingung. Zum Beispiel

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

gibt

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

Schalten Sie den Rückruf stumm, wenn Sie das Ergebnis nicht anzeigen möchten. Beispiel:

shell> ANSIBLE_DISPLAY_OK_HOSTS=false ansible-playbook playbook.yml

Einzelheiten zum Rückruf finden Sie unter

shell> ansible-doc -t callback default

Das Spiel schlägt fehl, wenn eines der Elemente die Bedingung nicht erfüllt. Zum Beispiel

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

gibt

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

Q:"Zeigt nur die Meldung „Assertion fehlgeschlagen“ an."

A: Fügen Sie eine Debug-Aufgabe hinzu, wenn Sie fehlgeschlagene Einhängepunkte anzeigen möchten. Zum Beispiel

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

gibt

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

verwandte Informationen