Ansible playbook ping conditional statement

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

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:
    - 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'

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.

thank you