Ansible Custom facts
Using Ansible for system automation provides several powerful features, one being Ansible facts.
what are ansible facts?
Ansible facts are system properties that are collected by Ansible when it executes on a remote system. The facts contain useful details such as storage and network configuration about a target system. They may be outputted to a file as a type of system report, or they may be used during Ansible playbook execution to make runtime decisions.
ansible localhost -m setup
Basically, Ansible ad-hoc syntax providing the setup module using the -m flag. A large number of facts are output by default.
print the specific fact only, using the filter parameter
ansible localhost -m setup -a "filter=*ipv4"
Here -a flag to provide the filter parameter which takes a simple pattern-match expression.
Whenever you run an Ansible playbook, the first thing that happens is the setup task. This task gathers a whole host of facts about the remote machine's IP addresses, disks, OS version, etc... Ansible refers to these as “facts”.
There are two types of custom facts: data and code. Data is just read from a file, and code is executed and its output is accepted as data.
Both facts lives in /etc/ansible/facts.d/ directory on the target machine not a controller, both have extension of .fact (example: /etc/ansible/facts.d/date.fact). If this file has execution permission, it’s a code, if it does not have execution permission, it’s data. For both output of the code and the content of the data fact, the format is the same: a JSON. It will be stored under key with fact name (file name before ‘.fact’) in ansible_local after fact gathering.
Setting up custom facts automatically
You probably don’t want to manually place your *.fact files on each remote machine. Since we’re using Ansible anyway, it makes sense to create a playbook that creates our *.fact files for us.
This module is automatically called by playbooks to gather useful variables about remote hosts that can be used in playbooks. It can also be executed directly by /usr/bin/ansible to check what variables are available to a host. Ansible provides many facts about the system, automatically.
gather facts:
When we execute an Ansible playbook by default it will gather facts of the operating system first and then execute tasks listed in it.
It's always not required to gather facts & if we have too many hosts it will take more time to execute a playbook.
To mitigate this we need to disable gather facts with the "gather_facts" attribute in ansible-playbook.
By default gather_facts attributes value is True, to disable it we need to set it as False.
gather_facts: False
---
- hosts: all
gather_facts: False
tasks:
- name: Zippyops
shell: "echo zippyops"
tags:
- zippyops
- name: Automation
shell: "echo automation"
tags:
- automation
Ex: if you're using a centos6 machine as a node,
---
- hosts: all
become: true
tasks:
- name: Create fact directory
file:
path: /etc/ansible/facts.d/
state: directory
- name: "Transfering custom fact files for centos6"
template:
src: "{{ item }}"
dest: /etc/ansible/facts.d/
mode: 0755
with_fileglob:
- ../templates/audit/centos6/*.fact
tags:
- centos
- centos6
Relevant Blogs:
Recent Comments
No comments
Leave a Comment
We will be happy to hear what you think about this post