Ansible Facts Variables
# 什么是facts
Ansible facts
用来自动采集,”被控端主机“ 自身的状态信息。
比如: 主机名、IP地址、系统版本、CPU数量、内存状态、磁盘状态等等。
# facts使用场景
- 通过facts变量检查被控端硬件CPU信息,从而生成不同的Nginx配置文件。
- 通过facts变量检查被控端内存状态信息,从而生成不同的memcached的配置文件。
- 通过facts变量检查被控端主机IP地址信息,从而生成不同的redis配置文件。
- 通过facts变量........
# facts语法示例
通过
facts
获取被控端的主机名与IP地址,然后通过debug
输出;[root@manager facts]# cat facts.yml - hosts: webservers tasks: - name: Output variables ansible facts debug: msg: > this default IPv4 address "{{ ansible_fqdn}}" is "{{ ansible_default_ipv4.address }}"
1
2
3
4
5
6
7
8facts开启后会影响Ansible主机的性能,如果没有采集被控端主机需求可选择关闭
[root@manager facts]# cat facts.yml - hosts: webservers gather_facts: no #关闭信息采集 tasks:
1
2
3
4
如何获取facts的变量,需要使用filter进行过滤
[root@manager facts]# ansible localhost -m setup -a "filter='ansible_default_ipv4'" localhost | SUCCESS => { "ansible_facts": { "ansible_default_ipv4": { "address": "10.0.0.11", "alias": "ens33", "broadcast": "10.0.0.255", "gateway": "10.0.0.2", "interface": "ens33", "macaddress": "00:50:56:38:9d:ec", "mtu": 1500, "netmask": "255.255.255.0", "network": "10.0.0.0", "type": "ether" } }, "changed": false }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# facts实践案例
# 案例1-根据主机IP地址生成Redis配置
准备两台物理主机
10.0.0.12 10.0.0.13
编写redis服务playbook
[root@manager facts]# cat redis.yml - hosts: webservers tasks: - name: Installed Redis Server yum: name: redis state: present - name: Configure Redis Server template: src: ./redis.conf.j2 dest: /etc/redis.conf notify: Restart Redis Server - name: Started Redis Server systemd: name: redis state: started enabled: yes handlers: - name: Restart Redis Server systemd: name: redis state: restarted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25配置文件如下
[root@manager facts]# cat redis.conf.j2 ... bind 127.0.0.1 {{ ansible_ens33.ipv4.address }} ...
1
2
3
4
# 案例2-根据主机CPU生成Nginx配置
准备两台物理CPU核心不一样的主机
10.0.0.12 1核
10.0.0.13 2核
编写nginx服务playbook
[root@manager facts]# cat nginx.yml - hosts: webservers tasks: - name: Install Nginx Server yum: name: nginx state: present - name: Configure Nginx.conf template: src: ./nginx.conf.j2 dest: /tmp/nginx.conf notify: Restart Nginx Server - name: Started Nginx Server systemd: name: nginx state: started enabled: yes handlers: - name: Restart Nginx Server systemd: name: nginx state: restarted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25nginx配置文件如下
ansible_processor_cores: 4 # 核心数 ( 每颗物理CPU的核心数)
ansible_processor_count: 2 # 颗数 (有几个CPU )
ansible_processor_vcpus: 8 # 总核心数
[root@manager facts]# cat nginx.conf.j2 user www; worker_processes {{ ansible_processor_vcpus * 2 }}; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# facts变量优化方案
# 方式1-关闭facts采集加速执行
编写
TASK
任务sleep10
秒,针对15
台机器同时执行,需要消耗的时间大概是1m54.980s
- hosts: all tasks: - name: sleep 10 command: sleep 10
1
2
3
4使用
gather_facts: no
关闭facts
信息采集,发现仅花费了0m38.164s
,整个速度提升了3倍,如果调整forks
操作的主机的数量,也可以得到非常大的提升;- hosts: all gather_facts: no tasks: - name: sleep 10 command: sleep 10
1
2
3
4
5
# 方式2-Redis缓存facts加速执行
当我们使用
gather_facts: no
关闭facts
,确实能加速Ansible
执行,但是有时候又需要使用facts
中的内容,还希望执行的速度快一点,这时候可以设置facts
的缓存;[root@manager facts]# yum install python2-redis [root@manager facts]# pip install --upgrade pip [root@manager facts]# pip install redis [root@manager facts]# cat /etc/ansible/ansible.cfg [defaults] # smart 表示默认收集 facts,但 facts 已有的情况下不会收集,即使用缓存 facts # implicit 表示默认收集 facts,要禁止收集,必须使用 gather_facts: False; # explicit 则表示默认不收集,要显式收集,必须使用 gather_facts: Ture。 gathering = smart #在使用 facts 缓存时设置为smart fact_caching_timeout = 86400 fact_caching = redis fact_caching_connection = 10.0.0.12:6379 # 若 redis 设置了密码 # fact_caching_connection = localhost:6379:0:admin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2. 编写`Playbook`测试;
```sh
[root@manager facts]# cat ansible_facts.yml
- hosts: all
tasks:
- name: sleep 10
command: sleep 10
```
3. 测试结果如下:
- 执行第一次花费了`1m49.881s`因为第一次需要将`facts`信息缓存至`Redis`
- 执行第二次花费了`0m38.130s`可以看出使用`Redis`缓存`facts`变量,整体执行时间提高了3倍
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Last Updated: 2022/04/01, 21:45:19