1、什么是facts
Ansible facts 用来自动采集,”被控端主机“ 自身的状态信息。
比如: 主机名、IP地址、系统版本、CPU数量、内存状态、磁盘状态等等
2、facts使用场景
1.通过facts变量检查被控端硬件CPU信息,从而生成不同的Nginx配置文件。
2.通过facts变量检查被控端内存状态信息,从而生成不同的memcached的配置文件。
3.通过facts变量检查被控端主机IP地址信息,从而生成不同的redis配置文件。
4.通过facts变量........
3、facts语法示例
3.1、通过 facts 获取被控端的主机名与IP地址,然后通过 debug 输出
cat <<'CAT_END' > get_ip_info.yaml
- hosts: httpd
tasks:
- name: output variable ansible facts
debug:
msg: "hostname->{{ansible_fqdn}},ip->{{ansible_default_ipv4.address}}"
CAT_END
3.2、facts开启后会影响Ansible主机的性能,如果没有采集被控端主机需求可选择关闭
]# cat get_ip_info.yaml
- hosts: httpd
gather_facts: no
tasks:
3.3、如何获取facts的变量,需要使用filter进行过滤
]# ansible localhost -m setup -a "filter="ansible_default_ipv4""
localhost | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.10.10",
"alias": "ens33",
"broadcast": "192.168.10.255",
"gateway": "192.168.10.2",
"interface": "ens33",
"macaddress": "00:0c:29:38:60:9b",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.10.0",
"type": "ether"
}
},
"changed": false
}
4、facts实践案例
4.2、示例1-根据主机IP地址生成Redis配置
4.2.1、编写redis playbook
cat <<'CAT_END'>redis.yaml
- hosts: redis
tasks:
- name: Install 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: start redis server
systemd:
name: redis
state: started
enabled: yes
handlers:
- name: restart redis server
systemd:
name: redis
state: restarted
CAT_END
4.2.2、模板配置文件
scp root@192.168.10.16:/etc/redis.conf redis.conf.j2
]# redis.conf.j2
bind 127.0.0.1 {{ ansible_ens33.ipv4.address }}
4.2.3、运行结果
]# vi /etc/redis.conf
bind 127.0.0.1 192.168.10.15
4.3、示例2-根据主机CPU生成Nginx配置
4.3.1、编写nginx playbook
cat <<'CAT_END'>nginx.yaml
- hosts: httpd
tasks:
- name: Install nginx server
yum:
name: nginx
state: present
- name: configure nginx server
template:
src: ./nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: restart nginx server
- name: start nginx server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: restart nginx server
systemd:
name: nginx
state: restarted
CAT_END
4.3.2、模板配置文件
scp root@192.168.10.16:/etc/nginx/nginx.conf nginx.conf.j2
]# vi nginx.conf.j2
...
worker_processes {{ ansible_processor_vcpus*2 }};
...
4.3.3、参数解析
ansible_processor_cores: 4 # 核心数 ( 每颗物理CPU的核心数)
ansible_processor_count: 2 # 颗数 (有几个CPU )
ansible_processor_vcpus: 8 # 总核心数
4.4、示例3-根据主机内存生成Memcached配置
4.4.1、编写memcached服务playbook
cat <<'CAT_END'>memcached.yaml
- hosts: redis
tasks:
- name: Install memcached server
yum:
name: memcached
state: present
- name: configure memcached server
template:
src: ./memcached.j2
dest: /etc/sysconfig/memcached
- name: start nginx server
systemd:
name: memcached
state: started
enabled: yes
CAT_END
4.4.2、模板配置文件
cat << 'CAT_END' > memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
#根据内存状态生成不同的配置(支持+-*/运算)
CACHESIZE="{{ ansible_memtotal_mb //2 }}"
OPTIONS=""
CAT_END
5、facts变量优化方案
5.1、方式1-关闭facts采集加速执行
]# cat ansible_facts.yml
- hosts: all
gather_facts: no
tasks:
...
5.2、方式2-Redis缓存facts加速执行
5.2.1、需知
当我们使用 gather_facts: no 关闭 facts ,确实能加速 Ansible 执行,但是有时候又需要使用 facts 中的内容,还希望执行的速度快一点,这时候可以设置facts 的缓存
5.2.2、安装redis模块
# 方法1
yum install python3-redis
# 方法2
pip install --upgrade pip
pip install redis
5.2.3、ansible 配置 连接redis的参数
~]# cat /etc/ansible/ansible.cfg
# 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 = 192.168.10.15:6379
# 若 redis 设置了密码
# fact_caching_connection = localhost:6379:0:admin
5.2.4、编写 Playbook 测试
]# cat test.yaml
- hosts: all
gather_facts: no
tasks:
- name: sleep 10
command: sleep 10