Passing an IP address of a host as an a command line argument to a bash script

Dear All,

My hosts file looks like this.

[client]
192.168.1.23

[server]
192.168.1.24

[exchange]
192.168.1.25

In my playbook, I want to run a script on a remote machine (“client” in my case) which requires the IP address of the other remote machine (server) as an cmd line argument to that script. My playbook looks like this.

hosts: client
tasks:

  • shell: myscript.sh {{ server }} {{ somevalue }}

What should I do? Please help.

Regards,
Fazal-e-Rehman Khan

I believe you are looking for the inventory_hostname variable.

I also tend to use this in combination with ansible_ssh_host for completeness, such as:

{{ ansible_ssh_host|default(inventory_hostname) }}

No I want to send the IP address of “server” as a command line argument to a script which is be run on “client”
client and server are 2 separate nodes - not the host on which ansible is running.

{{ inventory_hostname }} has all hosts in it. I’m looking for a particular host from the group “server” out of them.

as long as the server group only has 1 ip:

hosts: client
tasks:
   - shell: myscript.sh {{ groups['server'] }} {{ somevalue }}

Thank you so much Brian once again. :slight_smile: