0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ansible Tips: assertモジュールにて成功時にも評価式を表示させる例

Posted at

備忘

assertモジュールでは、評価結果が成功時には評価式が表示されず、Ansible logからは成功した状態であるということのエビデンスとして使用しにくい。

成功時にも評価式を表示させる例

評価式をloopとして回し、loop labelから評価式をログに残すことが可能。

tasks
- name: asserts for some tests
  assert:
    that:
    - "{{ item }}"
    success_msg: 'Passed: {{ item }}'
    fail_msg: 'FAILED: {{ item }}'
  register: r
  ignore_errors: true                   # summaryを出力させる為、failedでも継続
  loop:                                 # 評価式のlist
  - 1 == 1
  - "'a' in ['a','b']"
  - ansible_distribution in ['AIX']     # <= fail
  - ansible_distribution in ['MacOSX']  # <= success

- name: summary of asserts              # サマリー出力
  debug: var=r.results|map(attribute='msg')|list

- assert:                   # 上のassertでfailedがあった場合に停止
    that:
    - "{{ r is not failed }}"

実行結果

TASK [ansible-assert : asserts for some tests that=['{{ item }}'], success_msg=Passed: {{ item }}, fail_msg=FAILED: {{ item }}] ***
ok: [localhost] => (item=1 == 1) => {
    "ansible_loop_var": "item",
    "changed": false,
    "item": "1 == 1",
    "msg": "Passed: 1 == 1"
}
ok: [localhost] => (item='a' in ['a','b']) => {
    "ansible_loop_var": "item",
    "changed": false,
    "item": "'a' in ['a','b']",
    "msg": "Passed: 'a' in ['a','b']"
}
failed: [localhost] (item=ansible_distribution in ['AIX']) => {
    "ansible_loop_var": "item",
    "assertion": "ansible_distribution in ['AIX']",
    "changed": false,
    "evaluated_to": false,
    "item": "ansible_distribution in ['AIX']",
    "msg": "FAILED: ansible_distribution in ['AIX']"
}
ok: [localhost] => (item=ansible_distribution in ['MacOSX']) => {
    "ansible_loop_var": "item",
    "changed": false,
    "item": "ansible_distribution in ['MacOSX']",
    "msg": "Passed: ansible_distribution in ['MacOSX']"
}
...ignoring

TASK [ansible-assert : summary of asserts var=r.results|map(attribute='msg')|list] *************************
ok: [localhost] => {
    "r.results|map(attribute='msg')|list": [
        "Passed: 1 == 1",
        "Passed: 'a' in ['a','b']",
        "FAILED: ansible_distribution in ['AIX']",
        "Passed: ansible_distribution in ['MacOSX']"
    ]
}

TASK [ansible-assert : assert that=['{{ r is not failed }}']] **********************************************
fatal: [localhost]: FAILED! => {
    "assertion": false,
    "changed": false,
    "evaluated_to": false,
    "msg": "Assertion failed"

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?