ansible 内置facts

ansible 内置facts

ansible 内置facts

最近在写一个mysql的role, 想要自动根据机器内存大小设置数据库内存大小,只需要从facts里面取到物理内存进行判断就可以了。

如何获得内置facts

ansible somehost -m setup
somehoste | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"xxx.xxx.xxx.xxx"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "07/09/2012",
"ansible_bios_version": "6.00",

需要的是这条:

"ansible_memtotal_mb": 7873

将内置facts放到redis里面去

还有个简便办法,用redis来存facts, 存下的facts还可以作为CMDB的数据源,何乐而不为呢。

编辑配置文件

/etc/ansible/ansible.cfg

找到fact_caching,设置上一个redis就行了。

# if set to a persistent type (not 'memory', for example 'redis') fact values
# from previous runs in Ansible will be stored. This may be useful when
# wanting to use, for example, IP information from one group of servers
# without having to talk to them in the same playbook run to get their
# current IP information.
#fact_caching = memory
fact_caching = redis
fact_caching_connection = 127.0.0.1:7930
fact_caching_pass = wobugaosuni

ansible会自动设置key的ttl,因此不用担心redis被撑爆,每次运行都会更新facts。
当然playbook没有指定不收集facts才可以。下面这样就不会收集facts了

- hosts: whatever
gather_facts: no