Skip to main content

ansible-ssh

ansible-ssh is a command-line utility that enables SSH connection to a host, utilizing connection variables retrieved from an Ansible inventory file.
It provides user-friendly bash command completion (including completing hosts from Ansible inventory file). It supports connection details (such as host, port, user, key, and password) as well as advanced SSH options like ProxyJump/ProxyCommand for bastion host configurations.

Features

Simply run ansible-ssh <host> (if ansible.cfg exists) or ansible-ssh -i inventory <host>

  • Automated Connection Parameters: Extracts connection details from an Ansible inventory.
  • ansible.cfg Integration: Automatically detects and uses inventory file from ansible.cfg when present.
  • Advanced SSH Options: Supports ProxyJump, ProxyCommand, and other SSH options via ansible_ssh_common_args and ansible_ssh_extra_args.
  • Supports Vault Encrypted Passwords Detects vault configuration in ansible.cfg or could be provided via --vault-password-file.
  • Fallback Mechanism: Uses standard SSH configuration (e.g., ~/.ssh/config) for any unspecified settings.
  • Smart Bash Completion: Auto-completes inventory files from ansible.cfg and host names from your inventory.
  • Multiple Inventory Formats: Works with both YAML and INI inventory formats.

Requirements

  • Python3
  • Ansible: Required for running ansible-inventory.
  • sshpass: (Optional) Required for password-based SSH connections.
  • bash-completion: This is pretty much 50% of the functionality.
  • jq: Required for parsing JSON output in the bash completion script.

Installation

shell

Clone the repository, link/copy somewhere into $PATH, and install bash completion script.

# Probably don't need to install anything, but for the reference...
sudo apt-get update
sudo apt-get install git python3 ansible-core sshpass jq bash-completion -y

git clone https://github.com/marekruzicka/ansible-ssh.git
chmod +x ansible-ssh/ansible-ssh.py

# Link/copy somewhere within $PATH eg.:
mkdir -p ~/.local/bin/
ln -s $PWD/ansible-ssh/ansible-ssh.py ~/.local/bin/ansible-ssh

# Generate bash_completion script
ansible-ssh -C bash | sudo tee /etc/bash_completion.d/ansible-ssh
source /etc/bash_completion.d/ansible-ssh

pip

Create or activate virtual env, install it using pip, and install bash completion script.

# Create, activate python virtual environment
virtualenv myvenv
source myvenv/bin/activate

# Install package using pip (yes, pypi package has the name reversed. ansible-ssh is taken :-( )
pip install ansible-core ssh-ansible

# Generate bash_completion script
ansible-ssh -C bash | sudo tee /etc/bash_completion.d/ansible-ssh
source /etc/bash_completion.d/ansible-ssh

Usage

$ ansible-ssh --help
usage: ansible-ssh [-h] [-C {bash}] [-i INVENTORY] [host] [--print-only] [-v]

Connect to a host using connection variables from an Ansible inventory.

positional arguments:
  host                  Host to connect to

options:
  -h, --help            show this help message and exit
  -C, --complete {bash}
                        Print bash completion script and exit
  -i, --inventory INVENTORY
                        Path to the Ansible inventory file
  --vault-password-file FILE
                        Vault password file (overrides ansible.cfg vault_password_file)
  --print-only          Print SSH command instead of executing it
  -v, --verbose         Increase SSH verbosity, stackable up to -vvv
  --version             show program's version number and exit

EXAMPLES:
  Connect to a host:
         ansible-ssh.py -i inventory myhost

  Connect to a host with ssh verbosity:
         ansible-ssh.py -i inventory myhost -vv

  Print SSH command:
         ansible-ssh.py -i inventory myhost --print-only

  Generate and install bash completion script:
         ansible-ssh -C bash | sudo tee /etc/bash_completion.d/ansible-ssh

ansible.cfg Integration

ansible-ssh automatically detects and uses inventory configuration from ansible.cfg files, following Ansible's standard configuration hierarchy:

  1. $ANSIBLE_CONFIG environment variable
  2. ./ansible.cfg (current directory)
  3. ~/.ansible.cfg (user home directory)
  4. /etc/ansible/ansible.cfg (system-wide)

Example Configuration

# ansible.cfg
[defaults]
inventory = ./hosts.ini

With this configuration, you can simply run:

# No need to specify -i inventory
ansible-ssh myhost

Smart Bash Completion

The bash completion is ansible.cfg-aware:

  • When you type ansible-ssh -i <TAB>, it suggests the inventory file from ansible.cfg first
  • When no -i is specified, it uses the inventory from ansible.cfg for host completion
# Tab completion suggests ./hosts.ini from ansible.cfg
ansible-ssh -i <TAB>

# Tab completion shows hosts from the configured inventory
ansible-ssh <TAB>

Metadata

Release files for ssh-ansible 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ssh-ansible 1.2.1
File Size Uploaded Provenance Actions
ssh_ansible-1.2.1.tar.gz 24.8 kB With provenance Details for ssh_ansible-1.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ssh-ansible 1.2.1
File Interpreter ABI Platform Actions
ssh_ansible-1.2.1-py3-none-any.whl Python 3 none any Details for ssh_ansible-1.2.1-py3-none-any.whl

Total release size: 35.4 kB

Release files / ssh_ansible-1.2.1.tar.gz

Download URL ssh_ansible-1.2.1.tar.gz
Size 24.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e267e4aff87a1076aee035a5450cac4d6dbb40c18dd98dd39bd0e469d0f71001
BLAKE2b-256 checksum
How to use checksums
fc443509a6ecf87564831bde256dc8eb3f9b65d946b75e38457210146ed26472
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 25, 2026.

Transparency log

Release files / ssh_ansible-1.2.1-py3-none-any.whl

Download URL ssh_ansible-1.2.1-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69355f83655a138c3e7085c88d697f3cbaf171f561ffd51abf3864ad6539db63
BLAKE2b-256 checksum
How to use checksums
bbf9d6e26aa75edae88e21714fe79e1212c5ec471875b8e43ba6b1ef2461bfc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 25, 2026.

Transparency log

Security for ssh-ansible 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ssh_ansible-1.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page