Ansible Facts 变量详解与使用案例

 

主机规划

Ansible Facts 变量详解_Ansible Facts

 

添加用户账号

说明:

1、 运维人员使用的登录账号;

2、 所有的业务都放在 /app/ 下「yun用户的家目录」,避免业务数据乱放;

3、 该用户也被 ansible 使用,因为几乎所有的生产环境都是禁止 root 远程登录的(因此该 yun 用户也进行了 sudo 提权)。

1 # 使用一个专门的用户,避免直接使用root用户2 # 添加用户、指定家目录并指定用户密码3 # sudo提权4 # 让其它普通用户可以进入该目录查看信息5 useradd -u 1050 -d /app yun && echo '123456' | /usr/bin/passwd --stdin yun6 echo "yun  ALL=(ALL)       NOPASSWD: ALL" >>  /etc/sudoers7 chmod 755 /app/

 

Ansible 配置清单Inventory

之后文章都是如下主机配置清单

 1 [yun@ansi-manager ansible_info]$ pwd 2 /app/ansible_info 3 [yun@ansi-manager ansible_info]$ cat hosts_key 
 4 # 方式1、主机 + 端口 + 密钥 5 [manageservers] 6 172.16.1.180:22 7  8 [proxyservers] 9 172.16.1.18[1:2]:2210 11 # 方式2:别名 + 主机 + 端口 + 密码12 [webservers]13 web01 ansible_ssh_host=172.16.1.183 ansible_ssh_port=2214 web02 ansible_ssh_host=172.16.1.184 ansible_ssh_port=2215 web03 ansible_ssh_host=172.16.1.185 ansible_ssh_port=22

 

Facts 概述

Ansible Facts 是 Ansible 在被托管主机上自动收集的变量。它是通过在执行 Ad-Hoc 以及 Playbook 时使用 setup 模块进行收集的,并且这个操作是默认的。

因为这个收集托管主机上的 Facts 比较耗费时间,所以可以在不需要的时候关闭 setup 模块。收集的 Facts 中包含了托管主机特有的信息,这些信息可以像变量一样在 Playbook 中使用。

收集的 Facts 中包含了以下常用的信息:

主机名、内核版本、网卡接口、IP 地址、操作系统版本、环境变量、CPU 核数、可用内存、可用磁盘 等等……。

使用场景:

  1. 通过 facts 检查 CPU,生成对应的 Nginx 配置文件
  2. 通过 facts 检查内存情况,定义不同的 MySQL 配置文件或 Redis 配置文件
  3. 通过 facts 检查主机 hostname,生成不同的 zabbix 配置文件

