我编写了以下剧本来获取失败的端口状态。
- name: Check port status
wait_for:
host: 127.0.0.1
port: "{{ item }}"
timeout: 2
state: started
ignore_errors: yes
with_items:
- 80
- 22
- 8080
register: wait_result
- debug:
msg: "{{ 'Failed with message: ' ~ wait_result.msg if wait_result.failed else 'Success' }}"
从调试日志中获取时msg
我无法获取超时消息。
PLAY [mysrv] ************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [localhost]
TASK [Check Nagios port status] *****************************************************************************************************************
ok: [localhost] => (item=80)
ok: [localhost] => (item=22)
failed: [localhost] (item=8080) => {"ansible_loop_var": "item", "changed": false, "elapsed": 2, "item": 8080, "msg": "Timeout when waiting for 127.0.0.1:8080"}
...ignoring
TASK [debug] ************************************************************************************************************************************
ok: [localhost] => {
"msg": "Failed with message: One or more items failed"
}
PLAY RECAP **************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
有人可以帮忙修改我的代码吗?
答案1
例如,您可以编写消息
- debug:
msg: |
{% for i in wait_result.results|select('failed') %}
{{ i.msg }}
{% endfor %}
或者创建一个列表并稍后使用
failed_msg: "{{ wait_result.results|
select('failed')|
map(attribute='msg')|list }}"
完整测试剧本的示例
- hosts: localhost
vars:
failed_msg: "{{ wait_result.results|
select('failed')|
map(attribute='msg')|list }}"
tasks:
- name: Check port status
wait_for:
host: 127.0.0.1
port: "{{ item }}"
timeout: 2
state: started
register: wait_result
ignore_errors: yes
loop: [22, 25, 53, 80]
- debug:
var: wait_result
when: debug|d(false)|bool
- debug:
var: failed_msg
- debug:
msg: |
{% for i in wait_result.results|select('failed') %}
{{ i.msg }}
{% endfor %}
- assert:
that: wait_result.results|select('failed')|length == 0
fail_msg: |
{% for i in wait_result.results|select('failed') %}
{{ i.msg }}
{% endfor %}
success_msg: All services started.
给出
PLAY [localhost] *****************************************************************************
TASK [Check port status] ********************************************************************
ok: [localhost] => (item=22)
ok: [localhost] => (item=25)
failed: [localhost] (item=53) => changed=false
ansible_loop_var: item
elapsed: 2
item: 53
msg: Timeout when waiting for 127.0.0.1:53
failed: [localhost] (item=80) => changed=false
ansible_loop_var: item
elapsed: 2
item: 80
msg: Timeout when waiting for 127.0.0.1:80
...ignoring
TASK [debug] *********************************************************************************
skipping: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] =>
failed_msg:
- Timeout when waiting for 127.0.0.1:53
- Timeout when waiting for 127.0.0.1:80
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: |-
Timeout when waiting for 127.0.0.1:53
Timeout when waiting for 127.0.0.1:80
TASK [assert] ********************************************************************************
fatal: [localhost]: FAILED! => changed=false
assertion: wait_result.results|select('failed')|length == 0
evaluated_to: false
msg: |-
Timeout when waiting for 127.0.0.1:53
Timeout when waiting for 127.0.0.1:80
PLAY RECAP ***********************************************************************************
localhost: ok=2 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=1