如何从用户定义的ansible.cfg中设置ansible_config变量 [英] how to set ansible_config variables from user defined ansible.cfg

查看:423
本文介绍了如何从用户定义的ansible.cfg中设置ansible_config变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录结构,可在其中保留我的剧本,如下所示:

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]

我目前正在从ansible_work目录执行我的剧本,如下所示:

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

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

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

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

每个剧本都需要引用ansible.cfg中设置的某些变量,例如log_pathrole_path等.一切正常.

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

现在,我被告知,我应该使配置足够灵活,以便在给定完整清单和剧本路径的情况下,可以从任何位置执行这些剧本. (这似乎是公平的要求) 但是由于我的剧本引用了ansible.cfg的本地副本,因此如果我从/home/monk/ansible_work/ dir以外的任何地方触发executin,都不会设置在该本地ansible.cfg中设置的内容.

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.

默认情况下,Ansible.cfg查找优先级如下:

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. 

问题:

如何使剧本独立于ansible.cfg,并仍然设置DEFAULT_LOG_PATHDEFAULT_ROLES_PATH等配置?

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

如何使Playbook可从任何目录执行,但仍会继续寻找用户创建的ansible.cfg?这在三个默认位置中不存在.

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.

推荐答案

从任何目录执行剧本并仍然引用您定义的ansible.cfg文件的一种方法是使用额外的var ANS_WORK完全定义路径.短一点,并使用ANSIBLE_CONFIG var指向您的ansible.cfg:

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

任何用户都可以使用您的ansible架构的另一种方式是通过拥有通用的git工作区,例如任何人都可以在其自己的主目录中克隆(这就是我们在这里所做的).它的优点:任何人都可以独立使用git分支,而不必依赖不能同时存在于多个分支中的一个公共工作空间.

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.

这篇关于如何从用户定义的ansible.cfg中设置ansible_config变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