Custom facts (local facts) are the variables which are declared on ansible managed host. Custom facts are declared in ini or json file in the /etc/ansible/facts.d directory on managed host. File names of custom facts must have .fact extension.
In this article, we will cover how to create and use custom facts to install samba file server and starts its service on ansible managed host. Here we are using host1 and host2 as a part of fileservers group in the inventory.
To demonstrate custom facts, following is my lab setup
- control.example.com — 10.20.0.57
- host1.example.com — 10.20.0.10 // Ansible Managed Host
- host3.example.com — 10.20.0.30 // Ansible Managed Hosts
Note : devops user is configured on ansible control and managed hosts with sudo rights. Inventory and ansible.cfg file is defined under /home/develops/install directory. Content of my inventory are shown below:
[devops@control install]$ cat inventory [fileservers] host1.example.com host3.example.com [dbservers] host2.example.com host1.example.com [devops@control install]$
Logical steps to declare and use custom local facts are
- Create a facts file on ansible control host with .fact extension
- Create one play in the playbook to create a folder ‘/etc/ansible/facts.d’ and copy the facts file on managed hosts on this folder.
- Create 2nd play in the playbook which will use these custom facts using ansible_local.<facts-filename>.<fact-name>.<variable-name> to install samba server and start its service.
Let’s dive into the actual implementation of custom or local facts.
Step 1) Create custom facts file on control node
Let’s create customfacts.fact file with the following contents
[devops@control install]$ cat customfacts.fact [localfacts] pkgname = samba srvc = smb [devops@control install]$
Here localfacts is factname and pkgname & srvc are variables.
Step 2) Create a playbook with two different plays
Create customfacts-install.yaml playbook with following contents
[devops@control install]$ vi customfacts-install.yaml --- - name: Install custom facts hosts: fileservers vars: remote_dir: /etc/ansible/facts.d facts_file: customfacts.fact tasks: - name: Create Facts Dir on Managed Hosts file: path: "{{ remote_dir }}" state: directory recurse: yes - name: Copy Contents to Facts file copy: src: "{{ facts_file }}" dest: "{{ remote_dir }}" - name: Install Samba Server with Custom Facts hosts: fileservers tasks: - name: Install SMB package: name: "{{ ansible_local.customfacts.localfacts.pkgname }}" state: present - name: Start SMB Service service: name: "{{ ansible_local.customfacts.localfacts.srvc }}" state: started enabled: yes
save and exit the file.
Step 3) Run the playbook on fileservers
We will execute the playbook on fileservers, before running it, let’s verify the connectivity from control node towards these nodes.
[devops@control install]$ ansible fileservers -m ping
Above confirms that ping pong is working fine, so let’s run the ansible playbook using beneath command,
[devops@control install]$ ansible-playbook customfacts-install.yaml
Above output shows that playbook has been executed successfully. Let’s verify the installation of custom facts and samba service.
Step 5) Verify Custom local facts and Samba Service
Run below ansible ad-hoc command to verify custom facts installation,
[devops@control install]$ ansible fileservers -m setup -a "filter=ansible_local"
Verify samba server’s service status by executing below:
[devops@control install]$ ansible fileservers -m command -a "systemctl status smb"
Perfect, above output confirms that Samba has been installed successfully and its service is up and running.
That’s all from this article, I hope you get the basic idea about custom facts installation and its usage.
Read Also : How to Use Handlers in Ansible Playbook