Facts變量Facts變量不包含在前文中介紹的全局變量、劇本變量及資產(chǎn)變量之內(nèi)。
Facts變量不需要我們?nèi)藶槿?span id="esmkr7d" class="wpcom_tag_link">聲明變量名及賦值。
它的聲明和賦值完全有Ansible 中的 setup 模塊幫我們完成。
它收集了有關(guān)被管理服務器的操作系統(tǒng)版本、服務器IP地址、主機名,磁盤的使用情況、CPU個數(shù)、內(nèi)存大小等等有關(guān)被管理服務器的私有信息。
在每次PlayBook運行的時候都會發(fā)現(xiàn)在PlayBook執(zhí)行前都會有一個Gathering Facts的過程。這個過程就是收集被管理服務器的Facts信息過程。
實驗
[root@bogon ~]# ansible all -i localhost, -c local -m setuplocalhost | SUCCESS => { “ansible_facts”: { “ansible_all_ipv4_addresses”: [ “192.168.216.133” ], “ansible_all_ipv6_addresses”: [ “fe80::486e:8249:b0e8:8cab” ], “ansible_apparmor”: { “status”: “disabled” }, “ansible_architecture”: “x86_64”, “ansible_bios_date”: “04/13/2018”, “ansible_bios_version”: “6.00”, “ansible_cmdline”: { “BOOT_IMAGE”: “/vmlinuz-3.10.0-514.el7.x86_64”, “LANG”: “zh_CN.UTF-8”, “crashkernel”: “auto”, “quiet”: true, “rd.lvm.lv”: “cl/swap”, “rhgb”: true, “ro”: true, “root”: “/dev/mapper/cl-root” }, “ansible_date_time”: { “date”: “2022-05-17”, “day”: “17”,。。。。。。出一大串
如何針對性的獲取facts模塊中的信息模糊匹配獲取服務器的內(nèi)存情況信息
[root@bogon ~]# ansible all -i localhost, -c local -m setup -a “filter=*memory*”localhost | SUCCESS => { “ansible_facts”: { “ansible_memory_mb”: { “nocache”: { “free”: 645, “used”: 331 }, “real”: { “free”: 507, “total”: 976, “used”: 469 }, “swap”: { “cached”: 0, “free”: 2047, “total”: 2047, “used”: 0 } }, “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: false}
獲取服務器的磁盤掛載情況信息
[root@bogon ~]# ansible all -i localhost, -c local -m setup -a “filter=*mount*”localhost | SUCCESS => { “ansible_facts”: { “ansible_mounts”: [ { “block_available”: 223549, “block_size”: 4096, “block_total”: 259584, “block_used”: 36035, “device”: “/dev/sda1”, “fstype”: “xfs”, “inode_available”: 523958, “inode_total”: 524288, “inode_used”: 330, “mount”: “/boot”, “options”: “rw,seclabel,relatime,attr2,inode64,noquota”, “size_available”: 915656704, “size_total”: 1063256064, “uuid”: “1154f6ac-3c7e-4be3-a9d9-3ad34dce68bc” }, { “block_available”: 4045287, “block_size”: 4096, “block_total”: 4452864, “block_used”: 407577, “device”: “/dev/mapper/cl-root”, “fstype”: “xfs”, “inode_available”: 8853899, “inode_total”: 8910848, “inode_used”: 56949, “mount”: “/”, “options”: “rw,seclabel,relatime,attr2,inode64,noquota”, “size_available”: 16569495552, “size_total”: 18238930944, “uuid”: “0562565e-3c89-40cd-8492-a82bce391441” } ], “discovered_interpreter_python”: “/usr/bin/python” }, “changed”: false}
在playbook中使用facts變量
默認會查詢每臺服務器的facts值
—- name: a play example hosts: all remote_user: root tasks: – name: install nginx package yum: name=nginx state=present – name: copy nginx.conf to remote server copy: src=”/a2020/img/data-img.jpg” data-src=nginx.conf dest=/etc/nginx/nginx.conf – name: start nginx server service: name: nginx enabled: true state: started…[root@bogon ~]# ansible-playbook facts.yamlPLAY [a play example] *****************************************************************************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************ok: [192.168.216.134]ok: [192.168.216.132]TASK [install nginx package] **********************************************************************************************************************************************************************************************************ok: [192.168.216.132]
若在整個PlayBook 的執(zhí)行過程中,完全未使用過 Facts 變量,此時我們可以將其關(guān)閉,以加快PlayBook的執(zhí)行速度
[root@bogon ~]# cat factsout.yaml—- name: a play example hosts: webservers # 關(guān)閉 facts 變量收集功能 gather_facts: no remote_user: root tasks: – name: install nginx package yum: name=nginx state=present – name: copy nginx.conf to remote server copy: src=”/a2020/img/data-img.jpg” data-src=nginx.conf dest=/etc/nginx/nginx.conf – name: start nginx server service: name: nginx enabled: true state: started…[root@bogon ~]# ansible-playbook factsout.yaml[WARNING]: Could not match supplied host pattern, ignoring: webserversPLAY [a play example] *****************************************************************************************************************************************************************************************************************skipping: no hosts matchedPLAY RECAP ****************************************************************************************************************************************************************************************************************************
注冊變量通常用于保存一個task任務的執(zhí)行結(jié)果,便于debug使用或者將此次的task任務結(jié)果作為下一task任務的條件注冊變量在playbook中通過register關(guān)鍵字去實現(xiàn)
[root@bogon ~]# cat zuce.yaml—- name: install a package and print the result hosts: webservers remote_user: root tasks: – name: install nginx package yum: name=nginx state=present register: install_result #變量名稱 – name: print result debug: var=install_result #輸出結(jié)果…[root@bogon ~]# ansible-playbook zuce.yaml[WARNING]: Could not match supplied host pattern, ignoring: webserversPLAY [install a package and print the result] *****************************************************************************************************************************************************************************************skipping: no hosts matchedPLAY RECAP ****************************************************************************************************************************************************************************************************************************
變量的優(yōu)先權(quán)變量這一篇加上,上一篇變量一大堆有全局變量,劇本,資產(chǎn),F(xiàn)acts變量,注冊變量其中facts變量不需要人為聲明賦值,注冊變量只需要register去聲明,而不需要賦值全局變量,劇本及資產(chǎn)則完全需要人為的聲明賦值*當一個變量同時在全局變量、劇本變量和資產(chǎn)變量中定義時,優(yōu)先級最高的是全局變量;其次是劇本變量;最后才是資產(chǎn)變量。