Running a task only when tag defined

I want to be able to run a task only when I pass a tag to the playbook. Namely, I want to have a tag that allows for decoming a user and one would normally never run that as part of normal playbook run.

I found the conditional ‘what: tags is defined’ but it doesnt seem to work. My task looks like this…

  • name: decom user

shell: echo test

when: tag is defined

tags: decom

When I run it, I get this in the verbose output…

TASK [vyatta : decom user] *****************************************************

skipping: [10.10.10.54] => {“changed”: false, “skip_reason”: “Conditional check failed”, “skipped”: true}

I cant sort out why this is failing. Im passing the tag as part of the run…

ansible-playbook test.yml -t decom -v

Any ideas? Thanks!

I want to be able to run a task only when I pass a tag to the playbook.
Namely, I want to have a tag that allows for decoming a user and one would
normally never run that as part of normal playbook run.

To my knowledge this is not possible to do with tags.
But you can do it with variables.

I found the conditional 'what: tags is defined' but it doesnt seem to work.
My task looks like this...

- name: decom user
  shell: echo test
  when: tag is defined

This will check if the variable tag is defined, since it's not it will skip the task.

  tags: decom

When I run it, I get this in the verbose output...

TASK [vyatta : decom user]
*****************************************************

skipping: [10.10.10.54] => {"changed": false, "skip_reason": "Conditional
check failed", "skipped": true}

I cant sort out why this is failing. Im passing the tag as part of the
run...

ansible-playbook test.yml -t decom -v

Any ideas? Thanks!

Instead of tags you can use variable.

   - name: decom user
     debug: msg="Yay I'm running"
     when: decom is defined

You can set the variable at run time like this

ansible-playbook test.yml -e "decom=true"