Facts 是用來采集目標系統信息的,具體是用setup模塊來采集得。
使用setup模塊來獲取目標系統信息
ansible hostname -m setup
僅顯示與ansible相關的內存信息
ansible all -m setup -a 'filter=ansible_*_mb'
常用的變量
- ansible_distribution
- ansible_distribution_release
- ansible_distribution_version
- ansible_fqdn
- ansible_hostname
- ansible_os_family
- ansible_pkg_mgr
- ansible_default_ipv4.address
- ansible_default_ipv6.address
關閉自動采集
- hosts: whatever
gather_facts: no
自定義目標系統facts
在遠程主機/etc/ansible/facts.d/目錄下創建.fact 結尾的文件,也可以是json、ini 或者返回json 格式數據的可執行文件,這些將被作為遠程主機本地的facts 執行
Paste_Image.png
可以通過{{ ansible_local.preferences.test.h }}
方式來使用該變量
Paste_Image.png
Facts 使用文件作為緩存
修改ansible配置文件
# /etc/ansible/ansible.cfg
fact_caching = jsonfile
fact_caching_connection = /tmp/facts_cache
mkdir /tmp/facts_cache
chmod 777 /tmp/facts_cache/
運行playbook
ansible-playbook facts.yml
查看緩存目錄
Paste_Image.png
上述文件中存儲著json序列化的facts數據
Facts 使用redis作為緩存
安裝redis
yum -y install redis-server
easy_install pip
pip install redis
配置redis,使用密碼登陸
#vim /etc/redis.conf
requirepass "admin"
啟動redis
service redis start
修改ansible配置文件
# /etc/ansible/ansible.cfg
gathering = smart
fact_caching = redis
fact_caching_timeout = 86400
fact_caching_connection = localhost:6379:0:admin
運行playbook
ansible-playbook facts.yml
查看redis內容
Paste_Image.png
Facts 使用memcached作為緩存
安裝 memcached
yum install memcached
easy_install pip
pip install python-memcached
啟動memcached
/usr/bin/memcached -d -u memcached
修改ansible配置文件
# /etc/ansible/ansible.cfg
gathering = smart
fact_caching = memcached
fact_caching_timeout = 86400
fact_caching_connection = localhost:11211
查看memcached內容
Paste_Image.png
更多文章請看 Ansible 專題文章總覽