Ansible playbook to dynamically load the variable file named same as OS_name
The following task has to be performed using Ansible:
🔰 Create an Ansible Playbook which will dynamically load the variable file named same as OS_name and just by using the variable names we can Configure our target node.
(Note: No need to use when keyword here.)
To perform this task we will take the help of ansible_facts.
What are Ansible Facts?
Ansible facts are data related to your remote systems, including operating systems, IP addresses, attached filesystems, and more. You can access this data in the ansible_facts variable. By default, you can also access some Ansible facts as top-level variables with the ansible_ prefix. You can disable this behavior using the INJECT_FACTS_AS_VARS setting.
- To start the task I have already launched two instances in AWS. (You can use local system also). The AWS private key file must be uploaded in controller node.
2. Create inventory file
3. Edit the ansible.cfg file. Go to vi /etc/ansible/ansible.cfg
4. Check hosts list and connectivity
ansible --list-hosts all
5. Create main.yml file and write the playbook.
6. Now create two variable files:
a. RedHat-8.yml
package_name: httpd service_name: httpd src_file: redhat.html
b. Ubuntu-20.yml
package_name: apache2 service_name: apache2 src_file: ubuntu.html
7. Create two html files:
a. redhat.html
RedHat Server!!
b. ubuntu.html
Ubuntu Server!!
8. Run the playbook main.yml
OUTPUT: