亚洲全黄无码一级在线看_国产剧情久久久性色_无码av一区二区三区无码_亚洲成a×人片在线观看

當(dāng)前位置: 首頁(yè) > 科技新聞 >

Ansible Facts 變量詳解

時(shí)間:2020-06-19 17:33來(lái)源:網(wǎng)絡(luò)整理 瀏覽:
主機(jī)規(guī)劃添加用戶賬號(hào)說(shuō)明:1、 運(yùn)維人員使用的登錄賬號(hào);2、 所有的業(yè)務(wù)都放在 /app/ 下「yun用戶的家目錄」,避免業(yè)務(wù)數(shù)據(jù)亂放;3、

主機(jī)規(guī)劃

Ansible Facts 變量詳解


添加用戶賬號(hào)

說(shuō)明:

1、 運(yùn)維人員使用的登錄賬號(hào);

2、 所有的業(yè)務(wù)都放在 /app/ 下「yun用戶的家目錄」,避免業(yè)務(wù)數(shù)據(jù)亂放;

3、 該用戶也被 ansible 使用,因?yàn)閹缀跛械纳a(chǎn)環(huán)境都是禁止 root 遠(yuǎn)程登錄的(因此該 yun 用戶也進(jìn)行了 sudo 提權(quán))。

1 # 使用一個(gè)專門的用戶,避免直接使用root用戶
2 # 添加用戶、指定家目錄并指定用戶密碼
3 # sudo提權(quán)
4 # 讓其它普通用戶可以進(jìn)入該目錄查看信息
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

之后文章都是如下主機(jī)配置清單

 1 [yun@ansi-manager ansible_info]$ pwd
2 /app/ansible_info
3 [yun@ansi-manager ansible_info]$ cat hosts_key
4 # 方式1、主機(jī) + 端口 + 密鑰
5 [manageservers]
6 172.16.1.180:22
7
8 [proxyservers]
9 172.16.1.18[1:2]:22
10
11 # 方式2:別名 + 主機(jī) + 端口 + 密碼
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 在被托管主機(jī)上自動(dòng)收集的變量。它是通過(guò)在執(zhí)行 Ad-Hoc 以及 Playbook 時(shí)使用 setup 模塊進(jìn)行收集的,并且這個(gè)操作是默認(rèn)的。

因?yàn)檫@個(gè)收集托管主機(jī)上的 Facts 比較耗費(fèi)時(shí)間,所以可以在不需要的時(shí)候關(guān)閉 setup 模塊。收集的 Facts 中包含了托管主機(jī)特有的信息,這些信息可以像變量一樣在 Playbook 中使用。

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

主機(jī)名、內(nèi)核版本、網(wǎng)卡接口、IP 地址、操作系統(tǒng)版本、環(huán)境變量、CPU 核數(shù)、可用內(nèi)存、可用磁盤 等等……。

使用場(chǎng)景:

通過(guò) facts 檢查 CPU,生成對(duì)應(yīng)的 Nginx 配置文件通過(guò) facts 檢查內(nèi)存情況,定義不同的 MySQL 配置文件或 Redis 配置文件通過(guò) facts 檢查主機(jī) 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 中關(guān)閉 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 # 關(guān)閉 facts 變量
8 gather_facts: no
9
10 # 這時(shí)就不能取到 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 案例-獲取主機(jī)名和網(wǎng)卡信息

獲取受控端的主機(jī)名,內(nèi)網(wǎng)地址和外網(wǎng)地址

 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客戶端配置

根據(jù)受控端主機(jī)名的不同,在受控端生成不同的配置文件

 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 服務(wù)端配置
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 # 語(yǔ)法檢測(cè)
33 [yun@ansi-manager object03]$ ansible-playbook -b -i ../hosts_key -C test_zabbix_agentd.yml # 預(yù)執(zhí)行,測(cè)試執(zhí)行
34 [yun@ansi-manager object03]$ ansible-playbook -b -i ../hosts_key test_zabbix_agentd.yml # 執(zhí)行

受控端1配置文件查看

1 [yun@ansi-haproxy01 ~]$ cat /tmp/zabbix_agentd_temp.conf 
2 # 模擬 zabbix_agentd 配置文件
3
4 # zabbix 服務(wù)端配置
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 服務(wù)端配置
5 Server=172.16.1.180
6 ServerActive=172.16.1.180
7
8 # zabbix 客戶端配置
9 Hostname=ansi-haproxy02


Ansible Facts 變量詳解

推薦內(nèi)容