Ansible 举个例子:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html#adding-tags-to-blocks
# myrole/tasks/main.yml
tasks:
- block:
tags: ntp
并说“使用块并在该级别定义标签”,但在此页面上:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html
他们使用:
tasks:
- name: Install, configure, and start Apache
block:
- name: Install httpd and memcached
ansible.builtin.yum:
name:
- httpd
- memcached
state: present
如果我尝试“使用块并在该级别定义标签”,例如与:
tasks:
- name: Install, configure, and start Apache
block:
tag: broken
- name: Install httpd and memcached
或者(绝望中)
tasks:
- name: Install, configure, and start Apache
block:
- tag: broken
- name: Install httpd and memcached
我得到:
Syntax Error while loading YAML.
did not find expected key
问题是什么?如何向第二个示例添加标签?
将标签(以及任何其他块键盘)放在块之外
tasks:
- name: Install, configure, and start Apache
tags: not_broken
block:
- name: Install httpd and Memcached
...
tasks:
- name: Install, configure, and start Apache
block:
- name: Install httpd and Memcached
...
tags: not_broken
注意:首选第一个选项。如果 tags 在块之后,ansible-lint
将失败:key-order[task]: You can improve the task key order to: name, tags, block
“块中的所有任务都继承在块级别应用的指令。”
对于标签内部块来说,情况并非如此。下面的代码片段将失败并显示消息:“在此上下文中不允许映射值”
- name: ntp tasks
block:
tags: ntp
- name: Install ntp
...
代码片段(其中 tags 位于 block 外部
)按预期工作- name: ntp tasks
tags: ntp
block:
- name: Install ntp
...
当您将标签放入块时,例如
- name: Block
block:
tags: t1
- name: Task
debug:
msg: Task 1
播放将失败并显示错误消息:
ERROR! Syntax Error while loading YAML.
mapping values are not allowed in this context
正确的关键字是
tags
而不是tag
,例如
- name: Block
tag: t1
block:
- name: Task
debug:
msg: Task 1
播放将失败并显示错误消息:
ERROR! 'tag' is not a valid attribute for a Block