<p>You could try <code>ansible-console {inventory-host-name}</code></p>
<p>It mostly feels like you just have a shell open, unless you use and ansible task name as your command, in which case prefixing it with <code>shell &lt;whatever you wanted to run&gt;</code> should work.</p>
<p>It puts you into an interactive ansible console on the connected host.</p>

CC BY-SA 4.0
2020-02-29T22:14:35.240Z
Although it is not interactive as stated in the command help, +1 because it is really getting close. Thanks for that one.
Zeitounator
CC BY-SA 4.0
2024-07-31T20:59:41.303Z
Agree that while very limited, this does work for simple things using shell or command.  The primary annoyance is that a shell is created and disposed of for each command so you have to chain commands as for example `shell cd /etc;cat hosts`. As there is no way to, for example, do a quick edit with vi, I don't see this being something I would use much.
gview
CC BY-SA 4.0
2020-02-29T06:58:09.643Z
2024-08-01T14:11:50.103Z
2024-08-01T14:11:50.103Z
hvindin
<p>I recently made myself a small script that seems to do the work. It certainly can be done better but maybe you can get some inspiration:</p>
<pre><code>#!/bin/bash

set -e

if [ -z &quot;$1&quot; ]
 then
    echo &quot;No host patter supplied. Please provide a host pattern that would match only one host form the inventory.&quot;
    echo &quot;Usage: ansible-ssh &lt;host_pattern&gt;&quot;
    echo &quot;Example: ./ansible-ssh services[0]&quot;
    exit 1
fi

USER=sysadm
HOST_PATTERN=$1
HOST=`ansible -m shell -a &quot;echo {{ groups.$HOST_PATTERN }}&quot; localhost  | sed 1d`

if grep -q &quot;\[.*\]&quot; &lt;&lt;&lt; &quot;$HOST&quot;; then
  echo &quot;Host pattern $HOST_PATTERN matches more than one host: $HOST&quot;
  exit 1
fi

echo &quot;Connecting to $USER@$HOST...&quot;
ssh $USER@$HOST
</code></pre>
<p>Just edit your default USER (in my case sysadm) and execute in the following way:</p>
<pre><code>./ansible-ssh services[0]
</code></pre>
<p>Where <code>services</code> is your inventory entry holding one or more IPs.</p>

CC BY-SA 4.0
2020-12-31T05:07:06.160Z
2020-12-31T05:07:06.160Z
Aleksander Lech
<p>Here is my solution, which processes variables that Ansible uses to connect hosts, specifically ansible_ssh_common_args, and generates an SSH configuration file and wrapper shell scripts that they use.  It consists of a single playbook and a Python script.</p>
<p>Detailed explanation:
<a href="https://dmrub.github.io/ansible/ssh/2020/10/17/interactive-ssh-with-ansible.html" rel="nofollow noreferrer">https://dmrub.github.io/ansible/ssh/2020/10/17/interactive-ssh-with-ansible.html</a></p>
<p>Source code on github:</p>
<p><a href="https://github.com/dmrub/ansible-ssh-scripts-creator" rel="nofollow noreferrer">https://github.com/dmrub/ansible-ssh-scripts-creator</a></p>

CC BY-SA 4.0
2021-10-26T12:41:32.750Z
Could you give more than a link-only answer
Peter Turner
CC BY-SA 4.0
2024-07-31T20:39:36.563Z
A lot of work went into this, and I can understand how for some people, this would be really useful, but it isn't really what was asked for in the question.  To be clear for others, what it does is that the ansible inventory (aka for most people that might be /etc/ansible/hosts) and generates an ssh .config file.  Like many others I just want to use an inventory name and pull credentials to pass into the ssh command.
gview
CC BY-SA 4.0
2021-10-26T10:47:32.690Z
2021-10-26T20:25:03.820Z
2021-10-26T20:25:03.820Z
Dmitri Rubinstein
<p>Is there a semi-easy way to use Ansible to open an interactive session?</p>
<p>I've spent a good amount of time making use of keys and other credentials stores to make a stellar ansible inventory file. Problem is, when I want to get on the servers that I can easily manage, I've gotta go out and find those creds and take the time to log into them.</p>
<p>It's almost easier for me just to run adhoc commands with ansible instead of logging in via ssh.  Is there a way to pause ansible with a running interactive session so I can get a command prompt?  I noticed that sometimes it lets me accept host keys, but other times it bombs out, (I think this happens when running ssh proxy), so I know things can be done interactively.</p>

