Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set ansible_config variables from user defined ansible.cfg

Tags:

ansible

I have a directory structure under which I am keeping my playbooks like follow:

/home/monk/
    |_____Ansible_work
               |_____[ansible.cfg]
               |_____[playbook_dir_1]
               |                  |_______playbook_1.yml
               |_____[playbook_dir_2]
               |                  |_______playbook_2.yml
               |_____[playbook_dir_3]
               |                  |_______playbook_3.yml
               |_____[playbook_dir_4]
               |                  |_______playbook_4.yml
               |_____[inventory]
                     |___[inventory_1]
                     |___[inventory_2]

I am currently executing my playbooks like below from ansible_work dir:

ansible-playbook -i inventory/inventory_1 playbook_dir_1/playbook_1.yml

or

ansible-playbook -i inventory/inventory_1 playbook_dir_1/playbook_2.yml

or

ansible-playbook -i inventory/inventory_2 playbook_dir_1/playbook_1.yml

Each playbook need to refer to some of the variables set in ansible.cfg like log_path ,role_path etc. Everything works fine.

Now I have been told that, I should make the configuration flexible enough such that these playbooks can be executed from any location when given full path of inventory and playbook. (which seems fair requirement) But as my playbooks refer local copy of ansible.cfg , the content set within this local ansible.cfg will not be set if I trigger executin from any point apart from /home/monk/ansible_work/ dir.

By default Ansible.cfg lookup priority is as follow:

* ANSIBLE_CONFIG (an environment variable)
* ansible.cfg (in the current directory)                 <--------------I am currently using this
* .ansible.cfg (in the home directory)                   <--------------Cannot use this as multiple users will be running the PB
* /etc/ansible/ansible.cfg                               <--------------Cannot set this as , we do not have admin rights. 

Question:

How to make playbooks independent of ansible.cfg and still set the configuration like DEFAULT_LOG_PATH ,DEFAULT_ROLES_PATH etc. ?

OR

How to make playbook executable from any directory but still it keep sourcing a user created ansible.cfg ? which is not present in the three default locations.

like image 305
monk Avatar asked Nov 02 '25 04:11

monk


1 Answers

one way to execute your playbooks from any directory, and still refer the file ansible.cfg you defined is to fully define the paths, using an extra var ANS_WORK will be shorter, and use ANSIBLE_CONFIG var to point to your ansible.cfg:

ANS_WORK=/home/monk/ansible_work \
ANSIBLE_CONFIG=${ANS_WORK}/ansible.cfg \
ansible-playbook \
-i ${ANS_WORK}/inventory/inventory_2 \
${ANS_WORK}/playbook_dir_1/playbook_1.yml

another way any user can use your ansible architecture is by having common git workspace that anybody clones in their own home dir for example (that's what we do here). Advantage of it : any one can use git branches independantly, not relying on one common workspace that cannot be in several branches at the same time.

like image 62
tassinp Avatar answered Nov 04 '25 07:11

tassinp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!