如何在 Ansible 中向块添加标签?

问题描述 投票:0回答:1

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

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

ansible yaml
1个回答
5
投票

标签(以及任何其他块键盘放在之外

  1. 之前
 tasks:

   - name: Install, configure, and start Apache
     tags: not_broken
     block:
       - name: Install httpd and Memcached
         ...
  1. ,或 块结束后
 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
© www.soinside.com 2019 - 2024. All rights reserved.