我的文件由 IP 地址组成,我使用以下方法为我的任务创建此清单
ips.text
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
main.yml
- name: Add Instance IP Addresses to temporary inventory groups
shell: cat ~/ips.text
register: serverlist
- debug: msg={{ serverlist.stdout_lines }}
- name: Add Instance IP Addresses to temporary inventory groups
add_host
groups: working_hosts
hostname: "{{item}}"
with_items: "{{ serverlist.stdout_lines }}"
- hosts: working_hosts
become: yes
现在我想让它像如果192.168.0.1是可访问的,那么它应该跳过该文件中的其余 ip,如果192.168.0.1无法到达,则只能转到下一个192.168.0.2.
我们怎样才能做到这一点?
Q: “如果 192.168.0.1 可以访问,那么它应该跳过其余的 IP。”
答:我们吧wait_for_connection在所有主机上block并将连接状态存储在变量中reachable
。然后使用变量reachable
创建可访问主机组reachable
并与组中的第一个主机一起运行新的游戏groups.reachable.0
。例如
- name: Test reachable hosts
hosts: working_hosts
gather_facts: false
vars:
connection_timeout: "10"
tasks:
- block:
- wait_for_connection:
timeout: "{{ connection_timeout }}"
rescue:
- set_fact:
reachable: false
- meta: clear_host_errors
- meta: end_host
- set_fact:
reachable: true
- add_host:
name: '{{ item }}'
groups: 'reachable'
loop: "{{ hostvars|dict2items|json_query('[?value.reachable].key') }}"
run_once: true
- hosts: "{{ groups.reachable.0 }}"
tasks:
- debug:
msg: "{{ inventory_hostname }}"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)