大网数据广告活动
服务器运维问答

Ansible group_vars variable precedence (group_vars override other group_vars)

· 52CE 我爱测

On Ansible 2.2, I have an Ansible hosts file: [webserver] aegir.dev [hostmaster] aegir.dev I have two group_vars/ files: # group_vars/webserver.yml my_var: - vagrant and # group_vars/hostmaster.yml my_var: - vagrant - aegir and t…

问题原文

原帖链接 · tvl · CC BY-SA 3.0

On Ansible 2.2,

I have an Ansible hosts file:

[webserver]
aegir.dev

[hostmaster]
aegir.dev

I have two group_vars/ files:

# group_vars/webserver.yml
my_var:
  - vagrant

and

# group_vars/hostmaster.yml
my_var:
  - vagrant
  - aegir

and the playbook:

- hosts: webserver
  tasks:
    - debug: var=my_var

- hosts: hostmaster
  tasks:
    - debug: var=my_var

Output:

PLAY [webserver] ***************************************************************

TASK [setup] *******************************************************************
ok: [aegir.dev]

TASK [debug] *******************************************************************
ok: [aegir.dev] => {
    "my_var": [
        "vagrant",
        "aegir"
    ]
}

PLAY [hostmaster] **************************************************************

TASK [setup] *******************************************************************
ok: [aegir.dev]

TASK [debug] *******************************************************************
ok: [aegir.dev] => {
    "my_var": [
        "vagrant",
        "aegir"
    ]
}

Why both webserver and hostmaster use the variables from hostmaster.yml?

Probably I'm not using the group_vars correctly but how I can fix that?

EDIT

The real case scenario is that I have the same role running on two groups, one is webserver that actually contains 4 generic servers and the hostmaster group that is a webserver + extra configuration on the same role (the user aegir exists only on aegir.dev and not on the other 3 web-servers)

已采纳解答

原帖链接 · Ocean · CC BY-SA 4.0

This question is a bit old but the mentioned issue already exists, so I wrote my answer to one who needs the answer.

Actually, based on the documentation, Ansible focused on the Host and Task, so groups don’t really survive outside of inventory and host matching. To solve this problem, we need to add a prefix to variables of groups (like using namespacing to avoid conflicts).

# inventory/hosts.yml
webservers:
  hosts:
    host1:
    host2:
  vars:
    webserver_my_var: ...

hostmaster:
  hosts:
    host1:
  vars:
    hostmaster_my_var: ...

And in the playbook, you can use the variable safely.

# playbooks/main.yml
- hosts: webserver
  tasks:
    - debug: var=webserver_my_var | mandatory

- hosts: hostmaster
  tasks:
    - debug: var=hostmaster_my_var | mandatory

This way you will eliminate the problem safely.

Using the same variable name inside multiple groups has another problem. It's hard to guess the value of a variable by just looking at the inventory, you need to know the relation between groups and the order/precedence of one group to another and it is hard work and error-prone.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-11-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。