Ansible:获取一个组的所有IP地址

Ansible: Get all the IP addresses of a group

让我们想象一个这样的库存文件:

node-01 ansible_ssh_host=192.168.100.101
node-02 ansible_ssh_host=192.168.100.102
node-03 ansible_ssh_host=192.168.100.103
node-04 ansible_ssh_host=192.168.100.104
node-05 ansible_ssh_host=192.168.100.105

[mainnodes]
node-[01:04]

在我的剧本中,我现在想创建一些包含组 mainnodes 的 IP 地址的变量:

vars:
  main_nodes_ips: "192.168.100.101,192.168.100.102,192.168.100.103,192.168.100.104"
  main_nodes_ips_with_port: "192.168.100.101:3000,192.168.100.102:3000,192.168.100.103:3000,192.168.100.104:3000"

这是我目前得到的:

vars:
  main_nodes_ips: "{{groups['mainnodes']|join(',')}}"
  main_nodes_ips_with_port: "{{groups['mainnodes']|join(':3000,')}}"

但那样会使用主机名而不是 IP 地址。

知道如何做到这一点吗?

更新:

看了一会儿文档,我想这可以让我遍历所有的 ip 地址:

{% for host in groups['mainnodes'] %}
    {{hostvars[host]['ansible_ssh_host']}}
{% endfor %}

但我就是不知道如何创建一个包含所有这些 IP 的数组。这样我就可以对它们使用 |join() 命令。

更新2:
我只是以为我已经弄明白了......但事实证明你不能在剧本中使用 {% %} 语法......或者我可以吗? 那么在 vars 部分它没有。 :/

vars:
  {% set main_nodes_ip_arr=[] %}
  {% for host in groups['mesos-slave'] %}
     {% if main_nodes_ip_arr.insert(loop.index,hostvars[host]['ansible_ssh_host']) %} {% endif %}
  {% endfor %}
  main_nodes_ips: "{{main_nodes_ip_arr|join(',')}}"
  main_nodes_ips_with_port: "{{main_nodes_ip_arr|join(':3000,')}}"

我现在可以自己工作了。我对这个解决方案不太满意,但它可以:

main_nodes_ips: "{% set IP_ARR=[] %}{% for host in groups['mainnodes'] %}{% if IP_ARR.insert(loop.index,hostvars[host]['ansible_ssh_host']) %}{% endif %}{% endfor %}{{IP_ARR|join(',')}}"
main_nodes_ips_with_port: "{% set IP_ARR=[] %}{% for host in groups['mainnodes'] %}{% if IP_ARR.insert(loop.index,hostvars[host]['ansible_ssh_host']) %}{% endif %}{% endfor %}{{IP_ARR|join(':3000,')}

我前一段时间遇到了这个问题,这就是我想出的(不是最优的,但它有效)

---
# playbook.yml
  - hosts: localhost
    connection: local

    tasks:
      - name: create deploy template
        template:
          src: iplist.txt
          dest: /tmp/iplist.txt
      - include_vars: /tmp/iplist.txt

      - debug: var=ip

模板文件是

ip:
{% for h in groups['webservers'] %}
 - {{ hostvars[h].ansible_ssh_host }}
{% endfor %}

我找到了魔法map extracthere.

main_nodes_ips: "{{ groups['mainnodes'] | map('extract', hostvars, ['ansible_host']) | join(',') }}"
main_nodes_ips_with_port: "{{ groups['mainnodes'] | map('extract', hostvars, ['ansible_host']) | join(':3000,') }}:3000"

替代方案(想法来自here):

main_nodes_ips: "{{ groups['mainnodes'] | map('extract', hostvars, ['ansible_eth0', 'ipv4', 'address']) | join(',') }}"

(假设接口为eth0

我发现 "only way" 可以访问其他组的 ip,当满足以下任一条件时:

  • 一些成员还没有被 ansible 引导
  • 使用串行
  • 组不是剧本的一部分

如下:

{% set ips=[] %}{% for host in groups['othergroup'] %}{% if ips.append(lookup('dig', host)) %}{% endif %}{% endfor %}{{ ips }}

机器上需要dnspython 运行 ansible,通过

安装
sudo apt-get install python-dnspython

如果有人知道给定条件的更好方法,我很乐意摆脱这种可憎的事情。

我已经通过在剧本中使用可靠的事实来做到这一点。 该剧本采用 ansible_all_ipv4_addresses 列表和 ansible_nodename(实际上是完全限定的域名),遍历所有主机并将数据保存在本地主机上的 localpath_to_save_ips 文件中。您可以将 localpath_to_save_ips 更改为本地主机上的绝对路径。

---
- hosts: all
  become: yes
  gather_facts: yes

  tasks:
  - name: get ip
    local_action: shell echo {{ ansible_all_ipv4_addresses }} {{ ansible_nodename }} >> localpath_to_save_ips

- name: Create List of nodes to be added into Cluster
  set_fact: nodelist={%for host in groups['mygroup']%}"{{hostvars[host].ansible_eth0.ipv4.address}}"{% if not loop.last %},{% endif %}{% endfor %}

 - debug: msg=[{{nodelist}}]

 - name: Set Cluster node list in config file
   lineinfile:
         path: "/etc/myfonfig.cfg"
         line: "hosts: [{{ nodelist }}]"

作为结果,您将在配置文件中包含以下行:

hosts: ["192.168.126.38","192.168.126.39","192.168.126.40"]

这是我为了不被依赖而做的eth0(感谢 ADV-IT 的回答):

- name: gathering facts
  hosts: mainnodes
  gather_facts: true

- hosts: mainnodes
  tasks:
    - name: Create List of nodes
      set_fact: nodelist={%for host in groups['mainnodes']%}"{{hostvars[host]['ansible_env'].SSH_CONNECTION.split(' ')[2]}}"{% if not loop.last %},{% endif %}{% endfor %}

这对我有用。不依赖接口名称

- main_nodes_ips: "{{ groups['mainnodes'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | join(',') }}"

我 运行 在获取另一个组中的节点的 IP 地址时遇到了类似的问题。 使用像这样的结构: the_ip: "{{ hostvars[groups['master'][0]]['ansible_default_ipv4'].address }}" 仅当 运行 组 master 时才有效,这不是我的剧本的一部分(我在本地主机上是 运行)。 我通过在剧本中添加额外的剧本来解决这个问题,例如:

- hosts: master
  gather_facts: yes
  become: no
  vars: 
    - the_master_ip: "{{ hostvars[groups['master'][0]]['ansible_default_ipv4'].address }}"
  tasks:
  - debug: var=the_master_ip
  - set_fact: the_ip={{ the_master_ip }}

之后我就可以在接下来的剧本中使用the_ip

这也可以解决@Petroldrake 提到的可憎问题?

##Just fetch Ip's using -ansible_default_ipv4.address- 重定向到本地文件然后使用它

  • 姓名:gathering_facts 主持人:主持人 gather_facts:真 任务:

    • name: 重定向到文件 shell: 回显 "{{ansible_default_ipv4.address}}" >>ipss.txt delegate_to: 本地主机