# Ansible playbook ping conditional statement

**URL:** <https://forum.ansible.com/t/ansible-playbook-ping-conditional-statement/27743>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [June 7, 2018, 11:21pm UTC](https://forum.ansible.com/t/ansible-playbook-ping-conditional-statement/27743 "2018-06-07T23:21:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [June 7, 2018, 11:21pm UTC](https://forum.ansible.com/t/ansible-playbook-ping-conditional-statement/27743/1 "2018-06-07T23:21:10Z")

</div>

Hello all,

I am trying to get this playbook to work where it runs a task based on the OS type, debian/ubuntu or centos/redhat .My ansible version is 2.4.2.0, I am using ubuntu.

I have a mixture of OS in my environment and I want to ping them, but my issue is how do I get it to work when the user to ping with has to be specified at the command line…example:

`  
ansible-playbook -i ec2.py --private-key /var/lib/rundeck/.ssh/key ping.yml

`

The above wont work , but when I specify the user -u ec2-user , it pings the centos nodes, and if I -u ubuntu, it pings the debian nodes. How can I get the condition to take place in the playbook

`

tasks:

- name: Ping ubuntu  
remote\_user: ubuntu  
become: yes  
ping:  
when: ansible\_distribution == ‘Debian’ or ansible\_distribution == ‘Ubuntu’

- name: Ping centos  
remote\_user: ec2-user  
become: yes  
ping:  
when: ansible\_distribution == ‘Centos’

`

Thank you

---

<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:** [June 8, 2018, 6:51pm UTC](https://forum.ansible.com/t/ansible-playbook-ping-conditional-statement/27743/2 "2018-06-08T18:51:33Z")

</div>

> I am trying to get this playbook to work where it runs a task based on the  
> OS type, debian/ubuntu or centos/redhat .My ansible version is 2.4.2.0, I  
> am using ubuntu.
> 
> I have a mixture of OS in my environment and I want to ping them, but my  
> issue is how do I get it to work when the user to ping with has to be  
> specified at the command line..example:
> 
> ansible-playbook -i ec2.py --private-key /var/lib/rundeck/.ssh/key ping.yml

I don't have any experience with ec2/ec2.py so I can't help you with that.

> The above wont work , but when I specify the user -u ec2-user , it pings  
> the centos nodes, and if I -u ubuntu, it pings the debian nodes. How can I  
> get the condition to take place in the playbook
> 
> tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Ping ubuntu  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remote\_user: ubuntu  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ping:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_distribution == 'Debian' or ansible\_distribution ==  
> 'Ubuntu'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Ping centos  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remote\_user: ec2-user  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ping:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_distribution == 'Centos'

remote\_user is a setting for the ansible.cfg, environment variable or play so you can't use that on task.  
On task you can use become\_user, but become is only relevant when you are already logged inn.

You need to set the parameters before Ansible logs in with ssh, this can be done with ansible\_user variable.  
If your ec2.py can create separate group for each OS you can use the group\_vars to set the ansible\_user.

But the easiest would be to have the same across all platforms.

---

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [June 12, 2018, 2:32pm UTC](https://forum.ansible.com/t/ansible-playbook-ping-conditional-statement/27743/3 "2018-06-12T14:32:16Z")

</div>

thank you
