【问题标题】:Ansible: loops with inventory groups elementsAnsible:带有库存组元素的循环
【发布时间】:2020-11-26 19:54:28
【问题描述】:

我有点陷入以下问题。我有一个类似于以下内容的库存文件:

[server]
server0              ansible_host=10.254.1.35     
server1              ansible_host=10.254.1.46  

[worker]
worker0              ansible_host=10.254.1.10
worker1              ansible_host=10.254.1.12      
worker2              ansible_host=10.254.1.13  

我想创建一个角色来测试两组中所有机器之间的连接性(server0 到所有工作人员,server2 和所有工作人员等)。 所以像:

- hosts: "{{ target | default('server') }}"
  tasks
  - name: Test connectivity
    shell: |
    ping {{ worker_machine_ip }}
    args:
        executable: /bin/bash

worker_machine_ip 将替换为清单文件中每个工作人员的 IP。这可能吗?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    您可以使用special variable groups 列出特定组中的所有主机,然后使用另一个特殊变量hostvars 来获取您要ping 的其他主机的IP:

    - hosts: "{{ target | default('server') }}"
      
      tasks:
        - name: Test connectivity
          shell: ping -c 3 {{ hostvars[item].ansible_host }}
          loop: "{{ groups['worker'] }}"
          args:
              executable: /bin/bash
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      相关资源
      最近更新 更多