Configure web server on different OS distribution with Ansible
Agenda
👉 Create an Ansible Playbook which will dynamically load the variable file named the 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.)
For this task, we can use the vars_files module. we will create the var files with <OS-distribution>-<version>.yml. And the name will be changed according to the OS name and version from the ansible facts with jinja2 format
For this exercise, I have two different OS installed on Oracle VM Box. The first is RedHat Enterprise Linux 8 and the second is Ubuntu 20. Both Linux has a different package for the same task that's why we need to create two separate files. Here is those files:
RedHat-8.yml
software: httpd
Ubuntu-20.yml
software: apache2
And the final YAML code for the webserver configuration.
web-config.yml
- hosts: webserver
vars_files:
- "{{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_major_version'] }}.yml"
tasks:
- name: Installing {{ software }}
package:
name: "{{ software }}"
state: present- name: Writing the content
copy:
dest: /var/www/html/index.html
content: "Automation with the Ansible on {{ ansible_facts['distribution'] }}"- name: Starting the service
service:
name: "{{ software }}"
state: started
Structure of the entire exercise
ansible.cfg
inventory
Final output
Task successfully performed…