Low Orbit Flux Logo 2 F

Ansible - Facts

Facts:

Show all facts available:



- debug: var=ansible_facts

See raw gathered info:



ansible hostname -m setup

Hostname as reported by the system:




{{ ansible_facts['nodename'] }}


Disable facts gathering ( probably faster ):



- hosts: whatever
  gather_facts: no
  tasks:
    ....

Some common facts:



  tasks:
    - debug: var=ansible_facts['distribution']
    - debug: var=ansible_facts['distribution_major_version']
    - debug: var=ansible_facts['os_family']
    - debug: var=ansible_facts['all_ipv4_addresses']
    - debug: var=ansible_facts['default_ipv4']
    - debug: var=ansible_facts['env']
    - debug: var=ansible_facts['hostname']
    - debug: var=ansible_facts['interfaces']
    - debug: var=ansible_facts['kernel']

Facts from Other Hosts

Access fact from other host:




{{ hostvars['asdf.example.com']['ansible_facts']['os_family'] }}


For this to work, need to:

Example using jsonfile:



ansible.cfg

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /path/to/cachedir
fact_caching_timeout = 86400
# seconds

Custom Defined - facts.d

Facts can also be supplied by files that:

Example hash variable:



/etc/ansible/facts.d/preferences.fact
[general]
asdf=1
bar=2

Check it:




ansible host1 -m setup -a "filter=ansible_local"


"ansible_local": {
        "preferences": {
            "general": {
                "asdf" : "1",
                "bar"  : "2"
            }
        }
 }


Use stuff from facts.d like this:




{{ ansible_local['preferences']['general']['asdf'] }}