Ansible

Manage different values of variable against different host in ansible

In this blog we will explore how we can use the different values of variable against different host in ansible

If you want to see the video for this article, click here

Problem Statement:

  • Two servers who have different username and password to connect with

Solution:

In your hosts file you can pass the variables and the different values as shown below:

  • We have two hosts which we named as application-0 and application-1
  • In your inventory/host file like dev/hosts, we will put details like this 
application-0 ansible_host=1.1.1.1 ansible_user="{{ app0_user}}" ansible_sudo_pass="{{ app0_pass}}" ansible_password="{{ app0_pass}}"
application-1 ansible_host=12.1.1.1 ansible_user="{{ app1_username}}" ansible_sudo_pass="{{ app1_pass}}" ansible_password="{{ app1_pass}}"

How will you pass the variable

Below is the example of how you can run the playbook using all the variables

'ansible-playbook main.yml -i inventories/${ENV}/hosts  -e "app0_user=abc" -e "app0_pass=password" -e "app1_user=abc" -e "app1_pass=password" 

Please follow and like us: