无法识别组变量 [英] Cannot get ansible to recognize group variables

查看:27
本文介绍了无法识别组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 ansible 中设置特定于环境的变量(例如生产、登台、开发).出于某种原因,ansible 没有在 group_vars/[environment] 中提取变量.

我使用的是 ansible 1.9.1

这是我正在尝试做的一个精简的例子.

目录结构:

<预><代码>.├── group_vars│ └── 舞台├── 主机│ └── 舞台└── 网站.yml

group_vars/分期:

test_var: "这是一个测试"

site.yml:

---- 主机:本地主机任务:- 调试:味精=测试变量= {{ test_var }}"

hosts/staging 是一个空文件

运行 ansible-playbook -i hosts/staging site.yml 的输出:

PLAY [localhost] **************************************************************收集事实 *******************************************************************好的:[本地主机]任务:[debug msg="test variable = {{ test_var }}"] ****************************致命的:[本地主机] =>一个或多个未定义变量:'test_var' 未定义致命:所有主机都已经失败——正在中止播放回顾 ************************************************************************要重试,请使用:--limit @/Users/jcowley/site.retry本地主机:ok=1 更改=0 无法访问=1 失败=0

如果我将 group_vars/staging 移动到 group_vars/all,它会按预期工作并输出 test_var 的值.但我试图了解如何根据 Ansible 的 最佳做法

解决方案

要更具体地回答您的问题,请查看这个 github 示例 项目.我想,这就是你试图做的.我可能错了,但我认为问题来自您的库存文件.您实际上以库存文件名(staging)命名了您的 group_vars 文件(staging).但是,您必须在清单文件中的 部分 之后命名它,我想也就是 localhost 查看您的剧本.

因此,这是您应该拥有的:

主机/暂存:

[暂存]X.X.X.X

据我所知,这是组织项目的更可行的解决方案.它基于角色.

目录结构:

<预><代码>.├── group_vars│ └── 全部├── 主机│ └── 本地│ └── 舞台│ └── 产品├── 角色│ └── 例子│ └── 任务│ └── 变种│ └── local.yml│ └── staging.yml│ └── prod.yml└── 网站.yml

group_vars/all 可以有一个环境变量:

#应用环境# 可能的值是:prod、staging 或 local环境:本地# 其他全局变量...

您的库存文件:

[本地]X.X.X.X[分期]X.X.X.X[产品]X.X.X.X

然后,您的剧本 sites.yml 可能如下所示:

---- 名称:服务器配置主机:{{env}}"角色:- 例子vars_files:- "roles/example/vars/{{env}}.yml"

这样做会给您带来多重好处:

  • 您可以在项目的任何位置、jinja 模板中或作为非常实用的任务中的条件重用 env 变量;
  • 您的项目分为不同的角色.这种方式对于大项目更干净(你可以拥有 apache 角色、ssh 角色等);
  • 您可以在 roles/example/vars/ 目录中的单独文件中创建特定于环境的变量.

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].

I'm using ansible 1.9.1

Here's a stripped down example of what I'm trying to do.

Directory structure:

.
├── group_vars
│   └── staging
├── hosts
│   └── staging
└── site.yml

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

Output of running 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

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

解决方案

EDIT:

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:

hosts/staging:

[staging]
X.X.X.X

Here is, according to me, a more viable solution to organize your project. It is based on roles.

Directory structure:

.
├── group_vars
│   └── all
├── hosts
│   └── local
│   └── staging
│   └── prod
├── roles
│   └── exemple
│       └── tasks
│       └── vars
│           └── local.yml
│           └── staging.yml
│           └── prod.yml
└── site.yml

The group_vars/all could have an env variable:

# The application environment
# Possible values are : prod, staging or local
env: local

# Other global variables
...

Your inventory file:

[local]
X.X.X.X

[staging]
X.X.X.X

[prod]
X.X.X.X

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:

  • 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.

这篇关于无法识别组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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