ansible - 为什么 Ansible 变量覆盖策略的行为似乎不像记录的那样?
问题描述
假设我有一个参数(ansible 变量),除了一个之外,我想对多个库存保持相同。
根据文档,通过使用剧本级别的 group_vars 和库存级别的 group_vars,我可以为所有库存定义一个组,并且仍然可以通过定位孩子来覆盖库存中的变量,因为孩子优先于父母。
例子
库存/testervm/主机
[central:children]
testervm
[testervm]
127.0.0.1 ansible_connection=local
group_vars/central.yaml
parameter: "I want to be overridden"
库存/testervm/group_vars/central.yaml
parameter: "This override shouldn't work."
库存/testervm/group_vars/testervm.yaml
parameter: "This override should work"
剧本.yaml
- hosts: all
roles:
- the_role
角色/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:感谢弗拉基米尔,我最终做了这个技巧,也许有人会发现它很有用。在剧本中我添加了这个任务:
- 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
这使得在组变量中具有此优先顺序成为可能:
- 库存 group_vars/_overrides(最高)
- 剧本 group_vars
- 库存组变量(最低)
解决方案
你的假设是错误的。playbook group_vars/* (7.) 的优先级高于库存 group_vars/* (6.)。请参阅了解变量优先级。
测试结果正确
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 个选项(超过 10 个。)如何覆盖 host_vars 和 group_vars,这些都不适用于组。 ”
A: All of the 12 options apply, of course. This is the consequence of higher precedence. For example, the playbook below shows how to override parameter by include_vars (precedence 18.) if the host is a member of the group 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"
gives (abridged)
shell> ansible-playbook -i inventories/testervm playbook.yaml
"msg": "Playbook group_vars/central.yaml"
"msg": "Include_vars files/testervm.yaml"
推荐阅读
- c - 如何使用 %n 将高值放入整数?
- c - C 程序未在 Visual Studio Code 上运行
- laravel - 如何在 laravel 8 中将公用文件夹更改为 public_html?
- java - 根据时区将日历转换为日期
- linux - 无法为 sudo -u "user" 设置环境变量
- python - Opencv Dnn 分类的预处理图像
- html - Angular:从onClick事件按钮向输入添加值
- python - 是否有任何端点 (API) 用于在 microsoft graph REST API 中列出事件
- python-pptx - 使用 python-pptx 访问 PowerPoint XML
- tensorflow - libnvinfer.so.5:无法打开共享对象文件:没有这样的文件或目录