如果任何安裝點無法滿足斷言,我希望任務終止

如果任何安裝點無法滿足斷言,我希望任務終止

我需要一些有關此劇本的幫助。我使用斷言模組斷言磁碟空間小於 30%,並發送帶有失敗訊息的鬆弛通知。 Assert模組循環遍歷伺服器中的所有FS(約10個FS)。 slack 上發送的訊息不符合預期。我想要實現的只是僅顯示循環中失敗的項目。僅顯示斷言失敗項的訊息。

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

最終結果如下圖所示:

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

但我需要的消息只是這樣:

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

答案1

問:如果任何安裝點無法滿足斷言,我希望任務終止。

答:簡化條件。例如

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

如果您不想顯示結果,請將回呼靜音。例如

shell> ANSIBLE_DISPLAY_OK_HOSTS=false ansible-playbook playbook.yml

有關回調的詳細信息,請參見

shell> ansible-doc -t callback default

如果其中任何一項不符合條件,遊戲就會失敗。例如

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

問:僅顯示斷言失敗項的訊息。

A:如果要顯示失敗的掛載點,請新增偵錯任務。例如

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

相關內容