Configuration of Target Node by using Variable file having same name as the OS in Ansible

Objective

Project Understanding

Part 1 : Ansible Playbook (main.yml)

- hosts: target_os
vars_files:
- "{{ ansible_facts['distribution'] }}.yml"
tasks:
- name: Webserver Installation
package:
name: "{{ webserver_package_name }}"
state: present
- name: Placing Web Page in Document Root
template:
dest: "/var/www/html/index.html"
src: "/t14_3/web_page/index.html"
- name: Enabling Webserver service
service:
name: "{{ service }}"
state: started
enabled: yes

Important Points

  • The name of variable file is updated dynamically as per the target node’s OS and which makes it easier to configure webserver as per OS. It internally uses the facts associated with a particular target node.
  • Two OS are considered over here, one is RedHat 8 and the other one is Ubuntu 20.04. The software for setting up webserver has different name in both OS i.e., httpd in RedHat 8 and apache2 in Ubuntu.

Part 2 : Variable Files

webserver_package_name: httpd
service: httpd
  • Ubuntu.yml
webserver_package_name: apache2
service: apache2

Part 3 : Web Page

Hello, this is {{ ansible_facts['distribution'] }}

Note:

  • Also, in the inventory file, general user’s credential needs to be specified for Ansible to access the OS via SSH in case of Ubuntu.
  • Software can’t be installed in Ubuntu OS without root power. In order to solve this issue an option could be provided with the ansible-playbook command as mentioned below:
ansible-playbook main.yml --ask-become-pass
  • The above command works if the sudo password for both the OS is same.

Output

Web Page Output in RedHat 8
Web Page Output in Ubuntu 20.04

RHEL8 | AWS | Ansible | Docker| K8s | Jenkins