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


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


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
- "{{ ansible_facts['distribution'] }}.yml"
- name: Webserver Installation
name: "{{ webserver_package_name }}"
state: present
- name: Placing Web Page in Document Root
dest: "/var/www/html/index.html"
src: "/t14_3/web_page/index.html"
- name: Enabling Webserver 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.

Part 2 : Variable Files

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

Part 3 : Web Page


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


  • 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.
ansible-playbook main.yml --ask-become-pass
  • The above command works if the sudo password for both the OS is same.


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