SSH to host from ansible inventory
Project description
ssh-ansible
ansible-ssh (name of the actual binary) 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 (inlcuding completing hosts from Ansible inventory file).
It supports connection details (such as host, port, user, key, and even password). Extra SSH options via ansible_ssh_common_args and ansible_ssh_extra_args are still experimental and not working properly.
Features
Simply run ansible-ssh -i inventory <host>
- Automated Connection Parameters: Extracts connection details from an Ansible inventory.
- Fallback Mechanism: Uses standard SSH configuration (e.g.,
~/.ssh/config) for any unspecified settings. - Bash Completion: Generates a bash completion script that auto-completes host names based on your inventory.
- Extra SSH Options: Incorporates additional SSH arguments defined in your inventory. (disabled for now)
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)
pip install 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.py [-h] [-C {bash}] [-i INVENTORY] [host] [--print-only] [--debug]
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
--print-only Print SSH command instead of executing it
--debug Increase verbosity (can be used up to 3 times)
EXAMPLES:
Connect to a host:
ansible-ssh.py -i inventory myhost
Connect to a host with ssh verbosity:
ansible-ssh.py -i inventory myhost --debug --debug
Print SSH command:
ansible-ssh.py -i inventory myhost --print-only
Generate and install bash completion script:
ansible-ssh.py -C bash | sudo tee /etc/bash_completion.d/ansible-ssh.py
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ssh_ansible-0.9.0.tar.gz.
File metadata
- Download URL: ssh_ansible-0.9.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e6fb1217d0ffad0e69481e7a6a3f3dd3cc5394a8a753ab7be9d52306e1ec215
|
|
| MD5 |
c08c150abd87a6b967f027ee866fcb9d
|
|
| BLAKE2b-256 |
7d577fb7ff643cae7b8c76050785d7a57c4829bb61b03910a0d44f9ea9c4b2d8
|
File details
Details for the file ssh_ansible-0.9.0-py3-none-any.whl.
File metadata
- Download URL: ssh_ansible-0.9.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bead45d1b89a244173d24b1437d923248775e8b1871c2dd1cc7c75e2c533b8c
|
|
| MD5 |
b9178d6eaa83c4a27d74a101dd32a4c0
|
|
| BLAKE2b-256 |
57059a55ef5f0474eba116cb06bd52b62c2337187dae10befd6d41478c4c4a2d
|