search

Home  >  Q&A  >  body text

python - problem with ansible getting ip

Hello everyone!

I encountered a problem, that is, I need to adjust the zk of about 100 machines. Currently, a new zk ip has been written on the ansible control machine, and the plan is to distribute the file of this new zk ip to those 100 machines. in each machine, and then add their respective IPs and hostnames to this file in the files of these 100 machines.

So I wrote an 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}}"

But after writing it and executing it, the effect is like this:

What I want is this effect:

What should I do?

仅有的幸福仅有的幸福2751 days ago678

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-05-18 10:58:00

    The problem is solved, use IP: "{{ ansible_eth0'ipv4' }}" instead of {{ansible_all_ipv4_addresses}}

    The modified playbook is as follows:

    ---
    
    - 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}}"
    

    reply
    0
  • Cancelreply