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

Satyam Singh
2 min readJul 13, 2021

Objective

Configure Target Node by using the variable file of the same name as that of the Target Node’s OS name

Content of this Blog

  • Prerequisite
  • Project Understanding
  • Output

Prerequisite

For Ansible, refer the following link:

Project Understanding

Let’s understand the implementation part by part

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

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

Part 3 : Web Page

index.html

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

Note:

  • Unlike RedHat 8, root account can’t be logged into separately in case of Ubuntu OS. In order to use root power in Ubuntu OS, sudo password needs to be provided.
  • 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

--

--