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復(fù)制到各自機(jī)器上
copy: src=/root/hosts dest=/etc/ owner=root group=root mode=0544
- name: 在新的hosts文件后面追加各自機(jī)器內(nèi)網(wǎng)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?
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復(fù)制到各自機(jī)器上
copy: src=/root/hosts dest=/etc/ owner=root group=root mode=0644
- name: 在新的hosts文件后面追加各自機(jī)器內(nèi)網(wǎng)ip和hostname
lineinfile: dest=/etc/hosts line="{{IP}} {{ansible_hostname}}"