Query Regarding when condition

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

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”

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.

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

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.

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.

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.

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”

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.