Dynamic Variable File In Ansible

Hey guys,

I am back with another blog, In this blog I am going to tell you how you can load different different variable file in a single ansible playbook according to your need.

In my case, I want to load two different different variable files named “RedHat.yml” and “CentOS.yml” in a single playbook as I want to configure webserver in both systems.

Without wasting time, let’s get started..

  • create a separate workspace.
  • you can clone my repository here.

git clone <link>

  • In my case I have two Instances, one is RedHat in my local virtual Box while other one is CentOS inside Docker.
  • My inventory is hosts file.
File Structure
  • I have created two separate variable files for both of them with the exact name of their Distribution name. (“RedHat.yml” and “CentOS.yml” )
  • At the place of vars_files I have passed the same variable name from ansible_facts .
  • Now it’s going to run the playbook.
  • In my case one of the instance is inside docker so it’s going to run different command for that to start webserver.

If you don’t know how to connect to Docker Container with Ansible Dynamically you can go to one of my post.

  • At the end you can run main.yml playbook file, your output should look something like this.
  • Now you can check on browser with your IP’s .
RedHat webserver
CentOS webserver

I hope you like this blog, For next blog topic suggestions or queries you can DM me on LinkedIn. If you like this blog make sure you clap it.

Thank You

Signing off 🙌

Passinate Technology Lover …