Ansible doesn't pick up group_vars without loading it manually

依然范特西╮ 提交于 2020-01-11 19:56:43

问题


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

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

[webservers]
phl-web1
phl-web2

[perlservers]
phl-perl1
phl-perl2

[phl-stage:children]
webservers
perlservers

Directory structure:

group_vars
  all
  phl-stage
  phl-prod
site.yml
local.yml

回答1:


You're confusing the structure a bit.

  • 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

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

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.



来源:https://stackoverflow.com/questions/23767765/ansible-doesnt-pick-up-group-vars-without-loading-it-manually

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!