ansible - 如何在 Ansible 中为块添加标签?
问题描述
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
有什么问题,您如何为第二个示例添加标签?
解决方案
将标签(和任何其他块 keyworkds)放在块之外,例如在块之前
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
a 内部block
。下面的代码片段将失败并显示消息mapping values are not allowed in this context
- block:
tags: ntp
- name: Install ntp
它已经在上游修复了。tags
位于 之外的代码片段block
按预期工作
- name: ntp tasks
tags: ntp
block:
- name: Install ntp
在此上下文中不允许映射值
当你tags
放入block
例如
- 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
推荐阅读
- json - 如何将 JSON_VALUE 与变量一起使用
- linux - Linux 期望脚本有时以“外部主机关闭连接”结尾,这是期望脚本还是设备问题?
- scala - CollectionConverters 总是返回原始源对象?
- google-chrome - 比 chrome 调试器更好地调试网站的大型 JS 文件的方法?
- google-apps-script - 我可以在此脚本中添加什么以仅以相反的顺序将行粘贴为值
- asp.net-core - SignalR 同时双向流式传输
- java - 一旦用户读取它,如何将我的字符串限制在 001 和最大字符串到 999 实施条件之间
- sql - 具有多个 JOINS 的多个子查询 Oracle
- r - 使用keyword_search检测pdf时文件名太长?
- android - OkHttp 4.x 尝试通过呼叫请求访问 url 时出错