ansible-变量2

Facts变量Facts变量不包含在前文中介绍的全局变量、剧本变量及资产变量之内。

Facts变量不需要我们人为去声明变量名及赋值

它的声明和赋值完全有Ansible 中的 setup 模块帮我们完成。

它收集了有关被管理服务器的操作系统版本、服务器IP地址、主机名,磁盘的使用情况、CPU个数、内存大小等等有关被管理服务器的私有信息

在每次PlayBook运行的时候都会发现在PlayBook执行前都会有一个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模块中的信息模糊匹配获取服务器的内存情况信息

[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 的执行过程中,完全未使用过 Facts 变量,此时我们可以将其关闭,以加快PlayBook的执行速度

[root@bogon ~]# cat factsout.yaml—- name: a play example hosts: webservers # 关闭 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任务的执行结果,便于debug使用或者将此次的task任务结果作为下一task任务的条件注册变量在playbook中通过register关键字去实现

[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 #输出结果…[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 ****************************************************************************************************************************************************************************************************************************

变量的优先权变量这一篇加上,上一篇变量一大堆有全局变量,剧本,资产,Facts变量,注册变量其中facts变量不需要人为声明赋值,注册变量只需要register去声明,而不需要赋值全局变量,剧本及资产则完全需要人为的声明赋值*当一个变量同时在全局变量、剧本变量和资产变量中定义时,优先级最高的是全局变量;其次是剧本变量;最后才是资产变量。

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
(0)
用户投稿
上一篇 2022年6月13日
下一篇 2022年6月13日

相关推荐

联系我们

联系邮箱:admin#wlmqw.com
工作时间:周一至周五,10:30-18:30,节假日休息