ansible 獲取內網 IP
阿新 • • 發佈:2020-11-25
問題:
由於線上機器是多家廠商的且網絡卡會有公網IP和內網IP都繫結情形,並且有些服務必須保證監聽內網IP,但在host檔案配置中有可能是內網IP,也有可能是公網IP。
使用:變數 {{ ansible_all_ipv4_addresses }} 會獲取機器所有IP,但沒有排序
使用: 變數{{ ansible_default_ipv4['address'] }}則是網絡卡預設IP,經驗證此時有可能是內網IP也有可能是公網IP
解決:
安裝:netaddr模組
yum install -y python-netaddr pip3 install netaddr
對變數ansible_all_ip_addresses
使用ipaddr
過濾
{{ ansible_all_ipv4_addresses | ipaddr('private') | first }} or {{ ansible_all_ipv4_addresses | ipaddr('10.0.0.0/8') | first }}
參考:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters_ipaddr.html
https://stackoverflow.com/questions/34730239/how-to-get-host-private-network-address-in-ansible
https://www.cnblogs.com/carriezhangyan/p/10950970.html