Ansible Playbook — Target Node Configuration Using Variable File / Name

Tilwani Hardik
3 min readJun 17, 2022
Source : Owner

Hello there, in this article, we will create an ansible Playbook which will dynamically load the variable file named the same as OS_name & just by using the variable names we can configure our target node.

Note: No need to use “when” keyword here

Before starting to deploy such system we will learn about the term known as “Ansible Facts”.

What is Ansible Facts ???

  • Ansible facts are data gathered about target nodes (host nodes to be configured) and returned back to controller nodes. Ansible facts are stored in JSON format and are used to make important decisions about tasks based on their statistics. Facts are in an “ ansible_facts ” variable, which is managed by Ansible Engine.
  • Ansible facts make it easier for sysadmins to control under what circumstances playbooks will run based on actual system information.
  • You can also refer this documentation :

https://www.redhat.com/sysadmin/playing-ansible-facts

Tasks will be done step by step as followed :

Step 1 : Launching 3 Instances In AWS -

Step 2 : Create inventory file in our Controller node -

vim ip.txt
ip.txt

Step 3 : Install Ansible in our Controller node -

sudo amazon-linux-extras install ansible2

Step 4 : Create an Ansible configuration file in our Controller node -

vim ansible.cfg
ansible.cfg

Step 5 : To check the list of hosts -

ansible all --list-hosts

Step 6 : To ping our hosts -

ansible all -m ping

Step 7 : Creating the variable file in our controller node —

Step 7.1 : For RedHat OS —

vim RedHat.yml

Step 7.2 : For Ubuntu OS —

vim Ubuntu.yml

Step 8 : Creating a playbook to configure webserver -

vim task14.3.yml
14.3.yml

Step 9 : Creating a Html file as “ web.html ” -

vim web.html

Step 10 : Execute our Ansible playbook using following command -

ansible-playbook task14.3.yml

Step 11 : Our Output on target nodes —

  • Finally we created the Ansible playbook which dynamically loads the variable file named the same as OS_name & just by using the variable names we can configure our target nodes.

Thank You for reading & learning with me.

--

--