3

I tried to run playbook with role and only few task from it. F.e. I have playbook

# setup.yaml

- hosts: all
  tasks:
    - include_role:
        name: system
      tags: setup

and role "system" with tasks:

# roles/system/tasks/main.yaml

- name: Test ping
  import_tasks: ping.yaml
  tags: test
- name: Create ansible user
  import_tasks: create_user_ansible.yaml
  tags: setup
====================================================
# roles/system/tasks/ping.yaml   
                                                                                                            
- name: test_ping
  ping: 
====================================================
# roles/system/tasks/create_user_ansible.yaml  
                                                                                              
- name: Creating ansible user
  tags: setup
  user:
    name: ansible
    password: '<hash>'
    groups: adm
    state: present
    shell: /bin/bash
    system: no
    createhome: yes
    home: /home/ansible

When I run command

ansible-playbook -i inventories/setup setup.yaml

both of tasks (ping.yaml and create_user_ansible.yaml) running But when I run

ansible-playbook -i inventories/setup setup.yaml --tags setup

it works, like I need.

So, my question:

Is this that behavior, that Ansible developers design, or I made mistake in my playbook, and there are some way, to run only few tasks from role without using --tags in command line?

Anaoliy Tk
  • 58
  • 1
  • 6
  • See [Include Tasks of Role By Tag Or Filter In Ansible Playbook](https://stackoverflow.com/questions/58574687/include-tasks-of-role-by-tag-or-filter-in-ansible-playbook/). – Vladimir Botka Aug 13 '20 at 20:52
  • Thanks, @VladimirBotka, I already knew about this function, but, to tell the truth, I don't like it – Anaoliy Tk Aug 13 '20 at 21:42

1 Answers1

2

This is the default behavior of Ansible. As shown in the documentation:

By default, Ansible runs as if --tags all had been specified.

If you want a play to not run when you didn't specify any tags, you can add the special tag never to it. Again an example from the docs:

Another special tag is never, which will prevent a task from running unless a tag is specifically requested.

Example:

tasks:
  - debug: msg="{{ showmevar }}"
    tags: [ never, debug ]

In this example, the task will only run when the debug or never tag is explicitly requested.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Yes, thanks, tag ```never``` really work. But, it doesn't seems like best practise, does it? But also I suppose, there are no better way? – Anaoliy Tk Aug 13 '20 at 19:26
  • Maybe there is a better way, but I don't know it. – Michael Hampton Aug 13 '20 at 19:31
  • Okay, thankful for your help. I wait some time, and if there are would no better answer, I will mark your answer as best :) – Anaoliy Tk Aug 13 '20 at 19:33
  • `always` and `never` are an elegant addition to the concept of tags. For the very reason that OPs question asks. How is this not a best practice? It's especially useful and easy to comprehend in this 'debug' example. Otherwise you have to comment a bunch of stuff out, or remove it, once you're done debugging. That's fine if you want to keep to a minimum number of lines. If you just want to not run that code unless you call it, marking it with `['never', 'debug']` does exactly that. – Jeter-work May 09 '22 at 18:44