Running a task only when tag defined

2,891 views
Skip to first unread message

Jon Langemak

unread,
Sep 3, 2016, 1:27:18 PM9/3/16
to Ansible Project
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!



Kai Stian Olstad

unread,
Sep 3, 2016, 7:02:32 PM9/3/16
to ansible...@googlegroups.com
On 03. sep. 2016 19:27, Jon Langemak wrote:
> 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"

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages