Giter VIP home page Giter VIP logo

ansible-ssh's Introduction

ansible-ssh

An Ansible installation has all necessary parameters for connecting to managed hosts. Duplicating these parameters in ~/.ssh/config is not convenient, especially if a dynamic inventory is used.

This script parses ansible configuration to get these parameters. Just place it somewhere in your $PATH, like ~/.local/bin.

Considered inventory variables: ansible_host, ansible_ssh_host, ansible_user, ansible_ssh_user, ansible_port, ansible_ssh_port, ansible_ssh_private_key_file, ansible_ssh_common_args, ansible_ssh_extra_args.

Considered configuration variables: ssh_args, ssh_executable.

Jinja templates inside variables are rendered, like ansible_ssh_common_args="{{ my_ssh_args }}".

How it works:

user@work$ cd my_ansible_repo
user@work$ cat hosts | grep server1
server1 ansible_host=192.168.0.1 ansible_user=root ansible_port=2222
user@work$ cat ansible.cfg | grep ssh_args
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s

user@work$ ansible-ssh server1
ssh_args:  -C -o ControlMaster=auto -o ControlPersist=60s
ansible_host: 192.168.0.1
ansible_user: root
ansible_port: 2222
ansible_ssh_common_args:
result command: ssh [email protected] -C -o ControlMaster=auto -o ControlPersist=60s -p 2222

root@server1#

Options:

  • [-i INVENTORY] specify inventory file for ansible

  • all options and arguments after the hostname are passed to ssh.

Requirements

  • Ansible
  • jq

Build

Not necessary for end users, just some tooling to use jinja template.

./build.sh

P.S. If you like it, don't forget to put a star on its github repo.

ansible-ssh's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ansible-ssh's Issues

Support inventory scripts

Very nice script btw! I like it very much.

It would be nice if ansible-ssh supported ansible inventory scripts. I did the following hack in my locally installed ansible-ssh:

--- /home/olivier/bin/ansible-ssh.orig	2020-10-01 15:50:55.782872483 -0400
+++ /home/olivier/bin/ansible-ssh	2020-10-01 15:50:29.619670838 -0400
@@ -27,7 +27,7 @@
 # All other arguments are ssh arguments
 shift
 
-inventory=$(ansible-inventory --host "$host")
+inventory=$(ansible-inventory -i ansible/terraform.py --host "$host")
 if [ $? -ne 0 ]; then
     echo "ERROR: ansible inventory failed to gather info on $host"
     exit 1

However, adding a -i flag before the hostname to pass in the ansible inventory script (in my case ansible/terraform.py) would make it more properly supported. I'm willing to submit a PR but would like to know if you are interested in such a change first.

use jq -r instead of piping to tr

Just FYI you can greatly simplify this code by using jq features better ;-) Raw mode will drop the quotes, and you can specify an alternative value for a null result. Seems like you want no value, so ...

ansible_ssh_host=$(echo "$inventory" | jq -r '.ansible_ssh_host? // empty')

Using single quotes around the .jq query will prevent metacharacters from being interpreted by the shell

ssh_executable not set

I updated Ansible recently (brew on mac) and noticed that my ssh_executable is now empty and thus it fails;

ansible-ssh prd-jira
ssh_args:
ssh_executable:
ansible_host: 1.2.3.4
ansible_user: root
ansible_port: 22
ansible_ssh_private_key_file:
ansible_ssh_common_args:
ansible_ssh_extra_args:
result command: [email protected] -p 22
/Users/tomextel/git/ansible-supporting-files/ansible-ssh: line 87: exec: [email protected]: not found

This var is retrieved from "ansible-config dump" but it does (no longer) have any ssh settings weirdly enough.

I could change my ansible.cfg in each directory but i'd rather not. Current config;

retry_files_enabled = False
inventory = ./hosts
roles_path=./roles

become_method=sudo
become_user=root

[ssh_connection]
pipelining = True
ssh_args = -o ControlMaster=auto -o ControlPersist=30m -o ConnectionAttempts=100

Ansible version:

ansible --version
ansible [core 2.11.0]
  config file = /Users/tomextel/git/ansible-playbooks-deheus/ansible.cfg
  configured module search path = ['/Users/tomextel/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/4.0.0/libexec/lib/python3.9/site-packages/ansible
  ansible collection location = /Users/tomextel/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.9.5 (default, May  4 2021, 03:36:27) [Clang 12.0.0 (clang-1200.0.32.29)]
  jinja version = 3.0.1
  libyaml = True

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.