【问题标题】:How do you add a tag to a block in Ansible?如何在 Ansible 中为块添加标签?
【发布时间】:2021-02-17 18:20:59
【问题描述】:

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

问题是什么?如何为第二个示例添加标签?

【问题讨论】:

  • 修复最后两个示例和错误消息。这是tags 不是tag。结果是误导性错误did not find expected key。这里真正的问题是mapping values are not allowed in this context
  • 我不确定我是否理解。我使用的是单个标签而不是多个标签。
  • 语法是tags,即使是单个标签。

标签: ansible yaml


【解决方案1】:

标签(和任何其他block keyworkds)放在之外,例如在 block

之前
 tasks:
   - name: Install, configure, and start Apache
     tags: not_broken
     block:
       - name: Install httpd and Memcached
       ...

,或者block结束后

 tasks:
   - name: Install, configure, and start Apache
     block:
       - name: Install httpd and Memcached
       ...
     tags: not_broken

documentation

“块中的所有任务都继承在块级别应用的指令。”

这不适用于block 中的tags。下面的代码 sn-p 将失败并显示消息mapping values are not allowed in this context

- block:
  tags: ntp
  - name: Install ntp

它已在upstream 中修复。代码 sn-p,其中 tagsblock 之外,按预期工作

- 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

'tag' 不是 Block 的有效属性

正确的keywordtags 而不是tag 例如

    - name: Block
      tag: t1
      block:
        - name: Task
          debug:
            msg: Task 1

播放将失败并显示错误消息

ERROR! 'tag' is not a valid attribute for a Block

【讨论】:

    猜你喜欢
    • 2022-07-12
    • 2020-01-31
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 2020-01-26
    • 2014-04-26
    相关资源
    最近更新 更多