公司的模块都新加了加密算法,现在就是需要把约100台机器的zk 进行调整,目前在 ansible控制机上已经写好了新的zk ip,然后计划是把这个新zk ip的文件下发到那100台机器里,然后这100台机器的文件中把他们各自的ip和hostname添加到这个文件上。

于是就写了一个ansible-playbook:

---

- hosts: all
  tasks:
        - name: 将原有的hosts文件备份
          shell: mv /etc/hosts /etc/hosts_bak

        - name: 将ansible端的hosts复制到各自机器上
          copy: src=/root/hosts dest=/etc/ owner=root group=root mode=0544

        - name: 在新的hosts文件后面追加各自机器内网ip和hostname
          lineinfile: dest=/etc/hosts line="`ansible_all_ipv4_addresses`  `ansible_hostname`"

但是写完之后执行出来,却是这样的效果:
222.png

而我想要的是这样的效果:
222.png

遇到这种情况怎么办?


后来调整了一下,变量用 IP: "{{ ansible_eth0['ipv4']['address'] }}",而不是`ansible_all_ipv4_addresses`

修改了之后的playbook 如下:

---

- hosts: all
  vars:
        IP: "{{ ansible_eth0['ipv4']['address'] }}"
  tasks:
        - name: 将原有的hosts文件备份
          shell: mv /etc/hosts /etc/hosts_bak

        - name: 将ansible端的hosts复制到各自机器上
          copy: src=/root/hosts dest=/etc/ owner=root group=root mode=0644

        - name: 在新的hosts文件后面追加各自机器内网ip和hostname
          lineinfile: dest=/etc/hosts line="`IP`  `ansible_hostname`"

这样就达到目的了。