python - ansible获取ip的问题
问题描述
各位大虾好!
小弟遇到一个问题,就是需要把约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}}'
但是写完之后执行出来,却是这样的效果:
而我想要的是这样的效果:
请问如何是好?
问题解答
回答1:问题解决了,用IP: '{{ ansible_eth0’ipv4’ }}' 而不是{{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}}'
相关文章:
1. mongodb - windows7下mongod无法正常启动2. mysql中的全文索引支持词根检索吗?3. 修改mysql配置文件的默认字符集重启后依然不生效4. MySQL视图count速度优化5. php - 类似Apple官网顶部3级导航该如何设计数据库?6. mysql - 请教一个Java做数据库缓存的问题7. 请问一下各位老鸟 我一直在学习独孤九贱 现在是在tp5 今天发现 这个系列视频没有实战8. Python两个list互换,两种不同的操作结果有差异,原因是什么呢?9. android-studio - Android Studio logcat一闪而过,如何显示出来10. javascript - ueditor引入报错问题

网公网安备