# Query Regarding when condition

**URL:** <https://forum.ansible.com/t/query-regarding-when-condition/21314>\
**Category:** Ansible Developer\
**Created:** [April 29, 2016, 12:06pm UTC](https://forum.ansible.com/t/query-regarding-when-condition/21314 "2016-04-29T12:06:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suresh\_Uppu](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Suresh\_Uppu](https://forum.ansible.com/u/Suresh_Uppu)\
**Post date:** [April 29, 2016, 12:06pm UTC](https://forum.ansible.com/t/query-regarding-when-condition/21314/1 "2016-04-29T12:06:42Z")

</div>

Hi,

I am installing python-setuptools. I am have my play book like that to insatll on Suse and Redhat

- name: installing python-setuptools

yum: name=python-setuptools state=latest  
when: ansible\_os\_family == “RedHat”  
become: yes  
become\_method: sudo

- zypper: name=python-setuptools state=latest  
when: ansible\_os\_family == “SLES”  
become: yes  
become\_method: sudo

I expected the below should work. But it is not. Can I please request any one what I am missing here?

- name: installing python-setuptools

yum: name=python-setuptools state=latest  
when: ansible\_os\_family == “RedHat”

zypper: name=python-setuptools state=latest  
when: ansible\_os\_family == “SLES”

become: yes  
become\_method: sudo

But the copy is working fine. Please see the below.

- name: copying clinet install zip to server  
become: yes  
become\_method: sudo  
copy: src=/root/Desktop/clinetinstall.zip dest=clientproducts/Install.zip  
when: ansible\_distribution == “CentOS”  
copy: src=/root/Desktop/myfile dest=clientproducts/myfile  
when: ansible\_distribution == “RedHat”

Best Regards,  
Suresh

---

<div class="post-metadata">

**Author:** ![Allen\_Sanabria1](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@Allen\_Sanabria1](https://forum.ansible.com/u/Allen_Sanabria1)\
**Post date:** [April 29, 2016, 12:22pm UTC](https://forum.ansible.com/t/query-regarding-when-condition/21314/2 "2016-04-29T12:22:49Z")

</div>

Have you tried using blocks?

- block:

- name: installing python-setuptools

yum:

name: python-setuptools

state: latest

when: ansible\_os\_family == “RedHat”

- block:
- name: installing python-setuptools  
zypper:  
name: python-setuptools  
state: latest  
when: ansible\_os\_family == “SLES”

---

<div class="post-metadata">

**Author:** ![Suresh\_Uppu](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Suresh\_Uppu](https://forum.ansible.com/u/Suresh_Uppu)\
**Post date:** [April 29, 2016, 1:00pm UTC](https://forum.ansible.com/t/query-regarding-when-condition/21314/3 "2016-04-29T13:00:20Z")

</div>

Thanks for your reply.

yes I tried.

I got the same output even I used blocks.

I am seeing two tasks, however one is skipping. End user may feel that why the same task twice and one is skipping. Would like to see one task.

"

TASK [installing python-setuptools] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [54.169.240.218]

TASK [installing python-setuptools] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [54.169.240.218]

"  
I getting below output when I used copy module based distribution. got the below response.

TASK [copying clinet install zip to server] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [54.169.240.218]

Best Regards,  
Suresh.

---

<div class="post-metadata">

**Author:** ![Allen\_Sanabria1](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@Allen\_Sanabria1](https://forum.ansible.com/u/Allen_Sanabria1)\
**Post date:** [April 29, 2016, 1:06pm UTC](https://forum.ansible.com/t/query-regarding-when-condition/21314/4 "2016-04-29T13:06:13Z")

</div>

The block is working as expected. Since the OS that it matches is RedHat, it will than install the redhat and not continue with SLS. I think you should print out the ansible\_os\_family variable and see what it prints out. RedHat or SLS

---

<div class="post-metadata">

**Author:** ![Suresh\_Uppu](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Suresh\_Uppu](https://forum.ansible.com/u/Suresh_Uppu)\
**Post date:** [April 29, 2016, 1:25pm UTC](https://forum.ansible.com/t/query-regarding-when-condition/21314/5 "2016-04-29T13:25:08Z")

</div>

yes block is working as expected.

Can I avoid displaying the below “skipping”. from end user point of view trying the same task twice. However it is not continuing with SLES, but task will be displayed but skipped.

TASK [installing python-setuptools] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [54.169.240.218]

code with block:

- block:

- name: installing python-setuptools  
become: yes  
become\_method: sudo  
yum: name=python-setuptools state=latest  
when: ansible\_os\_family == “RedHat”

- block:

- name: installing python-setuptools  
become: yes  
become\_method: sudo  
zypper: name=python-setuptools state=latest  
when: ansible\_os\_family == “SLES”

I am getting the same out put with my original play.  
original code:

- name: installing python-setuptools

yum: name=python-setuptools state=latest  
when: ansible\_os\_family == “RedHat”  
become: yes  
become\_method: sudo

- name: installing python-setuptools
- zypper: name=python-setuptools state=latest  
when: ansible\_os\_family == “SLES”  
become: yes  
become\_method: sudo

Best Regards,  
Suresh.

---

<div class="post-metadata">

**Author:** ![Allen\_Sanabria1](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@Allen\_Sanabria1](https://forum.ansible.com/u/Allen_Sanabria1)\
**Post date:** [April 29, 2016, 1:28pm UTC](https://forum.ansible.com/t/query-regarding-when-condition/21314/6 "2016-04-29T13:28:16Z")

</div>

I do not think you can avoid the skipping message, as block evaluates the when condition, and if not meant it skips it, which I think is appropriate. I do see your concern though.

---

<div class="post-metadata">

**Author:** ![Suresh\_Uppu](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Suresh\_Uppu](https://forum.ansible.com/u/Suresh_Uppu)\
**Post date:** [April 29, 2016, 2:11pm UTC](https://forum.ansible.com/t/query-regarding-when-condition/21314/7 "2016-04-29T14:11:48Z")

</div>

Thank you very much.

“copy” module displaying one task only. Please see the below play.  
"

- name: copying clinet install zip to server  
become: yes  
become\_method: sudo  
copy: src=/root/Desktop/clinetinstall.zip dest=clientproducts/Install.zip  
when: ansible\_distribution == “CentOS”  
copy: src=/root/Desktop/myfile dest=clientproducts/myfile  
when: ansible\_distribution == “RedHat”

"  
expected the same behavior when I used “yum” and “zypper”.

Thank you very much for your help.

Best Regards,  
Suresh.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 29, 2016, 2:24pm UTC](https://forum.ansible.com/t/query-regarding-when-condition/21314/8 "2016-04-29T14:24:37Z")

</div>

It seems your problem, is that you have multiple module calls within a single task, which is not permitted.

You need two tasks, one with each copy, such as:

- name: copying clinet install zip to server (CentOS)  
become: yes  
become\_method: sudo  
copy: src=/root/Desktop/clinetinstall.zip dest=clientproducts/Install.zip  
when: ansible\_distribution == “CentOS”

- name: copying clinet install zip to server (RedHat)  
become: yes  
become\_method: sudo

copy: src=/root/Desktop/myfile dest=clientproducts/myfile

when: ansible\_distribution == “RedHat”

---

<div class="post-metadata">

**Author:** ![Suresh\_Uppu](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Suresh\_Uppu](https://forum.ansible.com/u/Suresh_Uppu)\
**Post date:** [May 2, 2016, 6:21am UTC](https://forum.ansible.com/t/query-regarding-when-condition/21314/9 "2016-05-02T06:21:34Z")

</div>

Thanks for your reply.

I was on vacation and travelling. Sorry for delayed reply.

its working fine when multiple module calls in a single task.

This is fine.

- name: copying clinet install zip to server  
become: yes  
become\_method: sudo  
copy: src=/root/Desktop/clinetinstall.zip dest=clientproducts/Install.zip  
when: ansible\_distribution == “CentOS”  
copy: src=/root/Desktop/myfile dest=clientproducts/myfile  
when: ansible\_distribution == “RedHat”

I expected the same when I used yum and zypper module in s single task.

This is not working:

- name: installing python-setuptools

yum: name=python-setuptools state=latest  
when: ansible\_os\_family == “RedHat”  
zypper: name=python-setuptools state=latest

when: ansible\_os\_family == “SLES”  
become: yes  
become\_method: sudo

Best Regards,  
Suresh.
