Ansible 不会在没有手动加载的情况下选择 group_vars [英] Ansible doesn't pick up group_vars without loading it manually

查看:30
本文介绍了Ansible 不会在没有手动加载的情况下选择 group_vars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 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/<;环境名称 >.如果我在 vars_files 中包含文件 group_vars/phl-stage 它可以工作,但我会认为组文件会自动加载吗?

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

- 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 放入组中,否则除了 group_vars/all.
  • 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屋!

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