Facts 是用來(lái)采集目標(biāo)系統(tǒng)信息的,具體是用setup模塊來(lái)采集得。
使用setup模塊來(lái)獲取目標(biāo)系統(tǒng)信息
ansible hostname -m setup
僅顯示與ansible相關(guān)的內(nèi)存信息
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
關(guān)閉自動(dòng)采集
- hosts: whatever
gather_facts: no
自定義目標(biāo)系統(tǒng)facts
在遠(yuǎn)程主機(jī)/etc/ansible/facts.d/目錄下創(chuàng)建.fact 結(jié)尾的文件,也可以是json、ini 或者返回json 格式數(shù)據(jù)的可執(zhí)行文件,這些將被作為遠(yuǎn)程主機(jī)本地的facts 執(zhí)行
Paste_Image.png
可以通過(guò){{ ansible_local.preferences.test.h }}
方式來(lái)使用該變量
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/
運(yùn)行playbook
ansible-playbook facts.yml
查看緩存目錄
Paste_Image.png
上述文件中存儲(chǔ)著json序列化的facts數(shù)據(jù)
Facts 使用redis作為緩存
安裝redis
yum -y install redis-server
easy_install pip
pip install redis
配置redis,使用密碼登陸
#vim /etc/redis.conf
requirepass "admin"
啟動(dòng)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
運(yùn)行playbook
ansible-playbook facts.yml
查看redis內(nèi)容
Paste_Image.png
Facts 使用memcached作為緩存
安裝 memcached
yum install memcached
easy_install pip
pip install python-memcached
啟動(dòng)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內(nèi)容
Paste_Image.png
更多文章請(qǐng)看 Ansible 專題文章總覽