Ansible special tag 'never' is not working with import_playbook and --tags

Viewed 469

The special tag 'never' is not applied in my second use case. I can't find out in the Ansible document if this is normal behavior, or if it is a bug.

playbook-parent.yml

- name: parent playbook
  import_playbook: playbook-child.yml
  tags:
    - toto

playbook-child.yml

- hosts: local
  roles:
    - role: test
      tags:
        - never

roles/test/tasks/main.yml

- name: hello word
  copy:
    content: hello world
    dest: testfile.txt

Test Case 1: Tag never is applied with the command below ==> OK :)

shell> ansible-playbook playbook-parent.yml

PLAY [local] **********************************

TASK [Gathering Facts] ************************
ok: [127.0.0.]

PLAY RECAP ************************************
...

Test Case 2: Tag 'never' is not applied with the command below ==> Normal? Bug?

ansible-playbook playbook-parent.yml --tags toto

PLAY [local] **********************************

TASK [Gathering Facts] ************************
ok: [127.0.0.]

TASK [test: help word] ************************
changed: [127.0.0.]

PLAY RECAP ************************************
...
2 Answers

Q: "The special tag 'never' is not applied in my second use case, I can't find out in the Ansible document if this is normal behavior."

A: Quoting from Special tags: always and never

If you assign the never tag to a task or play, Ansible will skip that task or play unless you specifically request it (--tags never).

Given the role

shell> cat roles/test/tasks/main.yml 
- debug:
    var: ansible_run_tags

The play

shell> cat playbook-child.yml
- hosts: localhost
  roles:
  - role: test
    tags:
      - never

works as expected

shell> ansible-playbook playbook-child.yml -t never

displays the debug message

TASK [test : debug] ***************************************************
ok: [localhost] => 
  ansible_run_tags:
  - never

and the play without the tag does nothing

shell> ansible-playbook playbook-child.yml

The next playbook applies the tag toto to all tasks in the playbook playbook-child.yml

shell> cat playbook-parent.yml
- name: parent playbook
  import_playbook: playbook-child.yml
  tags:
    - toto

As expected, without any tag the play does nothing

shell> ansible-playbook playbook-parent.yml

But if any of the tags toto or never or both are applied the play displays the debug message

shell> ansible-playbook playbook-parent.yml -t toto

TASK [test : debug] ********************************************************
ok: [localhost] => 
  ansible_run_tags:
  - toto
shell> ansible-playbook playbook-parent.yml -t never

TASK [test : debug] ********************************************************
ok: [localhost] => 
  ansible_run_tags:
  - never
shell> ansible-playbook playbook-parent.yml -t never,toto

TASK [test : debug] ********************************************************
ok: [localhost] => 
  ansible_run_tags:
  - never
  - toto

You appear to be misunderstanding the function of the never tag. As the documentation says:

If you assign the never tag to a task or play, Ansible will skip that task or play unless you specifically request it

You have specifically requested that play to run by passing --tags toto, so it ran. Your example is overly complex; you can demonstrate the behaviour clearly in a single playbook.

- hosts: localhost
  gather_facts: false
  tasks:
    - debug:
        msg: task 1
      tags:
        - never

    - debug:
        msg: task 2
      tags:
        - never
        - foo

    - debug:
        msg: task 3
      tags:
        - foo

    - debug:
        msg: task 4

ansible-playbook test.yml:

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "task 3"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "task 4"
}

ansible-playbook test.yml --tags foo:

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "task 2"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "task 3"
}

ansible-playbooks --tags foo --skip-tags never:

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "task 3"
}
Related