Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

Ansible fails to connect from Linux to Windows via SSH

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 ausfx81hrbaren 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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...