how to set ansible_config variables from user defined ansible.cfg

谁说我不能喝 提交于 2019-12-24 10:46:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/56691804/how-to-set-ansible-config-variables-from-user-defined-ansible-cfg

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!