问题:
想要使用ansible-playbook脚本创建一个以远程机器的IP命名的目录
错误的代码:# vim test.yml
--- - hosts: all remote_user: root vars: collect_info: "/data/test/{{ansible_all_ipv4_addresses}}/"tasks: - name: create IP directory file: name: "{{collect_info}}" state: directory
# ls -l /data/test/总用量 0drwxr-xr-x 2 root root 6 5月 30 17:27 [u'192.168.37.135']
原因是:获取的格式不对
# ansible all -m setup -a ansible_all_ipv4_addresses. | SUCCESS =>: ansible_default_ipv4”获取的格式则刚好可以使用
# ansible all -m setup -a "filter="ansible_default_ipv4""127.0.0.1 | SUCCESS => {"ansible_facts": {"ansible_default_ipv4": {"address": "192.168.0.1", "alias": "ens19", "broadcast": "192.168.0.255", "gateway": "192.168.0.254", "interface": "ens19", "macaddress": "00:0c:29:39:2c:95", "mtu": 1500, "netmask": "255.255.255.0", "network": "192.168.0.0", "type": "ether"} }, "changed": false}
正确的代码是:
--- - hosts: all remote_user: root vars: collect_info: "/data/test/{{ansible_default_ipv4['address']}}/"tasks: - name: create IP directory file: name: "{{collect_info}}" state: directory
网上有些说方法是:{{ipv4_eth0['address']}},但是我的机器setup获取到的数值中没有ipv4_eth0,所以在截取数值之前先确认一下ip地址位于哪个部分,然后再截取正确的数值。