问题描述
我正在尝试在ansible中设置特定于环境的变量(例如,生产,暂存,开发).由于某种原因,ansible不会在group_vars/[environment]中拾取变量.
I'm trying to set up environment specific variables in ansible (e.g. production, staging, development). For some reason, ansible is not picking up variables in group_vars/[environment].
我正在使用Ansible 1.9.1
I'm using ansible 1.9.1
这是我要做的事情的精简示例.
Here's a stripped down example of what I'm trying to do.
目录结构:
.
├── group_vars
│ └── staging
├── hosts
│ └── staging
└── site.yml
group_vars/分段:
group_vars/staging:
test_var: "this is a test"
site.yml:
---
- hosts: localhost
tasks:
- debug: msg="test variable = {{ test_var }}"
主机/分段是一个空文件
hosts/staging is an empty file
正在运行的ansible-playbook -i hosts/staging site.yml
的输出:
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [debug msg="test variable = {{ test_var }}"] ****************************
fatal: [localhost] => One or more undefined variables: 'test_var' is undefined
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/Users/jcowley/site.retry
localhost : ok=1 changed=0 unreachable=1 failed=0
如果我将group_vars/staging移到group_vars/all,它将按预期工作并输出test_var的值.但是我试图了解如何根据Ansible的最佳做法
If I move group_vars/staging to group_vars/all, it works as expected and outputs the value of test_var. But I'm trying to understand how I can separate environments per the documentation in Ansible's Best Practices
推荐答案
要更具体地回答您的问题,请查看此github示例项目.我认为这就是您尝试做的.我可能是错的,但我认为问题出在您的库存文件中.您实际上是在清单文件名(也为staging
)之后命名group_vars
文件(staging
)的.但是,您必须在清单文件中的部分后面为其命名,我想是localhost
在查看您的剧本.
To answer your question more specifically, please have a look at this github exemple project. This is, I think, what you tried to do.I might be wrong, but I think the problem comes from your inventory file. You actually named your group_vars
files (staging
) after your inventory filename (staging
too).But, you must name it after the section inside your inventory file, which is, I suppose, localhost
looking at your playbook.
因此,这就是您应该拥有的:
Thus, this is what you should have:
主机/分段:
[staging]
X.X.X.X
在我看来,这是组织项目的更可行的解决方案.它基于角色.
Here is, according to me, a more viable solution to organize your project. It is based on roles.
目录结构:
.
├── group_vars
│ └── all
├── hosts
│ └── local
│ └── staging
│ └── prod
├── roles
│ └── exemple
│ └── tasks
│ └── vars
│ └── local.yml
│ └── staging.yml
│ └── prod.yml
└── site.yml
group_vars/all
可以具有一个env变量:
The group_vars/all
could have an env variable:
# The application environment
# Possible values are : prod, staging or local
env: local
# Other global variables
...
您的库存文件:
[local]
X.X.X.X
[staging]
X.X.X.X
[prod]
X.X.X.X
然后,您的剧本sites.yml
可能看起来像这样:
Then, your playbook sites.yml
could look like this:
---
- name: Server(s) configuration
hosts: "{{env}}"
roles:
- exemple
vars_files:
- "roles/example/vars/{{env}}.yml"
通过这种方式可以为您带来多种好处:
Doing it this way gives you multiple benefits:
- 您可以在项目中的任何地方,jinja模板中或在非常实用的任务中作为条件重复使用env变量;
- 您的项目分为不同的角色.对于大型项目,这种方式更干净(您可以具有apache角色,ssh角色等);
- 您可以在
roles/exemple/vars/
目录的单独文件中创建特定于环境的变量.
- you can reuse the env variable anywhere in your project, in jinja templates or as a condition in tasks which is very practical;
- your project is split into separate roles. It’s cleaner this way for big project (you can have an apache role, ssh role, etc.);
- you can create env-specific variables in separate files in
roles/exemple/vars/
directory.
这篇关于无法识别组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!