配置ansible缓存加速Gathering Facts

Ansible当节点很多时会存在加载Gathering Facts很慢的情况,目前社区提供了两种方式实现Gathering Facts加速:JSONFIle和Reids缓存

JSONFIle

编辑ansible以下配置

[defaults]
callbacks_enabled=ansible.posix.profile_tasks // 用来测试加速效果
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/facts_cache

创建缓存目录

mkdir -p /tmp/facts_cache
chmod 777 /tmp/facts_cache

第一次运行ansible-playbook,加载Gathering Fact用了16.67s

Thursday 23 May 2024  17:02:04 +0800 (0:00:00.028)       0:00:35.453 ********** 
===============================================================================
Gathering Facts -------------------------------------------------------- 16.67s

第二次运行ansible-playbook,跳过Gathering Facts

Thursday 23 May 2024  17:07:24 +0800 (0:00:00.023)       0:00:17.304 ********** 
===============================================================================
rke : Check RKE Image Is Exist ------------------------------------------ 3.84s

Redis

创建redis配置文件

mkdir -p /data/redis/data
cat >/data/redis/redis.conf <<EOF
bind 0.0.0.0
port 6379
daemonize no
requirepass 123456
EOF

启动redis容器

docker run --name redis -idt \
--net host \
-p 6379:6379 \
--restart=always \
-v /etc/localtime:/etc/localtime \
-v /data/redis/redis.conf:/usr/local/etc/redis/redis.conf \
redis:6.2.10 \
redis-server /usr/local/etc/redis/redis.conf

测试访问redis

docker exec -it redis redis-cli
127.0.0.1:6379> auth 123456
OK

ansible节点安装redis python模块

pip3.6 install redis

编辑ansible配置文件

gathering = smart
fact_caching = redis
fact_caching_timeout = 86400
fact_caching_connection = 192.168.126.131:6379:0:123456

第一次运行效果

Thursday 23 May 2024  18:24:49 +0800 (0:00:00.023)       0:00:33.961 ********** 
===============================================================================
Gathering Facts -------------------------------------------------------- 16.74s

第二次运行效果

Thursday 23 May 2024  18:25:28 +0800 (0:00:00.023)       0:00:18.605 ********** 
===============================================================================
rke : Check RKE Image Is Exist ------------------------------------------ 4.65s
文章作者: 慕容峻才
文章链接: https://www.acaiblog.top/配置ansible缓存加速Gathering-Facts/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿才的博客
微信打赏
支付宝打赏