Skip to main content

SSH to host from ansible inventory

Project description

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.
  • 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

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 {bash}, --complete {bash}
                        Print bash completion script and exit
  -i INVENTORY, --inventory INVENTORY
                        Path to the Ansible inventory file (optional if ansible.cfg exists)
  --print-only          Print SSH command instead of executing it
  -v, --verbose         Increase verbosity, stackable up to -vvv

EXAMPLES:
  Connect to a host (using ansible.cfg):
         ansible-ssh myhost

  Connect to a host with specific inventory:
         ansible-ssh -i inventory myhost

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

  Print SSH command without executing:
         ansible-ssh 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>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ssh_ansible-1.1.0.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ssh_ansible-1.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file ssh_ansible-1.1.0.tar.gz.

File metadata

  • Download URL: ssh_ansible-1.1.0.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ssh_ansible-1.1.0.tar.gz
Algorithm Hash digest
SHA256 fc6c7d33df1a52d313cd14f71d70390bf5c4f446f4401b8ba40de60b13e32964
MD5 fc6bf405ed20d5825ac74950ee74c568
BLAKE2b-256 95481eb96df64e643ab3a8cc561b66477b88e33d58bb96d3f23036ab0b5e44d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for ssh_ansible-1.1.0.tar.gz:

Publisher: build_pypi.yml on marekruzicka/ansible-ssh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ssh_ansible-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: ssh_ansible-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ssh_ansible-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f882eab0101992221301323aa9ebf706d5cc96edf7c704620684a7c88c716db
MD5 f5c70f57f6d3b59740b81c753ec8af71
BLAKE2b-256 9a0451b59388cc79004bb4cd47214b258f66c9e08cd0268f56e2589cf2235250

See more details on using hashes here.

Provenance

The following attestation bundles were made for ssh_ansible-1.1.0-py3-none-any.whl:

Publisher: build_pypi.yml on marekruzicka/ansible-ssh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page