# Problem with conditionals restricting certain tasks

**URL:** <https://forum.ansible.com/t/problem-with-conditionals-restricting-certain-tasks/25615>\
**Category:** Ansible Project\
**Created:** [September 27, 2017, 4:52pm UTC](https://forum.ansible.com/t/problem-with-conditionals-restricting-certain-tasks/25615 "2017-09-27T16:52:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Dave](https://forum.ansible.com/u/Dave)\
**Post date:** [September 27, 2017, 4:52pm UTC](https://forum.ansible.com/t/problem-with-conditionals-restricting-certain-tasks/25615/1 "2017-09-27T16:52:40Z")

</div>

Hi,

Just had one more question. Is there a way to stop an Ansible playbook or set of lines from executing if a variable matches a specific text? I saw the Ansible conditionals [documentation](http://docs.ansible.com/ansible/latest/playbooks_conditionals.html), however it doesn’t provide good examples on how to achieve this with a “when:” statement nor does it tell where would be best to insert these conditionals to prevent the specific lines from executing.

Here’s my script

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 27, 2017, 5:12pm UTC](https://forum.ansible.com/t/problem-with-conditionals-restricting-certain-tasks/25615/2 "2017-09-27T17:12:51Z")

</div>

> Hi,
> 
> Just had one more question. Is there a way to stop an Ansible playbook or  
> set of lines from executing if a variable matches a specific text? I saw  
> the Ansible conditionals documentation  
> \<[http://docs.ansible.com/ansible/latest/playbooks\_conditionals.html&gt;](http://docs.ansible.com/ansible/latest/playbooks_conditionals.html&gt;),  
> however it doesn't provide good examples on how to achieve this with a  
> "when:" statement nor does it tell where would be best to insert these  
> conditionals to prevent the specific lines from executing.
> 
> Here's my script
> 
> ---  
> - hosts: all
> 
> &nbsp;&nbsp;&nbsp;become: yes
> 
> &nbsp;&nbsp;&nbsp;vars\_prompt:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: "new\_user"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prompt: "What is the name of the new user?"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: new\_user != "root"

vars\_prompt doesn't support when, only task does.

> &nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: some function  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: .....  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: another function  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: .....  
> &nbsp;&nbsp;&nbsp;  
> Do I tack the "when:" conditional on at the end of the vars\_prompt: or  
> after each "command:" function below that to stop the commands from being  
> run?

You would need it on each task.

But you can group task together with block: and set the when on the block. Then Ansible will add when on all the task

Or put all the task in a file, and use include: \<file\> and add the when to the include, here too Ansible will add when to each task.

---

<div class="post-metadata">

**Author:** ![Dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Dave](https://forum.ansible.com/u/Dave)\
**Post date:** [September 27, 2017, 7:10pm UTC](https://forum.ansible.com/t/problem-with-conditionals-restricting-certain-tasks/25615/3 "2017-09-27T19:10:31Z")

</div>

Thanks Kai. For some reason, when I run my playbook though, the Ansible doesn’t like how the when: conditional is structured

`when: new_user != "root"`

**fatal: [192.168.1.5]: FAILED! =\> {“changed”: false, “failed”: true, “invocation”: {“module\_args”: {“name”: “dave3”, “when”: “new\_user != "root"”}, “module\_name”: “user”}, “msg”: “unsupported parameter for module: when”}**

I don’t see too many examples where a variable is invoked by when: so, I’m not sure if the syntax somehow is wrong. I tried specifying it as a list as well, but that had no effect:

**fatal: [192.168.76.103]: FAILED! =\> {“changed”: false, “failed”: true, “invocation”: {“module\_args”: {“name”: “dave3”, “when”: [“new\_user != "root"”]}, “module\_name”: “user”}, “msg”: “unsupported parameter for module: when”}**

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 27, 2017, 7:27pm UTC](https://forum.ansible.com/t/problem-with-conditionals-restricting-certain-tasks/25615/4 "2017-09-27T19:27:51Z")

</div>

> Thanks Kai. For some reason, when I run my playbook though, the Ansible  
> doesn't like how the when: conditional is structured
> 
> when: new\_user != "root"

This when check if the variable new\_user not equal to the string root

> \*fatal: [192.168.1.5]: FAILED! =\> {"changed": false, "failed": true,  
> "invocation": {"module\_args": {"name": "dave3", "when": "new\_user !=  
> \"root\""}, "module\_name": "user"}, "msg": "unsupported parameter for  
> module: when"}\*
> 
> I don't see too many examples where a variable is invoked by when: so, I'm  
> not sure if the syntax somehow is wrong. I tried specifying it as a list as  
> well, but that had no effect:

Most when is using variables, and for the Ansible docs about conditional you posted has several (actually all of them has at least one variable)

But you error messages indicate wrong indentation of when.  
This would be easier if you showed your code and not just the error, but if you look at the link you posted you'll see when is indentation must be at the same level as the module name.

---

<div class="post-metadata">

**Author:** ![Dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Dave](https://forum.ansible.com/u/Dave)\
**Post date:** [September 27, 2017, 8:02pm UTC](https://forum.ansible.com/t/problem-with-conditionals-restricting-certain-tasks/25615/5 "2017-09-27T20:02:44Z")

</div>

I see where the issue was. With the when: conditional, doesn’t use the brackets and quotes for referencing. It’s just the variable name itself.

Yea, you’re right, the place where I defined my when: variable was in the wrong module and not under task. It’s working now. Thanks!
