2017-09-26 66 views
1

下面是一個簡單的集羣清單:Ansible聚集節點的IP地址在簇

[cluster] 
host1 
host2 
host3 

每個主機具有IPv4地址配置的interface。這些信息可以用setup模塊收集,並將在ansible_facts.ansible_{{ interface }}.ipv4.address

如何從每臺主機獲得interface的IPv4地址,並使其可用於每臺主機cluster,以便每臺主機知道所有集羣IP?

這是如何在角色中實現的?

+0

此信息* *已提供給集羣中的每個主機通過使用[HOSTVARS](HTTPS://docs.ansible .COM/ansible /最新/ playbooks_variables.html#魔變量和 - 如何對接入信息有關,其他的主機)。 – larsks

回答

0

由於@larsks已經發表評論,這些信息在hostvars中可用。如果,例如,要打印所有羣集IP地址你會遍歷groups['cluster']

- name: Print IP addresses 
    debug: 
    var: hostvars[item]['ansible_eth0']['ipv4']['address'] 
    with_items: "{{ groups['cluster'] }}" 

請注意,您必須首先收集的事實來填充hostvars的羣集主機。請確保您有在您調用任務(或在你的任務中的作用)劇中以下幾點:

- name: A play 
    hosts: cluster 
    gather_facts: yes 
0

尋找如何使列表變量與Jinja2的一段時間之後。這裏是一個需要在集羣cluster_interface變量和坪的所有節點來檢查連接的完整的解決方案:

--- 
- name: gather facts about {{ cluster_interface }} 
    setup: 
    filter: "ansible_{{ cluster_interface }}" 
    delegate_to: "{{ item }}" 
    delegate_facts: True 
    with_items: "{{ ansible_play_hosts }}" 
    when: hostvars[item]['ansible_' + cluster_interface] is not defined 

- name: cluster nodes IP addresses 
    set_fact: 
    cluster_nodes_ips: "{{ cluster_nodes_ips|default([]) + [hostvars[item]['ansible_' + cluster_interface]['ipv4']['address']] }}" 
    with_items: "{{ ansible_play_hosts }}" 

- name: current cluster node IP address 
    set_fact: 
    cluster_current_node_ip: "{{ hostvars[inventory_hostname]['ansible_' + cluster_interface]['ipv4']['address'] }}" 

- name: ping all cluster nodes 
    command: ping -c 1 {{ item }} 
    with_items: "{{ cluster_nodes_ips }}" 
    changed_when: false