# When Conditionals not skipping certain tasks for UBUNTU remote machines

**URL:** <https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [September 3, 2014, 3:45am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103 "2014-09-03T03:45:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Ross\_Tasipit](https://avatars.discourse-cdn.com/v4/letter/j/9fc348/32.png) [@John\_Ross\_Tasipit](https://forum.ansible.com/u/John_Ross_Tasipit)\
**Post date:** [September 3, 2014, 3:45am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103/1 "2014-09-03T03:45:31Z")

</div>

`

# The default shell for a user if none is specified

users\_default\_shell: /bin/bash

# Create home dirs for new users? Set this to false if you manage home

# directories in some other way.

users\_create\_homedirs: true

# Lists of users to create and delete

users:

- username: username  
name: full\_name\_of\_user  
groups: [‘sudo’]  
**when: ansible\_os\_family == “Debian”**  
groups: [‘wheel’]  
**when: ansible\_os\_family == “RedHat”**  
uid: 2001  
ssh\_key:
- "ssh-dss AAAAB3N…Enter code here…

`

When conditionals not skipping “RedHat” tasks using the above roles/users/default/main.yml syntax, which is for an ubuntu system

I also tried doing this:

`- username: same_as_above_username`  
`name: full_name_of_user`  
`groups: ['wheel']`  
`uid: 1001`

… but still not skipping “redhat” tasks

Hope someone can help. TIA

---

<div class="post-metadata">

**Author:** ![John\_Ross\_Tasipit](https://avatars.discourse-cdn.com/v4/letter/j/9fc348/32.png) [@John\_Ross\_Tasipit](https://forum.ansible.com/u/John_Ross_Tasipit)\
**Post date:** [September 3, 2014, 3:52am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103/2 "2014-09-03T03:52:44Z")

</div>

Sorry for the mistype, on 2nd highlighted code syntax, below is the correct one:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 3, 2014, 11:35am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103/3 "2014-09-03T11:35:06Z")

</div>

In your first example, you can’t stick a “when:” twice in a single task.

You can feed it a list of multiple conditionals, but I think your problem is you are missing a “-” to seperate two different tasks.

However, in the text below, there are other questions and problems, such as “username” vs “user”, which I suspect selects a module, and other duplicated fields, like “groups”.

Another problem is the when is indented and not at “task” level, though the module should yell to you about being sent a parameter it doesn’t know about.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 3, 2014, 11:36am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103/4 "2014-09-03T11:36:36Z")

</div>

In your second example, is this a task, or is this a data definition? I can’t tell because there is not a module named “username” and once again your indentation is all messed up 🙂

If this is your first ansible task, let me know and I can point you to the proper documentation sections, if not, please take a critical eye to how it looks relative to your other tasks and it should be easy to spot.

Again, in failure scenarios, it’s helpful to post the error message. I suspect it’s not only “not skipping”, but the module is telling you what’s wrong.

---

<div class="post-metadata">

**Author:** ![John\_Ross\_Tasipit](https://avatars.discourse-cdn.com/v4/letter/j/9fc348/32.png) [@John\_Ross\_Tasipit](https://forum.ansible.com/u/John_Ross_Tasipit)\
**Post date:** [September 4, 2014, 1:32am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103/5 "2014-09-04T01:32:33Z")

</div>

**Wow! that was a lightning fast reply 😃**

> In your first example, you can’t stick a “when:” twice in a single task.

**Thanks for telling that, now I know** 🙂

> You can feed it a list of multiple conditionals, but I think your problem is you are missing a “-” to seperate two different tasks.

**Sorry I’m pretty confused with this. What I really wanted to do is to automate creating users (which were sysad dudes) for every new server we build. But the problem is, the DEFAULT user included on /etc/sudoers file were different on Ubuntu (%sudo) and CentOS (%wheel).**

**So I stick a “when:” twice in a single task.**

> However, in the text below, there are other questions and problems, such as “username” vs “user”, which I suspect selects a module, and other duplicated fields, like “groups”.

**If you mean text below was this:**  
` `- username: same\_as\_above\_username` `name: full\_name\_of\_user` `groups: [‘wheel’]` `uid: 1001`

`  
**Sorry I paste it wrong.**

> Another problem is the when is indented and not at “task” level, though the module should yell to you about being sent a parameter it doesn’t know about.

**I thought “when” must be indented? Which was sampled here: [http://docs.ansible.com/playbooks\_conditionals.html#the-when-statement](http://docs.ansible.com/playbooks_conditionals.html#the-when-statement)**

---

<div class="post-metadata">

**Author:** ![John\_Ross\_Tasipit](https://avatars.discourse-cdn.com/v4/letter/j/9fc348/32.png) [@John\_Ross\_Tasipit](https://forum.ansible.com/u/John_Ross_Tasipit)\
**Post date:** [September 4, 2014, 1:46am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103/6 "2014-09-04T01:46:16Z")

</div>

**Sorry I pasted it wrong (for unknown cause), here is the correct one:**

`

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [September 4, 2014, 7:01am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103/7 "2014-09-04T07:01:29Z")

</div>

Hi John,

it does not seem like a task, more like variable definition. For example, user module does not have username param, name is used for user name, where you are defining real name.

For examples how to use user module look here: [http://docs.ansible.com/user\_module.html#examples](http://docs.ansible.com/user_module.html#examples)

David

---

<div class="post-metadata">

**Author:** ![John\_Ross\_Tasipit](https://avatars.discourse-cdn.com/v4/letter/j/9fc348/32.png) [@John\_Ross\_Tasipit](https://forum.ansible.com/u/John_Ross_Tasipit)\
**Post date:** [September 4, 2014, 7:07am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103/8 "2014-09-04T07:07:43Z")

</div>

Yes I forgot to mention, I’m aware that it is a variable definition. The task was located in roles/users/tasks/main.yml which contains:

`

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [September 4, 2014, 8:21am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103/9 "2014-09-04T08:21:48Z")

</div>

Hi,

that explains it. When is part of task, it cannot be used i variables (i guess).

In this specific case you do not need at all, just define something like:

sudo\_group: “{% if ansible\_family == ‘RedHat’ %}wheel{% else %}sudo{% endif %}”

in your host\_vars, or main.yml in role vars/defaults.

Than just user sudo\_group in task.

David

---

<div class="post-metadata">

**Author:** ![John\_Ross\_Tasipit](https://avatars.discourse-cdn.com/v4/letter/j/9fc348/32.png) [@John\_Ross\_Tasipit](https://forum.ansible.com/u/John_Ross_Tasipit)\
**Post date:** [September 4, 2014, 8:31am UTC](https://forum.ansible.com/t/when-conditionals-not-skipping-certain-tasks-for-ubuntu-remote-machines/16103/10 "2014-09-04T08:31:58Z")

</div>

Thanks David I will try that
