我有一本有多个剧本的剧本,如下所示,
- name: play1
hosts: host1
tasks:
- command: dgfdgd
- name: play2
hosts: host2
tasks:
- command: ls
我得到的输出如下,
PLAY [play1] *****************************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [10.15.23.76]
TASK [command] ***************************************************************************************
fatal: [10.15.23.76]: FAILED! => {"changed": false, "cmd": "dgfdgd", "msg": "[Errno 2] No such file or directory", "rc": 2}
PLAY RECAP *******************************************************************************************
10.155.23.76 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
我希望两个剧本都执行,即使一个剧本执行失败。如果我使用ignore_errors:是,它将执行,但我也希望失败的显示在PLAY RECAP摘要中。
请对此提供任何支持。
答案 0 :(得分:0)
使用 block 和救援。例如剧本
shell> cat playbook.yml
- name: play1
hosts: localhost
tasks:
- block:
- command: /bin/true
rescue:
- set_fact:
my_rescued: "{{ my_rescued|default(0)|int + 1 }}"
- block:
- command: /bin/false
rescue:
- set_fact:
my_rescued: "{{ my_rescued|default(0)|int + 1 }}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- name: play2
hosts: localhost
tasks:
- block:
- command: /bin/false
rescue:
- set_fact:
my_rescued: "{{ my_rescued|default(0)|int + 1 }}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- name: play3
hosts: localhost
tasks:
- block:
- command: /bin/true
rescue:
- set_fact:
my_rescued: "{{ my_rescued|default(0)|int + 1 }}"
- debug:
var: my_rescued
给予(删节)
TASK [debug] *****************************************************************
ok: [localhost] =>
my_rescued: '2'
PLAY RECAP *******************************************************************
localhost: ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=2 ignored=0
Q:“有没有办法将my_rescued添加到失败计数?”
A:PLAY RECAP
统计值在剧本级别不可用。对剧本结果的这种评估应该在剧本之外进行。例如在脚本,AWX,ansible-runner等中。
例如下面的脚本
shell> cat playbook.sh
#!/bin/bash
out=$(ansible-playbook playbook.yml 2>&1 | tail -2)
host=$(echo $out | cut -d ':' -f 1 | xargs)
results=$(echo $out | cut -d ':' -f 2)
ok=$(echo $results | cut -d ' ' -f 1 | cut -d '=' -f 2)
changed=$(echo $results | cut -d ' ' -f 2 | cut -d '=' -f 2)
unreachable=$(echo $results | cut -d ' ' -f 3 | cut -d '=' -f 2)
failed=$(echo $results | cut -d ' ' -f 4 | cut -d '=' -f 2)
skipped=$(echo $results | cut -d ' ' -f 5 | cut -d '=' -f 2)
rescued=$(echo $results | cut -d ' ' -f 6 | cut -d '=' -f 2)
ignored=$(echo $results | cut -d ' ' -f 7 | cut -d '=' -f 2)
printf "$host: ok=$ok changed=$changed unreachable=$unreachable \
failed=$failed skipped=$skipped rescued=$rescued ignored=$ignored\n"
给予
shell> ./playbook.sh
localhost: ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=2 ignored=0