

Using Openssh as transport for Ansible instead of default paramiko
source link: https://arrfab.net/posts/2012/Oct/30/using-openssh-as-transport-for-ansible-instead-of-default-paramiko/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

You've probably read thatAnsibleuses by default paramikofor the SSH connections to the host(s) you want to manage. But since 0.5 (quite some ago now ...) Ansible can use plain openssh binary as a transport. Why ? simple reasons : you sometimes have complex scenario and you can for example declare a ProxyCommandin your \~/.ssh/config if you need to use a JumpHost to reach the real host you want to connect to. That's fine and I was using that for some of the hosts i have to managed (specifying -c ssh when calling ansible, but having switched to a bash alias containing that string and also -i /path/to/my/inventory for those hosts).
It's great but it can lead to strange results if you don't have a full look at what's happening in the background. Here is the situation I just had yesterday : one of the remote hosts is reachable, but not a standard port (aka tcp/22) so an entry in my \~/.ssh/config was containing both HostName (for the known FQDN of the host I had to point to, not the host i wanted to reach) and Port.
Host myremotehost
HostName my.public.name.or.the.one.from.the.bastion.with.iptables.rule
Port 2222
With such entry, I was able to just "ssh user@myremotehost" and was directly on the remote box. "ansible -c ssh -m ping myremotehost" was happy, but in fact was not reaching the host I was thinking : running "ansible -c ssh -m setup myremotehost -vvv" showed me that ansible_fqdn (one of the ansible facts) wasn't the correct one but instead the host in front of that machine (the one declared with HostName in \~/.ssh/config). The verbose mode showed me that even if you specify the Port in your \~/.ssh/config, ansible will *always* use port 22 :
\<myremotehost> EXEC ['ssh', '-tt', '-q', '-o', 'AddressFamily=inet', '-o', 'ControlMaster=auto', '-o', 'ControlPath=/tmp/ansible-ssh-%h-%p-%r', '-o', 'StrictHostKeyChecking=no', '-o', 'Port=22', '-o', 'User=root', 'myremotehost', 'mkdir -p /var/tmp/ansible-1351603527.81-16435744643257 && echo /var/tmp/ansible-1351603527.81-16435744643257']
Hmm, quickly resolved : a quick discussion with people hanging in the #ansible IRC channel (on irc.freenode.net) explained the issue to me : Port is *never* being looked at in your \~/.ssh/config, even when using -c ssh. Solution is to specify the port in your inventory file, as a variable for that host :
myremotehost ansible_ssh_port=9999
In the same vein, you can also use ansible_ssh_host , this one corresponding to the HostName of your \~/.ssh/config.
Hope that it can save you time, if you encounter the same "issue" one day ...
Recommend
-
48
The eslint-scope npm package got compromised recently, stealing npm credentials from your home directory. We started running tabletop exercises: what else would you smash-and-grab, and how can we mitigate that risk?...
-
49
README.rst Paramiko
-
38
README.md
-
33
概述 ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SSHv2协议(底层使用cryptography)。 有了Paramiko以后,我们就可以在Python代码中直接使用SSH协议对远程服务器执行操作,而不是通...
-
50
paramiko模块 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。 因此,如果需要使用SSH从一个平台连接到另外一个平台,进行一系列的操作时,paramiko是最佳工具之一...
-
11
Installing PyCrypto/Paramiko for Python3 x64 Windows Submitted by NanoDano on Sat, 11/28...
-
12
Jager · 6月16日 · 2017年python · shell · shell脚本 663次已读最近一直在开发自动化运维...
-
11
Daniel Pepuho Posted on Nov 22...
-
10
Python解决paramiko使用invoke_shell交互式命令超时问题 20...
-
6
解决paramiko使用invoke_shell交互式命令超时问题Jager · 3月1日 · 2022年python 78次已读 最近手头有个业务...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK