마운트 지점이 어설션을 충족하지 못하는 경우 작업이 종료되기를 원합니다.

마운트 지점이 어설션을 충족하지 못하는 경우 작업이 종료되기를 원합니다.

이 플레이북과 관련하여 도움이 필요합니다. 저는 Assert 모듈을 사용하여 디스크 공간이 30% 미만임을 확인하고 실패 메시지와 함께 여유 알림을 보냅니다. Assert 모듈은 서버의 모든 FS(약 10FS)를 반복합니다. 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

큐:"마운트 지점이 어설션을 충족하지 못하면 작업이 종료되기를 원합니다."

A: 조건을 단순화하세요. 예를 들어

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

관련 정보