0
votes

I'm trying to figure out what is the best strategy to use custom variables on my ansible-playbooks/ansible-roles. What I'm doing is at this time is:


Customer1:

  • Create a new inventory file with Customer1 devices on inventories/customer1.ini
  • Overwrite customer variables on vars/controls.yml
  • Execute my ansible-playbook/ansible-role for Customer1 ansible-playbook -i inventories/customer1.ini site.yml

Customer2:

  • Create a new inventory file with Customer2 devices on inventories/customer2.ini
  • Overwrite customer variables on vars/controls.yml
  • Execute my ansible-playbook/ansible-role for Customer2 ansible-playbook -i inventories/customer2.ini site.yml

Customer N:


What I want to do, is just create a different variables controls vars/controls.yml per customer: vars/controls-customer1.yml vars/controls-customer2.yml

And that my ansible-playbook/ansible-role is able to read it without any change (reusability).

I hope you can give me some light on this.

Thank you!

1
Create your inventory in inventories/customerX/hosts.ini and put your customer vars in inventories/customerX/group_vars/all.yml. You will not have to overwrite anything and vars will be loaded automagically for any hosts in your inventory.Zeitounator
Thanks @Zeitounator!! In this case should I edit my ‘ansible.cfg’ file or my ‘host.ini’ file to let Ansible know where to look for the variables?Luis Cacho
No: using -i inventories/customerX option will be enough. docs.ansible.com/ansible/latest/user_guide/…Zeitounator

1 Answers

0
votes

You can also put the following into you ansible.cfg for this/these playbooks:

[defaults]
inventory = ./inventories/

Which will then load all "inventory"-files inside directory "inventories". You don't have to use the -i option on the command line if you don't specificly want this one specific inventory to be used.