Ansible group_vars 未根据环境/主机列表加载 [英] Ansible group_vars not loading based on environment / host list

查看:27
本文介绍了Ansible group_vars 未根据环境/主机列表加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的剧本:

- 主机:{{env}}"名称:《Redis 手册》须藤:没有变量:产品:redis角色:- Redis

我用:ansible-playbook pb_redis.yml -i inventory/redis -e env=qa -v

我的目录结构如下:

<预><代码>.├── group_vars│ ├── qa│ │ ├── 通用│ │ |└── redis.yml│ │ ├──产品│ │ |└── abc-1.yml│ │ |└── xyz-2.yml│ ├──测试│ │ ├── 普通│ │ |└── redis.yml│ │ ├──产品│ │ |└── abc-1.yml│ │ |└── xyz-2.yml├── 库存└── 角色└── Redis├── 文件├── 处理程序├──元├── 任务├── 模板└── 变种

我有一个类似的库存:

[qa:children]qa_redis[qa_redis]mybox.1.spacemybox.2.spacemybox.3.space

我的问题:当我运行 ansible-playbook pb_redis.yml -i inventory/redis -e env=qa -v 时,我仍然在拿起 group_vars 定义在 ../test/common/redis.yml 而不是 ../qa/common/redis.yml -- 我是不是误解了这应该如何工作?正确的主机是从清单文件中提取的,但不是正确的group_vars.我应该将 redis.yml 放在 ../qa/products/ 下吗?

解决方案

Ansible 中的库存(主机和组)变量绑定到主机.为方便起见,存在组变量.

如果一个主机同时在多个组中,则所有组变量都应用于该主机.
如果不同的组有相同的变量,它们在库存加载过程中会相互覆盖.

因此,如果您在 qatest 组中有 mybox.1.space,则来自 qa 组的变量和 test 应用于此主机.

通常您希望使用单独的清单来处理不同的部署环境.并且组用于分隔库存中的不同逻辑单元.

I have a playbook like so:

- hosts: "{{env}}"
  name: "REDIS Playbook"
  sudo: no
  vars:
    product: redis
  roles:
    - redis

And I call it with: ansible-playbook pb_redis.yml -i inventory/redis -e env=qa -v

I have a directory structure like:

.
├── group_vars
│   ├── qa
│   │   ├── common
│   │   |    └── redis.yml
│   │   ├── products
│   │   |    └── abc-1.yml
│   │   |    └── xyz-2.yml
│   ├── test
│   │   ├── common
│   │   |    └── redis.yml
│   │   ├── products
│   │   |    └── abc-1.yml
│   │   |    └── xyz-2.yml
├── inventory
└── roles
    └── redis
        ├── files
        ├── handlers
        ├── meta
        ├── tasks
        ├── templates
        └── vars

And I have an inventory like:

[qa:children]
qa_redis

[qa_redis]
mybox.1.space
mybox.2.space
mybox.3.space

My Issue: When I run ansible-playbook pb_redis.yml -i inventory/redis -e env=qa -v, I'm still picking up group_vars defined in the ../test/common/redis.yml instead of ../qa/common/redis.yml -- am I misunderstanding how this should work? The correct hosts get picked up from the inventory file, but not the correct group_vars. Should I place the redis.yml under ../qa/products/ instead?

解决方案

Inventory (host and group) variables in Ansible are bound to host. Group variables exist for convenience.

If a host is in multiple groups at the same time, all group variables are applied to that host.
If different groups have same variables, they overwrite each other during inventory load process.

So if you have mybox.1.space in groups qa and test, variables from groups qa and test are applied to this host.

Usually you want to use separate inventories to work with different deploy environments. And groups are used to separate different logical units inside inventory.

这篇关于Ansible group_vars 未根据环境/主机列表加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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