0
点赞
收藏
分享

微信扫一扫

自动化运维工具Ansible(6) Facts变量

Facts变量简介

  • Facts变量不包含在全局变量、剧本变量及资产变量之内。
  • Facts变量不需要我们人为去声明变量名及赋值。
  • 它的声明和赋值完全有Ansible 中的 setup 模块帮我们完成。
  • 它收集了有关被管理服务器的操作系统版本、服务器IP地址、主机名,磁盘的使用情况、CPU个数、内存大小等等有关被管理服务器的私有信息。
  • 在每次PlayBook运行的时候都会发现在PlayBook执行前都会有一个Gathering Facts的过程。这个过程就是收集被管理服务器的Facts信息过程。


手动收集Facts 变量

ansible all -i localhost, -c local -m setup

过滤Facts

可以通过使用Facts 模块中的filter参数去过滤我们想要的信息

仅获取服务器的内存情况信息

ansible all -i localhost, -m setup -a "filter=*memory*" -c local

仅获取服务器的磁盘挂载情况

ansible all -i localhost, -m setup -a "filter=*mount*" -c local

在PlayBook中去使用Facts 变量

---
- name: print facts variable
hosts: all
tasks:
- name: print facts variable
debug:
msg: "The default IPV4 address is {{ansible_default_ipv4.address}}"
...


在PlayBook中去关闭Facts 变量的获取

  • 若在整个PlayBook 的执行过程中,完全未使用过 Facts 变量,此时我们可以将其关闭,以加快PlayBook的执行速度。
- name: the first play example
hosts: all
# 关闭 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=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
- name: start nginx server
service:
name: nginx
enabled: true
state: started


举报

相关推荐

0 条评论