如何总结Ansible Tower执行结束时的失败断言?

huangapple go评论113阅读模式
英文:

How do I summarize the failed assertions in Ansible Tower at the end of execution?

问题

我正在运行大约20-30个断言在我的Ansible Playbook中。通过忽略错误,我可以运行所有这些而不跳过。但是,如果有任何随机的2-3个断言失败,那么我必须搜索日志以查看详细信息。

有没有办法在执行结束时总结失败的断言?

附言:我可能没有条件编写自定义的Python插件,而更喜欢使用任何内置模块来执行此操作。

英文:

I am running around 20-30 assertions in my Ansible Playbook. By ignoring errors I could run all of these without skipping. However, if any random 2-3 assertions are failed then I have to search through logs to see the details.

Is there any way I could summarize the failed assertions at the end of execution?

P.S. I may not be in a position to write a custom Python plugin and prefer any built-in modules to do this.

答案1

得分: 2

以下是翻译好的内容:

- 断言:
    that: "{{ item }}"
    quiet: true
  ignore_errors: true
  loop: "{{ _assertions }}"
  vars:
    _assertions:
      - "'a' == 'a'"
      - "'a' == 'b'"
      - "'b' == 'b'"
      - "'b' == 'a'"
  register: assertions

- 失败:
    msg: ">"
      失败的结果:

      {% for assertion in assertions.results if assertion.failed %}
        - {{ assertion.msg }}: '{{ assertion.assertion }}'
      {% endfor %}
  when: assertions.results | selectattr('failed') | length
- 断言:
    that: "{{ item }}"
    quiet: true
  ignore_errors: true
  loop: "{{ _assertions }}"
  vars:
    _assertions:
      - "'a' == 'a'"
      - "'a' == 'b'"
  register: assertions_1

# 我只是一个任务间的虚拟示例
- meta: noop

- 断言:
    that: "{{ item }}"
    quiet: true
  ignore_errors: true
  loop: "{{ _assertions }}"
  vars:
    _assertions:
      - "'b' == 'b'"
      - "'b' == 'a'"
  register: assertions_2

- 失败:
    msg: ">"
      失败的结果:

      {% for assertion in assertions if assertion.failed %}
        - {{ assertion.msg }}: '{{ assertion.assertion }}'
      {% endfor %}
  when: assertions | selectattr('failed') | length
  vars:
    assertions: "{{ assertions_1.results + assertions_2.results }}"
英文:

It is not really clear if you are having tasks in-between those assertions or not, but if you are roughly running a bunch of assertions at the end of the playbook, you could actually run them in a loop — because if you have all the assertions in a list under the parameter that, the whole assertion will end at the first failure — so, you can register all their return at once.

And then have a conditional fail task that would highlight the failed assertions if there are any in the results list.

For example:

- assert:
    that: "{{ item }}"
    quiet: true
  ignore_errors: true
  loop: "{{ _assertions }}"
  vars:
    _assertions:
      - "'a' == 'a'"
      - "'a' == 'b'"
      - "'b' == 'b'"
      - "'b' == 'a'"
  register: assertions

- fail:
    msg: >-
      Failed result:

      {% for assertion in assertions.results if assertion.failed %}
        - {{ assertion.msg }}: '{{ assertion.assertion }}'
      {% endfor %}
  when: assertions.results | selectattr('failed') | length

Would yield

fatal: [localhost]: FAILED! => changed=false 
  msg: |-
    Failed result:
      - Assertion failed: ''a' == 'b''
      - Assertion failed: ''b' == 'a''

If you do have tasks in-between, you can still do it, but you'll have to register each assertions and, then, join all the list of results of the different assertions together:

- assert:
    that: "{{ item }}"
    quiet: true
  ignore_errors: true
  loop: "{{ _assertions }}"
  vars:
    _assertions:
      - "'a' == 'a'"
      - "'a' == 'b'"
  register: assertions_1

# I am just an dummy example of a task in-between
- meta: noop

- assert:
    that: "{{ item }}"
    quiet: true
  ignore_errors: true
  loop: "{{ _assertions }}"
  vars:
    _assertions:
      - "'b' == 'b'"
      - "'b' == 'a'"
  register: assertions_2

- fail:
    msg: >-
      Failed result:

      {% for assertion in assertions if assertion.failed %}
        - {{ assertion.msg }}: '{{ assertion.assertion }}'
      {% endfor %}
  when: assertions | selectattr('failed') | length
  vars:
    assertions: "{{ assertions_1.results + assertions_2.results }}"

huangapple
  • 本文由 发表于 2023年5月18日 11:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定