# Using ansible's inventory to ssh

**URL:** <https://forum.ansible.com/t/using-ansibles-inventory-to-ssh/19062>\
**Category:** Ansible Project\
**Created:** [August 19, 2015, 8:26pm UTC](https://forum.ansible.com/t/using-ansibles-inventory-to-ssh/19062 "2015-08-19T20:26:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Picciano](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Brian\_Picciano](https://forum.ansible.com/u/Brian_Picciano)\
**Post date:** [August 19, 2015, 8:26pm UTC](https://forum.ansible.com/t/using-ansibles-inventory-to-ssh/19062/1 "2015-08-19T20:26:12Z")

</div>

Hi there! Sorry if this has been asked before, I searched quite a bit and wasn’t able to find anything of this nature.

We have our ansible repo with our inventory and all of that fun stuff. Currently our inventory is static, but at some point in the future we’'ll likely switch to a dynamic inventory. At the moment when I go to ssh into any of our machines I go to our inventory file which has all the ips, copy the one I want, and paste it into an ssh command. This is kind of a pain. I’m wondering if there’s a way to use ansible to start an ssh session. If not ansible actually starting a process, a way at least for ansible to tell me ip it would use to connect to any given name of a machine. I could easily write a script which just greps through our file, but then this script would get rewritten (and likely become a lot more complicated) when we switch to a dynamic inventory. It also just makes more sense for just ansible to understand the inventory, and to provide the information about it to other processes which need it (at least in my mind).

Hopefully someone has an answer, and thanks!

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [August 19, 2015, 8:39pm UTC](https://forum.ansible.com/t/using-ansibles-inventory-to-ssh/19062/2 "2015-08-19T20:39:29Z")

</div>

Sounds like you're wanting to be able to ssh to a machine based on  
it's name instead of it's ip address. DNS is the obvious answer.

If not that, then maybe create an ansible playbook that generates an  
/etc/hosts file for your control server based on the servers in the  
inventory. Then you can just use the hostname. The downside of that is  
you have to re-run the playbook if any IPs change.

---

<div class="post-metadata">

**Author:** ![Brian\_Picciano](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Brian\_Picciano](https://forum.ansible.com/u/Brian_Picciano)\
**Post date:** [August 19, 2015, 8:52pm UTC](https://forum.ansible.com/t/using-ansibles-inventory-to-ssh/19062/3 "2015-08-19T20:52:04Z")

</div>

The problem with dns, like the /etc/hosts problem you mentioned, is we’d have to run ansible for any inventory changes. Which isn’t the end of the world, I was just hoping there might be a cleaner solution. I have the inventory locally, I have ansible which can read and understand that inventory, I was hoping I could get the information I need directly from ansible rather than having ansible essentially cache it in another place. It’s kind of a weird request, so I understand if there isn’t a great solution. Thanks for the response!

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [August 19, 2015, 8:55pm UTC](https://forum.ansible.com/t/using-ansibles-inventory-to-ssh/19062/4 "2015-08-19T20:55:45Z")

</div>

If the inventory mostly changes when you run other ansible playbooks  
(e.g. you're using Ansible to provision systems), you could potentially  
update /etc/hosts (or DNS, or whatever) as part of the provisioning process.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 20, 2015, 2:00am UTC](https://forum.ansible.com/t/using-ansibles-inventory-to-ssh/19062/5 "2015-08-20T02:00:11Z")

</div>

someone was working on an ansible-ssh that does just that, sshs into  
an 'ansible defined host'

---

<div class="post-metadata">

**Author:** ![Abhijit\_Menon-Sen1](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Abhijit\_Menon-Sen1](https://forum.ansible.com/u/Abhijit_Menon-Sen1)\
**Post date:** [August 20, 2015, 5:38am UTC](https://forum.ansible.com/t/using-ansibles-inventory-to-ssh/19062/6 "2015-08-20T05:38:50Z")

</div>

Yes, I agree completely. I want to be able to treat my inventory as the  
canonical source of all connection information.

I'm the someone who was working on an ansible-ssh that executes ssh with  
the same command-line arguments that ansible would use. It works, but to  
be really useful it needs the functionality provided by PR #11908 to be  
able to construct a ProxyCommand.

[https://github.com/ansible/ansible/pull/11908](https://github.com/ansible/ansible/pull/11908)

I'll put ansible-ssh in a repository on Github sometime. If I haven't  
said anything in a couple of days in this thread, please remind me by  
private mail.

-- Abhijit

---

<div class="post-metadata">

**Author:** ![Abhijit\_Menon-Sen1](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Abhijit\_Menon-Sen1](https://forum.ansible.com/u/Abhijit_Menon-Sen1)\
**Post date:** [August 28, 2015, 7:15am UTC](https://forum.ansible.com/t/using-ansibles-inventory-to-ssh/19062/7 "2015-08-28T07:15:56Z")

</div>

[https://github.com/2ndQuadrant/ansible-ssh](https://github.com/2ndQuadrant/ansible-ssh)

Feedback welcome.

-- Abhijit

---

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [August 28, 2015, 8:02pm UTC](https://forum.ansible.com/t/using-ansibles-inventory-to-ssh/19062/8 "2015-08-28T20:02:59Z")

</div>

not that fancy and no support for non default port, you could do something similar with bash:

# function ansible-ssh { ssh $(ansible -m debug -a “msg={{ ansible\_ssh\_user }}@{{ ansible\_ssh\_host }}” $1 | grep msg |awk -F" ‘{print $4}’);}

# export -f ansible-ssh

# ansible-ssh

esco
