我有一个获取ip地址的剧本,如下所示。
---
- hosts: all
tasks:
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address']
输出:
TASK [debug] *************************************************************************************************************************************************
ok: [mwiwas01] => {
"hostvars[inventory_hostname]['ansible_default_ipv4']['address']": "10.0.12.15"
}
然而,我希望获得ip地址的最后两段,即仅12.15
。
注意:每个主机的ip地址都会发生变化,因此我正在寻找一个兼容任何给定ip版本4的标准解决方案。
如何从IP地址获取相同的内容。
使用split
函数。
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address'].split(".")[3]+hostvars[inventory_hostname]['ansible_default_ipv4']['address'].split(".")[4]