CC BY-SA 4.0
2020-01-17T16:32:59.423Z
The host_key acceptance is a local prompt done before connection. Long strory short: no, you can't have interactive ssh sessions inside ansible. At best you can have prompts before a play starts (vars_prompt play section) or during run (prompt module).
Zeitounator
<ansible><ssh>
Using SSH to open interactive session in ansible
Peter Turner
# Using SSH to open interactive session in ansible

(Asked by: username_0 on 2020-01-14)

Is there a semi-easy way to use Ansible to open an interactive session?

I've spent a good amount of time making use of keys and other credentials stores to make a stellar ansible inventory file. Problem is, when I want to get on the servers that I can easily manage, I've gotta go out and find those creds and take the time to log into them.

It's almost easier for me just to run adhoc commands with ansible instead of logging in via ssh. Is there a way to pause ansible with a running interactive session so I can get a command prompt? I noticed that sometimes it lets me accept host keys, but other times it bombs out, (I think this happens when running ssh proxy), so I know things can be done interactively.

**Tags:** ansible, ssh

> **Comments:**
> **username_1**: The host_key acceptance is a local prompt done before connection. Long strory short: no, you can't have interactive ssh sessions inside ansible. At best you can have prompts before a play starts (vars_prompt play section) or during run (prompt module).  

---

## 3 answers

### Answer by username_2 on 2020-02-29. Score: 4

You could try `ansible-console {inventory-host-name}`

It mostly feels like you just have a shell open, unless you use and ansible task name as your command, in which case prefixing it with `shell`  should work.

It puts you into an interactive ansible console on the connected host.

> **Comments:**
> **username_1**: Although it is not interactive as stated in the command help, +1 because it is really getting close. Thanks for that one.  
> **username_3**: Agree that while very limited, this does work for simple things using shell or command.  The primary annoyance is that a shell is created and disposed of for each command so you have to chain commands as for example `shell cd /etc;cat hosts`. As there is no way to, for example, do a quick edit with vi, I don't see this being something I would use much.  

---

### Answer by username_4 on 2020-12-31. Score: 3

I recently made myself a small script that seems to do the work. It certainly can be done better but maybe you can get some inspiration:
#!/bin/bash

set -e

if [ -z "$1" ]
 then
    echo "No host patter supplied. Please provide a host pattern that would match only one host form the inventory."
    echo "Usage: ansible-ssh "
    echo "Example: ./ansible-ssh services[0]"
    exit 1
fi

USER=sysadm
HOST_PATTERN=$1
HOST=`ansible -m shell -a "echo {{ groups.$HOST_PATTERN }}" localhost  | sed 1d`

if grep -q "\[.*\]" <<< "$HOST"; then
  echo "Host pattern $HOST_PATTERN matches more than one host: $HOST"
  exit 1
fi

echo "Connecting to $USER@$HOST..."
ssh $USER@$HOST

Just edit your default USER (in my case sysadm) and execute in the following way:
./ansible-ssh services[0]

Where services is your inventory entry holding one or more IPs.


---

### Answer by username_5 on 2021-10-26. Score: 2

Here is my solution, which processes variables that Ansible uses to connect hosts, specifically ansible\_ssh\_common\_args, and generates an SSH configuration file and wrapper shell scripts that they use. It consists of a single playbook and a Python script.

Detailed explanation:
<https://dmrub.github.io/ansible/ssh/2020/10/17/interactive-ssh-with-ansible.html>

Source code on github:

<https://github.com/dmrub/ansible-ssh-scripts-creator>

> **Comments:**
> **username_0**: Could you give more than a link-only answer  
> **username_3**: A lot of work went into this, and I can understand how for some people, this would be really useful, but it isn't really what was asked for in the question.  To be clear for others, what it does is that the ansible inventory (aka for most people that might be /etc/ansible/hosts) and generates an ssh .config file.  Like many others I just want to use an inventory name and pull credentials to pass into the ssh command.  

---

