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. 极光推送 - Android app消息推送 百度 极光 个推 信鸽哪个好一些?2. javascript - 为什么当index等于5的时候,不在当前页面跳转到百度?不跳转的代码在倒数第五行3. android - VideoView与百度Map冲突4. javascript - 微信支付问题5. javascript - typescript关于接口,对象字面量额外属性检测问题,为什么使用断言或者变量时就不会检测额外属性?6. html - CSS 怎么筛选没有id的某元素7. javascript - 百度搜索网站,如何让搜索结果显示一张图片加上一段描述,如图;求教8. angular.js - angularjs如何动态改变ng-model值,在controller中获取input中输入的值并组合post发送到后台9. 为什么微信内置浏览器用$_COOKIE取不到值?10. mongoDB怎么把数据导出为csv或excel?
