Ansible:将清单中的 IP 添加到所有节点的 /etc/hosts

Kev*_*vin 12 ansible

我正在部署一个小型 3 节点集群,我想将清单中定义的公共 IP 地址添加到所有节点的 /etc/hosts 文件中。

我正在尝试使用以下内容,但它给了我一个错误:

- name: Add IP address of all hosts to all hosts
  lineinfile: 
    dest: /etc/hosts
    line: '{{ hostvars[item]["ansible_host"] }} {{ hostvars[item]["ansible_hostname"] }} {{ hostvars[item]["ansible_nodename"] }}'
    state: present
  with_items: groups['all']
Run Code Online (Sandbox Code Playgroud)

错误是:

致命:[app1.domain.com]:失败!=> {"failed": true, "msg": "the field 'args' has an invalid value, 它似乎包含一个未定义的变量。错误是:'ansible.vars.hostvars.HostVars object' has no属性 u"groups['all']"\n\n错误似乎在 '/Users/k/Projects/Ansible/roles/common/tasks/main.yml':第 29 行,第 3 列,但可能\ nbe 位于文件中的其他位置,具体取决于确切的语法问题。\n\n有问题的行似乎是:\n\n\n- name: 将所有主机的 IP 地址添加到所有主机\n ^ here\n"}

关于我缺少什么的任何想法?

Bas*_*l A 24

前面的答案根本不起作用,因为它为同一主机添加了一个新行,而不是在主机的 IP 地址更改时修改现有行。

以下解决方案考虑了特定服务器的 ip 地址何时发生变化,并通过仅修改行而不是添加重复条目来很好地处理它。

---
- name: Add IP address of all hosts to all hosts
  lineinfile:
    dest: /etc/hosts
    regexp: '.*{{ item }}$'
    line: "{{ hostvars[item].ansible_host }} {{item}}"
    state: present
  when: hostvars[item].ansible_host is defined
  with_items: "{{ groups.all }}"
Run Code Online (Sandbox Code Playgroud)


gbo*_*olo 16

看起来你的语法有错误。另外你使用的是什么版本的ansible?变量名可能不同。在版本上2.2这对我有用:

- name: Add IP address of all hosts to all hosts
  lineinfile:
    dest: /etc/hosts
    line: "{{ hostvars[item].ansible_host }} {{ hostvars[item].inventory_hostname }} {{ hostvars[item].inventory_hostname_short }}"
    state: present
  with_items: "{{ groups.all }}"
Run Code Online (Sandbox Code Playgroud)

更新

Basil 已经考虑过 IP 发生变化时的情况。在这种情况下,最好使用他建议的解决方案:

- name: Add IP address of all hosts to all hosts
  lineinfile:
    dest: /etc/hosts
    regexp: '.*{{ item }}$'
    line: "{{ hostvars[item].ansible_host }} {{item}}"
    state: present
  when: hostvars[item].ansible_host is defined
  with_items: "{{ groups.all }}"
Run Code Online (Sandbox Code Playgroud)

  • 在 `regexp: '.*{{ item }}$'` 上,如果我们有两个名为 `s1` 和 `ss1` 的主机怎么办? (2认同)

小智 5

我遇到了同样的问题,这是我为感兴趣的人提供的解决方案。

主机/dev.ini

[controller]
controller1 ansible_ssh_host=10.11.11.10
controller2 ansible_ssh_host=10.11.11.11
controller3 ansible_ssh_host=10.11.11.12

[compute]
compute1 ansible_ssh_host=10.11.11.13
compute2 ansible_ssh_host=10.11.11.14
compute3 ansible_ssh_host=10.11.11.15

[block]
block1 ansible_ssh_host=10.11.11.16
block2 ansible_ssh_host=10.11.11.17

[haproxy]
haproxy1 ansible_ssh_host=10.11.11.18

[nginx]
nginx1 ansible_ssh_host=10.11.11.19

[deployment]
deployment ansible_ssh_host=10.11.11.20

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Run Code Online (Sandbox Code Playgroud)

任务/main.yml

---
- name: Update /etc/hosts
  become: true
  blockinfile:
      path: /etc/hosts
      create: yes
      block: |
        127.0.0.1 localhost

        # The following lines are desirable for IPv6 capable hosts
        ::1 ip6-localhost ip6-loopback
        fe00::0 ip6-localnet
        ff00::0 ip6-mcastprefix
        ff02::1 ip6-allnodes
        ff02::2 ip6-allrouters
        ff02::3 ip6-allhosts

        {% for item in ansible_play_batch %}
        {{ hostvars[item].ansible_ssh_host }}   {{ item }}    
        {% endfor %}
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 蟒蛇 3.7.5 ansible 2.9.0
  • 我决定使用 blockinfile 而不是模板,因为 hostvars 上下文没有在模板内更新。另外我很着急:-)。