2

I'd like to use tags to select specific set of tasks in the case of an installation or another in the an upgrade: so the scenario let's say: I have a task A and a task B I'd like to use tags to select either A or B here is what I have so far : a role where in my task/main.yml :

- name: "Configuration"
  include: 03-configuration.yml
  when: upgrade
  tags:
    - configuration

so I launch my playbook like this which leads me to go to the file 03-configuration.yml and plays everything in this file

ansible-playbook -v ThePlayBook.yml --tags "configuration"

the 03-configuration.yml file:

- name: "A"

- name: "B"

but as you can see in this file 03-configuration.yml I have the two task I'm talking about A and B so at this moment it will try to execute both of them, whereas I want to make use of a tag like "installation" or "upgrade" to launch either one of them.

2 Answers 2

1

It might be possible for you to use an approach like

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: "Config"
    include_tasks: install_update.yml
    tags:
      - config
- name: "A"
  debug:
  tags: install

- name: "B"
  debug:
  tags: update

call it via

ansible-playbook ThePlayBook.yml --tags="config"
ansible-playbook ThePlayBook.yml --tags="config,install"
ansible-playbook ThePlayBook.yml --tags="config,update"

resulting into an output of

PLAY RECAP **********************
localhost                  : ok=1

...

TASK [A] ************************
ok: [localhost] =>
  msg: Hello world!

PLAY RECAP **********************
localhost                  : ok=2

...

TASK [B] ************************
ok: [localhost] =>
  msg: Hello world!

PLAY RECAP **********************
localhost                  : ok=2

Further Readings

1

For example, given the file

shell> cat configuration.yml 
- debug:
    msg: installation
  tags: installation
- debug:
    msg: upgrade
  tags: upgrade

Include it in a playbook

shell> cat playbook.yml
- hosts: localhost
  tasks:
    - include_tasks: configuration.yml
      when: upgrade|default(false)|bool
      tags: configuration

When you run this playbook without any tag

shell> ansible-playbook playbook.yml -e upgrade=true

all tasks are executed

TASK [debug] **************************************************************
ok: [localhost] => 
  msg: installation

TASK [debug] **************************************************************
ok: [localhost] => 
  msg: upgrade

When you run it with the configuration tag only

shell> ansible-playbook playbook.yml -e upgrade=true -t configuration

the file is included but no task is executed


TASK [include_tasks] ********************************************************
included: /export/scratch/tmp8/configuration.yml for localhost

The tags: configuration is not inherited by the tasks in the included file. See Tag inheritance for includes .... If you want to execute such a task both tags are needed if tags are specified on the command line. For example

shell> ansible-playbook playbook.yml -e upgrade=true -t configuration,installation

gives abridged

TASK [debug] **************************************************************
ok: [localhost] => 
  msg: installation

See more details in Tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.