Ansible Dynamically Load Operating System Specific Variable File
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.
For this demo, we required Virtual Machines with different Operating System image, so I used the AWS cloud to launch managed Nodes.
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.
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.
After creating the inventory file, we can check the managed node’s facts using the below command.
ansible -m setup managedNodeIpAddress
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.
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.
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.