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. css3 - 這個右下角折角用css怎么畫出來?2. css3 - 沒明白盒子的height隨width的變化這段css是怎樣實現的?3. javascript - canvas 裁剪空白區域4. atom開始輸入!然后按tab只有空格出現沒有html格式出現5. javascript - 移動端自適應6. javascript - 在 model里定義的 引用表模型時,model為undefined。7. apache - 想把之前寫的單機版 windows 軟件改成網絡版,讓每個用戶可以注冊并登錄。類似 qq 的登陸,怎么架設服務器呢?8. android - 課程表點擊后浮動后邊透明可以左右滑動的界面是什么?9. python3.x - c++調用python310. java - 根據月份查詢多個表里的內容怎么實現好?
