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

954 views
Skip to first unread message

Fazal-e-Rehman Khan

unread,
Dec 19, 2014, 6:37:24 AM12/19/14
to ansible...@googlegroups.com
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

Matt Martz

unread,
Dec 19, 2014, 8:12:21 AM12/19/14
to ansible...@googlegroups.com
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) }}
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b4f0839b-e89f-428a-9162-86f310dbcb3b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Message has been deleted

Fazal-e-Rehman Khan

unread,
Dec 19, 2014, 8:30:52 AM12/19/14
to ansible...@googlegroups.com
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.

On Friday, 19 December 2014 18:12:21 UTC+5, Matt Martz wrote:
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) }}

On Friday, December 19, 2014, Fazal-e-Rehman Khan <fazale...@gmail.com> wrote:
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Brian Coca

unread,
Dec 19, 2014, 9:47:35 AM12/19/14
to ansible...@googlegroups.com
as long as the server group only has 1 ip:

hosts: client
tasks:
- shell: myscript.sh {{ groups['server'] }} {{ somevalue }}
>>> email to ansible-proje...@googlegroups.com.
>>> To post to this group, send email to ansible...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/ansible-project/b4f0839b-e89f-428a-9162-86f310dbcb3b%40googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> Matt Martz
>> @sivel
>> sivel.net
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/f4ccc7ed-e769-40f3-861a-a01d538f6d33%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Fazal-e-Rehman Khan

unread,
Dec 19, 2014, 12:12:15 PM12/19/14
to ansible...@googlegroups.com
Thank you so much Brian once again. :)
Reply all
Reply to author
Forward
0 new messages