Ansible playbook to dynamically load the variable file named same as OS_name

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.

  1. 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.
No alt text provided for this image

2. Create inventory file

No alt text provided for this image

3. Edit the ansible.cfg file. Go to vi /etc/ansible/ansible.cfg

No alt text provided for this image

4. Check hosts list and connectivity

ansible --list-hosts all

No alt text provided for this image

5. Create main.yml file and write the playbook.

No alt text provided for this image

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

No alt text provided for this image

OUTPUT:

No alt text provided for this image
No alt text provided for this image


To view or add a comment, sign in

More articles by Amit Gope

Insights from the community

Others also viewed

Explore topics