[root@afei weixin]# vim playbook.yml
---- hosts: all
tasks:- name: make facts
debug:
var: ansible_facts
[root@afei weixin]# ansible-playbook -C playbook.yml
},"hw_timestamp_filters":[],"macaddress":"52:54:00:d5:ed:f8","mtu":1500,"promisc": true,"timestamping":["tx_software","rx_software","software"],"type":"ether"},"virtualization_role":"guest","virtualization_type":"VMware"}}
PLAY RECAP ****************************************************************************192.168.240.134: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3.用变量取出事实facts中的ip部分
[root@afei weixin]# vim playbook.yml
---- hosts: all
tasks:- name: make facts
debug:
var: ansible_facts['default_ipv4']['address']~[root@afei weixin]# ansible-playbook -C playbook.yml
PLAY [all]****************************************************************************
TASK [Gathering Facts]****************************************************************
ok:[192.168.240.134]
TASK [make facts]*********************************************************************
ok:[192.168.240.134]=>{"ansible_facts['default_ipv4']['address']":"192.168.240.134"}
PLAY RECAP ****************************************************************************192.168.240.134: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4.关闭事实收集
[root@afei weixin]# vim playbook.yml
---- hosts: all
gather_facts: no
tasks:- name: make facts
debug:
var: ansible_facts['default_ipv4']['address'][root@afei weixin]# ansible-playbook playbook.yml
PLAY [all]****************************************************************************
TASK [make facts]*********************************************************************
ok:[192.168.240.134]=>{"ansible_facts['default_ipv4']['address']":"VARIABLE IS NOT DEFINED!"}
PLAY RECAP ****************************************************************************192.168.240.134: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5.即使关闭了,也可以用setup临时收集事实
[root@afei weixin]# vim playbook.yml
---- hosts: all
gather_facts: no
tasks:- name: feige
setup:- name: make facts
debug:
var: ansible_facts['default_ipv4']['address'][root@afei weixin]# ansible-playbook -C playbook.yml
PLAY [all]****************************************************************************
TASK [feige]**************************************************************************
ok:[192.168.240.134]
TASK [make facts]*********************************************************************
ok:[192.168.240.134]=>{"ansible_facts['default_ipv4']['address']":"192.168.240.134"}
PLAY RECAP ****************************************************************************192.168.240.134: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
6.创建自定义事实
[root@localhost ~]# mkdir -p /etc/ansible/facts.d
[root@localhost ~]# cd /etc/ansible/facts.d/[root@localhost facts.d]# vim feige.facts.d
[packages]
web_package = httpd
db_package = mariadb-server
[users]
user1 = joe
user2 = jane
[root@localhost ~]# ansible all -m setup|less