Ansible Dynamically Load Operating System Specific Variable File

Rohit Raut
3 min readJan 12, 2021

--

From Google

In this article, you will see how we can create a playbook using facts that will dynamically load os specific variable file in the playbook for configuring the Apache webserver without using the “when” keyword.

Ansible Facts

Ansible Facts are information gathered by ansible before configuring any managed node. With facts, we can use the behavior or state of one system as configuration on other systems.

Ansible Directory Structure.

The below picture shows how I have managed the things that we required. It is good practice to create a separate folder for the same type of thing.

Directory

For this demo, we required Virtual Machines with different Operating System image, so I used the AWS cloud to launch managed Nodes.

AWS EC2 Dashboard

Next thing is to configure the inventory file. To do this you have to write down the IP address and user-name in the inventory file. As both instances have the same key to log-in so instead of declaring a key for every instance I declared it in the ansible conf file. if you different key for instance you can refer to this ansible inventory documentation.

Inventory File

As these users are not root users and to configure we need root privileges. for this, we need to use the privilege_escalation block in the conf file. It internally uses the sudo[by default] method to configure the managed node.

Ansible Configuration File

After creating the inventory file, we can check the managed node’s facts using the below command.

ansible -m setup managedNodeIpAddress
RedHat Facts
Ubuntu Facts

The next step is to create files with these values of ansible fact called ansible_distribution and set variables that you think you need while creating the playbook. As I require to configure the Apache webserver in the managed node I have created some variables as shown below.

Variable File

The last step is to create a playbook and run the playbook. if you look at the vars_file key word, I have used facts called distribution to import specific os file.

Playbook
Output

Congratulation, we have successfully created a playbook that dynamically uses the operating system specific variable file.

GitHub Link: https://github.com/rohitraut3366/Ansible-Dynamically-Load-OS-specific-File.git

That's all for this article, Hope You learned Something from here.

Thanks, everyone for reading.

--

--

No responses yet