8

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.

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). Commented Jan 17, 2020 at 16:32

3 Answers 3

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 <whatever you wanted to run> should work.

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

2
  • 1
    Although it is not interactive as stated in the command help, +1 because it is really getting close. Thanks for that one. Commented Feb 29, 2020 at 22:14
  • 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
    Commented Jul 31 at 20:59
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 <host_pattern>"
    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.

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

2
  • 1
    Could you give more than a link-only answer
    – Peter Turner
    Commented Oct 26, 2021 at 12:41
  • 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
    Commented Jul 31 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.