2020-09-04

tech2025-10-17  4

Linux高级运维-事实、变量、循环

1.事实指的是受控主机自动检测到的变量

[root@afei weixin]# ansible webservers -i inventory -m setup |less 192.168.240.134 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.240.134", "192.168.122.1" ], "ansible_all_ipv6_addresses": [ "fe80::7c16:db91:85ec:a36c" ], "ansible_apparmor": { "status": "disabled" }, "ansible_architecture": "x86_64", "ansible_bios_date": "07/29/2019", "ansible_bios_version": "6.00",

2.用变量取出所有事实facts

[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
最新回复(0)