获取指定受控端的 facts 信息

 1 [yun@ansi-manager ansible_info]$ pwd 2 /app/ansible_info 3 [yun@ansi-manager ansible_info]$ ansible 172.16.1.181 -m setup -i ./hosts_key 
 4 172.16.1.181 | SUCCESS => { 5     "ansible_facts": { 6         "ansible_all_ipv4_addresses": [ 7             "10.0.0.181", 
 8             "172.16.1.181" 9         ],10 ………………

 

如何在 playbook 中关闭 facts

 

 1 [yun@ansi-manager object03]$ pwd 2 /app/ansible_info/object03 3 [yun@ansi-manager object03]$ cat test_facts.yml 
 4 --- 5 # facts 使用 6 - hosts: proxyservers 7   # 关闭 facts 变量 8   gather_facts: no 9 10   # 这时就不能取到 ansible_hostname、ansible_eth0.ipv4.address、ansible_eth1 ['ipv4']['address'] 变量信息11   tasks:12     - name: "get ansible facts var"13       debug:14         msg: "This host name is {{ ansible_hostname }} ,eth0: {{ ansible_eth0.ipv4.address }}, eth1: {{ ansible_eth1['ipv4']['address'] }}"

 

Facts 案例-获取主机名和网卡信息

获取受控端的主机名,内网地址和外网地址

 1 [yun@ansi-manager object03]$ pwd 2 /app/ansible_info/object03 3 [yun@ansi-manager object03]$ ll 4 total 4 5 -rw-rw-r-- 1 yun yun  241 Aug 22 10:41 test_facts.yml 6 [yun@ansi-manager object03]$ cat test_facts.yml 
 7 --- 8 # facts 使用 9 - hosts: proxyservers10 11   tasks:12     - name: "get ansible facts var"13       debug:14         msg: "This host name is {{ ansible_hostname }} ,eth0: {{ ansible_eth0.ipv4.address }}, eth1: {{ ansible_eth1['ipv4']['address'] }}"15         #### 上面写了两种方式引用变量,推荐使用后一种引用方式16 17 [yun@ansi-manager object03]$ ansible-playbook -b -i ../hosts_key test_facts.yml 
18 19 PLAY [proxyservers] ***********************************************************************************************20 21 TASK [Gathering Facts] ********************************************************************************************22 ok: [172.16.1.181]23 ok: [172.16.1.182]24 25 TASK [get ansible facts var] **************************************************************************************26 ok: [172.16.1.181] => {27     "msg": "This host name is ansi-haproxy01 ,eth0: 172.16.1.181, eth1: 10.0.0.181"28 }29 ok: [172.16.1.182] => {30     "msg": "This host name is ansi-haproxy02 ,eth0: 172.16.1.182, eth1: 10.0.0.182"31 }32 33 PLAY RECAP ********************************************************************************************************34 172.16.1.181               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 35 172.16.1.182               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

Facts 案例-模拟zabbix客户端配置

根据受控端主机名的不同,在受控端生成不同的配置文件

 1 [yun@ansi-manager object03]$ pwd 2 /app/ansible_info/object03 3 [yun@ansi-manager object03]$ ll 4 total 32 5 drwxrwxr-x 2 yun yun   58 Aug 22 12:31 file 6 -rw-rw-r-- 1 yun yun  224 Aug 22 12:33 test_zabbix_agentd.yml 7 [yun@ansi-manager object03]$ cat file/vars_file.yml  # playbook 变量 8 zabbix_server: 172.16.1.180 9 10 [yun@ansi-manager object03]$ cat file/zabbix_agentd_temp.conf.j2  # 模拟 zabbix_agentd 配置文件11 # 模拟 zabbix_agentd 配置文件12 13 # zabbix 服务端配置14 Server={{ zabbix_server }}15 ServerActive={{ zabbix_server }}16 17 # zabbix 客户端配置18 Hostname={{ ansible_hostname }}19 20 [yun@ansi-manager object03]$ cat test_zabbix_agentd.yml  # 具体的 yml 文件21 ---22 # zabbix 配置23 - hosts: proxyservers24   vars_files: ./file/vars_file.yml25 26   tasks:27     - name: config zabbix_agentd28       template:29         src: ./file/zabbix_agentd_temp.conf.j230         dest: /tmp/zabbix_agentd_temp.conf31 32 [yun@ansi-manager object03]$ ansible-playbook -b -i ../hosts_key --syntax-check test_zabbix_agentd.yml  # 语法检测33 [yun@ansi-manager object03]$ ansible-playbook -b -i ../hosts_key -C test_zabbix_agentd.yml  # 预执行,测试执行34 [yun@ansi-manager object03]$ ansible-playbook -b -i ../hosts_key test_zabbix_agentd.yml  # 执行

 

受控端1配置文件查看

1 [yun@ansi-haproxy01 ~]$ cat /tmp/zabbix_agentd_temp.conf 
2 # 模拟 zabbix_agentd 配置文件3 4 # zabbix 服务端配置5 Server=172.16.1.1806 ServerActive=172.16.1.1807 8 # zabbix 客户端配置9 Hostname=ansi-haproxy01

 

受控端2配置文件查看

1 [yun@ansi-haproxy02 ~]$ cat /tmp/zabbix_agentd_temp.conf 
2 # 模拟 zabbix_agentd 配置文件3 4 # zabbix 服务端配置5 Server=172.16.1.1806 ServerActive=172.16.1.1807 8 # zabbix 客户端配置9 Hostname=ansi-haproxy02

 


  

———END———