问题描述
我在角色中使用了许多YAML锚和引用,以将逻辑保持在一个位置,而不是在多个任务中重复自己.以下是一个非常非常基本的示例.
I use a lot of YAML anchors and references in my roles to keep the logic in a single spot instead of repeating myself in multiple tasks. Following is a very very basic example.
- &sometask
name: "Some Task"
some_module: with a lot of parameters
with_items: list_A
- <<: *sometask
name: "Some OTHER Task"
with_items: list_B
因此,我没有对整个任务定义两次,而是对第一个使用锚,并将其所有内容合并到一个新任务中,然后覆盖不同的部分.很好.
So instead of defining the whole task twice, I use an anchor to the first one and merge all its content into a new task, then override the differing pieces. That works fine.
请清楚一点,这是基本的YAML功能,与Ansible本身无关.
Just to be clear, this is basic YAML functionality and has nothing to do with Ansible itself.
以上定义的结果(以及Ansible解析YAML文件时看到的内容)将评估为:
The result of above definition (and what Ansible sees when it parsed the YAML file) would evaluate to:
- name: "Some Task"
some_module: with a lot of parameters
with_items: list_A
- name: "Some Task"
some_module: with a lot of parameters
with_items: list_A
name: "Some OTHER Task"
with_items: list_B
Ansible 2现在具有一项功能,可以在任务中多次定义键时进行投诉.它仍然有效,但是在运行剧本时会产生不必要的噪音:
Ansible 2 now has a feature to complain when keys have been defined multiple times in a task. It still works, but creates unwanted noise when running the playbook:
TASK [Some OTHER Task] *******************************************************
[WARNING]: While constructing a mapping from /some/file.yml, line 42, column 3, found a duplicate dict key (name). Using last defined value only.
[WARNING]: While constructing a mapping from /some/file.yml, line 42, column 3, found a duplicate dict key (with_items). Using last defined value only.
Ansible配置可防止 deprecation_warnings
和 command_warnings
.有没有办法防止这种警告?
Ansible configuration allows to prevent deprecation_warnings
and command_warnings
. Is there a way to also prevent this kind of warning?
推荐答案
从Ansible 2.9.0开始,可以通过将 ANSIBLE_DUPLICATE_YAML_DICT_KEY
环境变量设置为 ignore
来实现..此变量的其他可能值是 warn
(它是默认值并保留原始行为)和 error
(这会使剧本执行失败).
As of Ansible 2.9.0, this can be achieved by setting the ANSIBLE_DUPLICATE_YAML_DICT_KEY
environment variable to ignore
. Other possible values for this variable are warn
, which is the default and preserves the original behaviour, and error
, which makes the playbook execution fail.
有关实现的详细信息,请参见此拉取请求.
See this pull request for details about the implementation.
这篇关于防止Ansible 2中出现重复的按键警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!