Ansible 无法通过 SSH 从 Linux 连接到 Windows [英] Ansible fails to connect from Linux to Windows via SSH

查看:73
本文介绍了Ansible 无法通过 SSH 从 Linux 连接到 Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 choco 上设置了一台带有 OpenSSH 8.0.0.1 的 Windows 10 机器 这里.我已经设置了一个公钥,现在可以从我的 Linux 机器连接到 Windows.

$ ssh windows-machine echo test测试

运行以下命令会报错:

$ ansible -m win_command -a "echo test";窗户机windows-machine |无法访问!=>{改变":假,msg":无法创建临时目录.在某些情况下,您可能已经能够进行身份验证并且没有目标目录的权限.考虑将 ansible.cfg 中的远程 tmp 路径更改为以 \/tmp\"为根的路径,有关更多错误信息,请使用 -vvv.失败的命令是:( umask 77 && mkdir -p \"` echo ~/.ansible/tmp `\"&& mkdir \"` echo ~/.ansible/tmp/ansible-tmp-1619774767.22-15575-62648242524392`\"&安培;&安培;回声ansible-TMP-1619774767.22-15575-62648242524392 = \"`回波〜/.ansible/TMP/ansible-TMP-1619774767.22-15575-62648242524392`\"), 以结果 1" 退出,无法访问":true}

那些是当前目录下的文件:

$ lsansible.cfg 主机$ cat ansible.cfg[默认]库存= ./hostsansible_connection = sshansible_shell_type = powershell$猫主机窗户机

我已将 Windows 上的默认 shell 设置为 powershell.我还使用 cmd 作为默认 shell 对其进行了测试,但这并没有改变任何东西.错误仍然存​​在.

我已经将 ansible 更新到最新版本:

pip3 install --user --upgrade pippip3 install --user ansible哈希 -r

现在我得到了:

$ ansible --version2.10.8 版配置文件 =/home/user/git/ansible-win/ansible.cfg配置的模块搜索路径 = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python 模块位置 =/home/user/.local/lib/python3.6/site-packages/ansible可执行位置 =/home/user/.local/bin/ansiblepython 版本 = 3.6.9(默认,2021 年 1 月 26 日,15:33:00)[GCC 8.4.0]

使用 -vvv 运行相同的命令:

$ ansible -m win_command -vvv -a "echo test";窗户机

给出我保存到 pastebin

的以下输出

关键部分:(1, '', '/bin/sh : Die Benennung "/bin/sh" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines \r\nausf\x81hrbaren Programms erkannt.>

我看过:

我现在的问题是:

  • 是否可以通过 SSH 使用 Ansible for Windows?
  • 如果是:如何?

解决方案

ansible 中缺少的部分是选项 ansible_shell_type=powershellshell_type=powershell.cfg-file 被命令行 ansible 调用忽略.

命令行需要选项 --extra-vars 或短版本 -e 来告诉需要使用什么 shell.

因此,对于 Windows 机器,从 linux 机器调用 ssh windows-machine 正在工作,这台 windows 机器配置了默认 shell 设置为 powershell 和 linux 机器上的以下文件:

$ lsansible.cfg 主机$ cat ansible.cfg[默认]库存= ./hosts$猫主机窗户机

我得到了这个结果:

$ ansible -m win_ping -e 'ansible_shell_type=powershell' windows-machinewindows-machine |成功 =>{改变":假,乒":乒"}

I've setup a Windows 10 machine with OpenSSH 8.0.0.1 over choco as discribed here. I've setup a public key and I can now connect from my Linux machine to the Windows.

$ ssh windows-machine echo test
test

Running the following command will result in a error:

$ ansible -m win_command -a "echo test" windows-machine
windows-machine | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to create temporary directory.In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in \"/tmp\", for more error information use -vvv. Failed command was: ( umask 77 && mkdir -p \"` echo ~/.ansible/tmp `\"&& mkdir \"` echo ~/.ansible/tmp/ansible-tmp-1619774767.22-15575-62648242524392 `\" && echo ansible-tmp-1619774767.22-15575-62648242524392=\"` echo ~/.ansible/tmp/ansible-tmp-1619774767.22-15575-62648242524392 `\" ), exited with result 1", 
    "unreachable": true
}

Those are the files in the current directory:

$ ls
ansible.cfg  hosts

$ cat ansible.cfg 
[defaults]
inventory = ./hosts
ansible_connection = ssh
ansible_shell_type = powershell

$ cat hosts 
windows-machine

I've setup the default shell on windows to be powershell. I've also tested it with cmd as the default shell, but this didn't change anything. The error still remains.

I've update ansible to the latest version with:

pip3 install --user --upgrade pip
pip3 install --user ansible
hash -r

Now I got:

$ ansible --version
ansible 2.10.8
  config file = /home/user/git/ansible-win/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/.local/lib/python3.6/site-packages/ansible
  executable location = /home/user/.local/bin/ansible
  python version = 3.6.9 (default, Jan 26 2021, 15:33:00) [GCC 8.4.0]

Running the same command with -vvv:

$ ansible -m win_command -vvv -a "echo test" windows-machine

Gives following output which I saved to pastebin

The crucial part beeing: <windows-machine> (1, '', '/bin/sh : Die Benennung "/bin/sh" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines \r\nausf\x81hrbaren Programms erkannt.

I've looked at:

My question is now:

  • Is it at all possible to use Ansible for Windows with SSH?
  • If yes: how?

解决方案

The missing part was the option ansible_shell_type=powershell and shell_type=powershell in the ansible.cfg-file is beeing ignored by the command-line ansible call.

The command-line needs the option --extra-vars or the short version -e to tell what shell needs to be used.

So with a windows machine where calling ssh windows-machine from the linux machine is working, this windows machine configured with a default shell set to powershell and the following files on the linux machine:

$ ls
ansible.cfg  hosts

$ cat ansible.cfg 
[defaults]
inventory = ./hosts

$ cat hosts
windows-machine

I get this result:

$ ansible -m win_ping -e 'ansible_shell_type=powershell' windows-machine
windows-machine | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

这篇关于Ansible 无法通过 SSH 从 Linux 连接到 Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