我有以下剧本来获取第一个可到达的主机:
---
- hosts: all
gather_facts: yes
ignore_unreachable: yes
tasks:
- name: Get the first good host in the group
vars:
query: "[?ansible_facts!=''].inventory_hostname"
set_fact:
first_good_host: "{{ ansible_play_hosts | map('extract', hostvars) | list | json_query(query) | first }}"
delegate_to: localhost
run_once: yes
- name: Aggregate the first good host to reachable group
ansible.builtin.add_host:
name: '{{ first_good_host }}'
groups: 'reachable'
run_once: yes
- name: print reachable hosts
debug:
msg: "{{ groups['reachable'] }}"
但是,如果存在任何无法访问的主机,则此剧本会与
4
一起存在。如果我通过 ignore_unreachable: true
,那么它会以 0
退出,但 reachable
还包含 unreachable
主机,这是我不想要的。
我也尝试过
any_errors_fatal
和 max_failure_percentage
但没有运气。我不想在任何无法访问的主机上中止/失败。
如何在不使用
4
的情况下处理退出代码 ignore_unreachable: true
?
ansible_play_hosts
变量已仅包含您游戏中有效的可访问主机的列表。失败/无法访问的主机不在此列表中。因此,您不需要创建特殊的自定义变量来仅列出可访问的主机。如果您需要包含所有主机(包括无法访问的主机)的列表,您可以使用 ansible_play_hosts_all
。它包含该剧所针对的所有主机的列表。
如果我通过
,那么它会以ignore_unreachable: true
退出,但0
还包含无法访问的主机,这是我不想要的。reachable
有一个最小的示例剧本
---
- hosts: all
gather_facts: true
ignore_unreachable: true
tasks:
- name: Get the first good host in the group
set_fact:
first_good_host: "{{ ansible_play_hosts | map('extract', hostvars) | list | json_query(query) | first }}"
vars:
query: "[?ansible_facts!=''].inventory_hostname"
delegate_to: localhost
run_once: yes
- name: Aggregate the first good host to reachable group
ansible.builtin.add_host:
name: '{{ first_good_host }}'
groups: 'reachable'
- hosts: "{{ groups['reachable'] }}"
tasks:
- name: Print reachable hosts
debug:
msg: "{{ ansible_play_hosts }}"
我无法重现这一点。动态组
reachable
将仅包含第一个可到达的主机,从而产生 的输出
PLAY [all] *****************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************
ok: [test2.example.com]
ok: [test1.example.com]
fatal: [test3.example.com]: UNREACHABLE! => changed=false
msg: 'Failed to connect to the host via ssh: ssh: connect to host test3.example.com port 22: No route to host'
skip_reason: Host test3.example.com is unreachable
unreachable: true
TASK [Get the first good host in the group] ********************************************************************
ok: [test1.example.com -> localhost]
TASK [Aggregate the first good host to reachable group] ********************************************************
changed: [test1.example.com]
PLAY [[u'test1.example.com']] **********************************************************************************
TASK [Gathering Facts] *****************************************************************************************
ok: [test1.example.com]
TASK [Print reachable hosts] ***********************************************************************************
ok: [test1.example.com] =>
msg:
- test1.example.com
PLAY RECAP *****************************************************************************************************
test1.example.com: ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test2.example.com: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test3.example.com: ok=0 changed=0 unreachable=1 failed=0 skipped=1 rescued=0 ignored=0
和
ansible-playbook unreachable.yml; echo $?
0