[Ansible] – Understanding Ansible facts

What are Ansible Facts?

Ansible facts are variables that automatically are obtained for example the hostname, the network interfaces, the version of the operating system and the number of CPUs.

How to the get the ansible facts?

Following the command lines that can be used.

$ ansible <host> -m setup

Following the command to filter the result.

$ ansible <host> -m setup -a ‘filter=EXPRESSION’

How to customize ansible facts? 

The facts are in the path /etc/ansible/facts.d by default. To add customization is required to create files in this dir with the extension .fact. ( E.g custom.fact ) These custom files need to be in the key-par or JSON format. Following an example.

# custom.fact file
service = httpd
enabled = true
# File setup_facts.yml as example of this configuration.
# These values will be returned when the command "ansible myserver.example.com -m setup -a 'filter=ansible_local'" be executed
# Use ansible to run this script "ansible-playbook setup_facts.yml"
- name: Script to setup the custom facts
  hosts: myserver.example.com
    path: /etc/ansible/facts.d
    file: custom.fact
    - name: Create the remote directory {{ path }}
        state: directory
        recurse: yes
        path: "{{ path }}"
    - name: Copy the {{ file }} to {{ path }}
        src: "{{ file }}"
        dest: "{{ path }}"

For further information, check Gathers facts about remote hosts and Local Facts (Facts.d).



1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s