为什么 Ansible 变量覆盖策略的行为似乎不像记录的那样?

Why Ansible variable override strategy doesn't seem to behave as documented?

假设我有一个参数(ansible 变量),除了一个之外,我想对多个库存保持相同。

根据 the docs,通过使用剧本级别 group_vars 和库存级别 group_vars,我可以为所有库存定义一个组并且仍然能够覆盖库存中的变量通过定位 children 因为 children 优先于 parents.

例子

inventories/testervm/hosts

[central:children]
testervm

[testervm]
127.0.0.1 ansible_connection=local

group_vars/central.yaml

parameter: "I want to be overridden"

inventories/testervm/group_vars/central.yaml

parameter: "This override shouldn't work."

inventories/testervm/group_vars/testervm.yaml

parameter: "This override should work"

playbook.yaml

- hosts: all
  roles:
    - the_role

roles/the_role/tasks/main.yaml

- name: printing the parameter
  debug:
    msg: "{{ parameter }}"

当我运行

$ ansible --version
ansible 2.9.7
[...]

$ ansible-playbook -i inventories/testervm playbook.yaml 

PLAY [all] *********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [127.0.0.1]

TASK [the_role : printing the parameter] ***************************************************************************
ok: [127.0.0.1] => {
    "msg": "I want to be overridden"
}

PLAY RECAP *********************************************************************************************************
127.0.0.1                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

不用说我本以为会看到 "msg": "This override should work"

我哪里错了?

[编辑] 修复了文件路径中的错误路径

[编辑] PS: 多亏了 Vladimir,我才完成了这个技巧,也许有人会觉得它有用。在剧本中我添加了这个任务:

    - name: include variable overrides
      include_vars:
        file: "{{ item }}"
      when: item is file
      with_items: "{{ [inventory_dir ~ '/group_vars/_overrides/'] | product(groups.keys()) | map('join') | product(['.yaml']) | map('join') | list }}"
      tags: always

示例:inventories/testervm/group_vars/_overrides/testervm.yaml

这使得在组变量中具有此优先顺序成为可能:

你的假设是错误的。 playbook group_vars/* (7.) 的优先级高于 inventory group_vars/* (6.)。参见 Understanding variable precedence


测试结果正确

shell> cat playbook.yaml 
- hosts: all
  roles:
    - the_role
shell> cat roles/the_role/tasks/main.yaml 
- name: printing the parameter
  debug:
    msg: "{{ parameter }}"
shell> cat inventories/testervm/hosts 
[central:children]
testervm

[testervm]
127.0.0.1 ansible_connection=local
shell> cat inventories/testervm/group_vars/central.yaml
parameter: "Inventory group_vars/central.yaml"
shell> cat inventories/testervm/group_vars/testervm.yaml
parameter: "Inventory group_vars/testervm.yaml"
shell> cat group_vars/central.yaml
parameter: "Playbook group_vars/central.yaml"

给出(删节)

shell> ansible-playbook -i inventories/testervm playbook.yaml

    "msg": "Playbook group_vars/central.yaml"

问:“仅在一个特定库存中覆盖变量... 12 options(10 以上。)如何覆盖 host_vars 和 group_vars none 其中适用于组。"

A:当然,所有 12 个选项都适用。这是更高优先级的结果。例如,下面的剧本展示了如果主机是组 testervm

- hosts: all
  roles:
    - the_role
  tasks:
    - include_vars: files/testervm.yaml
      when: inventory_hostname in groups.testervm
    - debug:
        msg: "{{ parameter }}"
shell> cat files/testervm.yaml
parameter: "Include_vars files/testervm.yaml"

给出(删节)

shell> ansible-playbook -i inventories/testervm playbook.yaml

    "msg": "Playbook group_vars/central.yaml"
    "msg": "Include_vars files/testervm.yaml"