Reputation: 86167
Ansible give this example:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html#adding-tags-to-blocks
# myrole/tasks/main.yml
tasks:
- block:
tags: ntp
and says "use a block and define the tags at that level" but then on this page:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html
they use:
tasks:
- name: Install, configure, and start Apache
block:
- name: Install httpd and memcached
ansible.builtin.yum:
name:
- httpd
- memcached
state: present
If I try to "use a block and define the tags at that level", e.g. with:
tasks:
- name: Install, configure, and start Apache
block:
tag: broken
- name: Install httpd and memcached
or (in desperation)
tasks:
- name: Install, configure, and start Apache
block:
- tag: broken
- name: Install httpd and memcached
I get:
Syntax Error while loading YAML.
did not find expected key
What's the problem and how do you add a tag to that second example?
Upvotes: 4
Views: 6782
Reputation: 68074
Put tags (and any other block keyworkds) outside the block
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
Note: The first option is preferred. ansible-lint will fail if tags is after the block:
key-order[task]: You can improve the task key order to: name, tags, block
The documentation says:
"All tasks in a block inherit directives applied at the block level."
This is not true for tags inside a block. The code snippet below will fail with the message: "mapping values are not allowed in this context"
- name: ntp tasks
block:
tags: ntp
- name: Install ntp
...
The code snippet, where tags is outside the block, works as expected
- name: ntp tasks
tags: ntp
block:
- name: Install ntp
...
When you put tags inside the block, e.g.
- name: Block
block:
tags: t1
- name: Task
debug:
msg: Task 1
the play will fail with the error message:
ERROR! Syntax Error while loading YAML.
mapping values are not allowed in this context
The correct keyword is tags
not tag
, e.g.
- name: Block
tag: t1
block:
- name: Task
debug:
msg: Task 1
the play will fail with the error message:
ERROR! 'tag' is not a valid attribute for a Block
Upvotes: 6