【问题标题】:Skip other hosts if first host is reachable in Ansible如果在 Ansible 中可以访问第一台主机,则跳过其他主机
【发布时间】:2020-03-02 07:56:45
【问题描述】:

我的文件包含我使用以下方法为我的任务制作清单的 IP 地址

ips.text

192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4

ma​​in.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 可以访问,那么它应该跳过该文件中的其余 ips,如果 192.168.0.1 无法访问,那么只有转到下一个192.168.0.2

我们怎样才能做到这一点?

【问题讨论】:

    标签: ansible ansible-inventory


    【解决方案1】:

    问:“如果 192.168.0.1 可以访问,那么它应该跳过其余的 IP。”

    A:让我们在block 中的所有主机上使用wait_for_connection,并将连接状态存储在变量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 }}"
    

    【讨论】:

      【解决方案2】:

      好吧,在清单中按您的需要订购您的主机,设置任务的gather_facts: yesrun_once: yes,您就可以开始了:

      ---
      - hosts: all
        gather_facts: yes
        tasks:
        - debug:
            var: ansible_hostname
          run_once: yes
      

      在一组主机上运行此 playbook,关闭列表中的前几台主机,您将看到该任务仅在第一个响应的主机上运行。

      另一个选项类似于弗拉基米尔的建议,但使用来自gather_facts 的(缺乏)结果:

      ---
      - hosts: all
        gather_facts: yes
        tasks:
        - set_fact:
            first_good_host: "{{ ansible_play_hosts | map('extract', hostvars) | list | json_query(query) | first }}"
          run_once: yes
          delegate_to: localhost
          vars:
            query: "[?ansible_facts!=''].inventory_hostname"
      
        - debug:
            var: first_good_host
          delegate_to: localhost
      
        - add_host:
            name: '{{ first_good_host }}'
            groups: 'reachable'
          run_once: yes
      
      - hosts: reachable
        gather_facts: yes
        tasks:
        - debug:
            msg: "{{ inventory_hostname }}"
      

      祝你好运!

      【讨论】:

        猜你喜欢
        • 2018-06-07
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-10
        • 2022-11-04
        • 1970-01-01
        相关资源
        最近更新 更多