仅将ansible playbook运行到活动主机

时间:2018-04-23 11:46:50

标签: automation ansible

有什么方法可以运行我的ansible playbook,所以它只能在当前活动的主机上执行?

真的会有所帮助。

1 个答案:

答案 0 :(得分:-1)

您可以在主机上运行ping并保存应答者以运行其他任务...

- name: check reachable hosts
  hosts: all
  gather_facts: no
  tasks:
    - command: ping -c1 {{ inventory_hostname }}
      delegate_to: localhost
      register: ping_result
      ignore_errors: yes
    - group_by: key=reachable
      when: ping_result|success

然后,为新的主机集运行新任务。

- name: your actual play
  hosts: reachable
  gather_facts: yes
  tasks:
    - debug: msg="this is {{ ansible_hostname }}"

来源:http://www.googlinux.com/ansible-run-play-only-on-reachable-host/