首先 Ping 组。无法访问的主机将被排除在未来的播放之外:
这是我的库存:
[groupA]
AnsibleTower ansible_host=192.168.124.8
[groupB]
jaxsat ansible_host=192.168.124.111
rhel7.5 ansible_host=192.168.124.4
现在,这是没有 ping 的剧本:
---
- hosts: groupB
gather_facts: no
connection: ssh
tasks:
- name: Run hostname command
command: /bin/hostname
register: result
run_once: yes
- name: Show result
debug:
var: result
run_once: yes
因为jaxsat 关闭而失败:
$ ansible-playbook -i inventory/ test_one_only.yml
PLAY [groupB] **********************************************************************************
TASK [Run hostname command] ********************************************************************
Tuesday 21 July 2020 13:40:38 -0400 (0:00:00.064) 0:00:00.064 **********
fatal: [jaxsat]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.124.111 port 22: No route to host\r\n", "unreachable": true}
[WARNING]: Failure using method (v2_runner_on_unreachable) in callback plugin
(<ansible.plugins.callback.mail.CallbackModule object at 0x7f95688fbf10>): [Errno 111]
Connection refused
NO MORE HOSTS LEFT *****************************************************************************
to retry, use: --limit @/home/jack/Ansible/TEST/test_one_only.retry
PLAY RECAP *************************************************************************************
jaxsat : ok=0 changed=0 unreachable=1 failed=0
Tuesday 21 July 2020 13:40:42 -0400 (0:00:03.218) 0:00:03.283 **********
===============================================================================
Run hostname command -------------------------------------------------------------------- 3.22s
Playbook run took 0 days, 0 hours, 0 minutes, 3 seconds
现在,我添加ping:
---
- hosts: groupB
gather_facts: no
connection: ssh
tasks:
- name: ping all hosts
ping:
- name: Run hostname command
command: /bin/hostname
register: result
run_once: yes
- name: Show result
debug:
var: result
run_once: yes
并且jaxsat 没有在其他任务上尝试过:
$ ansible-playbook -i inventory/ test_one_only.yml
PLAY [groupB] **********************************************************************************
TASK [ping all hosts] **************************************************************************
Tuesday 21 July 2020 13:42:12 -0400 (0:00:00.057) 0:00:00.057 **********
ok: [rhel7.5]
fatal: [jaxsat]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.124.111 port 22: No route to host\r\n", "unreachable": true}
[WARNING]: Failure using method (v2_runner_on_unreachable) in callback plugin
(<ansible.plugins.callback.mail.CallbackModule object at 0x7f07f21ad850>): [Errno 111]
Connection refused
TASK [Run hostname command] ********************************************************************
Tuesday 21 July 2020 13:42:15 -0400 (0:00:03.217) 0:00:03.275 **********
changed: [rhel7.5]
TASK [Show result] *****************************************************************************
Tuesday 21 July 2020 13:42:15 -0400 (0:00:00.348) 0:00:03.623 **********
ok: [rhel7.5] => {
"result": {
"changed": true,
"cmd": [
"/bin/hostname"
],
"delta": "0:00:00.001530",
"end": "2020-07-21 13:42:15.921749",
"failed": false,
"rc": 0,
"start": "2020-07-21 13:42:15.920219",
"stderr": "",
"stderr_lines": [],
"stdout": "localhost.localdomain.localdomain",
"stdout_lines": [
"localhost.localdomain.localdomain"
]
}
}
to retry, use: --limit @/home/jack/Ansible/TEST/test_one_only.retry
PLAY RECAP *************************************************************************************
jaxsat : ok=0 changed=0 unreachable=1 failed=0
rhel7.5 : ok=3 changed=1 unreachable=0 failed=0
Tuesday 21 July 2020 13:42:15 -0400 (0:00:00.037) 0:00:03.661 **********
===============================================================================
ping all hosts -------------------------------------------------------------------------- 3.22s
Run hostname command -------------------------------------------------------------------- 0.35s
Show result ----------------------------------------------------------------------------- 0.04s
Playbook run took 0 days, 0 hours, 0 minutes, 3 seconds