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

查看:23
本文介绍了如何从用户定义的 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/ 目录之外的任何点触发执行,则不会设置此本地 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. 

问题:

如何使 playbooks 独立于 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.

推荐答案

从任何目录执行你的 playbooks 并且仍然引用你定义的文件 ansible.cfg 的一种方法是完全定义路径,使用额外的 var ANS_WORK 将更短,并使用 ANSIBLE_CONFIG 变量指向您的 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天全站免登陆