如果不手动加载,Ansible不会选择group_vars [英] Ansible doesn't pick up group_vars without loading it manually
问题描述
在我的local.yml
中,我可以在group_vars/all
中运行剧本和引用变量,但是我不能在group_vars/phl-stage
中访问变量.让我们假设以下内容.
In my local.yml
I'm able to run the playbook and reference variables within group_vars/all
however I'm not able to access variables within group_vars/phl-stage
. Let's assume the following.
ansible-playbook -i phl-stage site.yml
我有一个变量,我们称其为 deploy_path
,每个环境都不同.我将变量放在group_vars/< environment name >
中.如果我将文件group_vars/phl-stage
包含在vars_files
中,它可以工作,但我会认为该组文件会自动加载?
I have a variable, let's call it deploy_path
that's different for each environment. I place the variable within group_vars/< environment name >
. If I include the file group_vars/phl-stage
within vars_files
it works but I would've thought the group file would be automatically loaded?
site.yml
- include: local.yml
local.yml
local.yml
- hosts: 127.0.0.1
connection: local
vars_files:
- "group_vars/perlservers"
- "group_vars/deploy_list"
group_vars/phl-stage
group_vars/phl-stage
[webservers]
phl-web1
phl-web2
[perlservers]
phl-perl1
phl-perl2
[phl-stage:children]
webservers
perlservers
目录结构:
group_vars
all
phl-stage
phl-prod
site.yml
local.yml
推荐答案
您对该结构有些困惑.
-
group_vars
目录包含清单文件中定义的每个主机组的文件 .这些文件定义了成员主机可以使用的变量. - 库存文件不位于
group_vars
目录中,它应该位于外部. - 仅属于组成员的主机可以使用其变量,因此,除非您将127.0.0.1放入组中,否则它将无法使用
.
- The
group_vars
directory contains files for each hostgroup defined in your inventory file. The files define variables that member hosts can use. - The inventory file doesn't reside in the
group_vars
dir, it should be outside. - Only hosts that are members of a group can use its variables, so unless you put 127.0.0.1 in a group, it won't be able to use any group_vars beside those defined in
group_vars/all
.
您想要的是这个目录结构:
What you want is this dir structure:
group_vars/
all
perlservers
phl-stage
hosts
site.yml
local.yml
假设127.0.0.1只是一个登台服务器,而不是perl或Web服务器,您的主机文件应如下所示:
Your hosts file should look like this, assuming 127.0.0.1 is just a staging server and not perl or web server:
[webservers]
phl-web1
phl-web2
[perlservers]
phl-perl1
phl-perl2
[phl-stage]
127.0.0.1
[phl-stage:children]
webservers
perlservers
因此,您要定义哪些主机属于清单中的哪个组,然后为每个组在其group_vars文件中定义变量.
So you define which hosts belong to which group in the inventory, and then for each group you define variables in its group_vars file.
这篇关于如果不手动加载,Ansible不会选择group_vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!