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 yun
6 echo "yun  ALL=(ALL)       NOPASSWD: ALL" >>  /etc/sudoers
7 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]:22
10 
11 # 方式2:別名 + 主機 + 端口 + 密碼
12 [webservers]
13 web01 ansible_ssh_host=172.16.1.183 ansible_ssh_port=22
14 web02 ansible_ssh_host=172.16.1.184 ansible_ssh_port=22
15 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: proxyservers
10 
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: proxyservers
24   vars_files: ./file/vars_file.yml
25 
26   tasks:
27     - name: config zabbix_agentd
28       template:
29         src: ./file/zabbix_agentd_temp.conf.j2
30         dest: /tmp/zabbix_agentd_temp.conf
31 
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.180
6 ServerActive=172.16.1.180
7 
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.180
6 ServerActive=172.16.1.180
7 
8 # zabbix 客戶端配置
9 Hostname=ansi-haproxy02

 


  

———END———
如果覺得不錯就關注下唄 (-^O^-) !

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM