【问题标题】:Unable to provision a Windows Server 2019 vagrant box with Ansible over ssh无法通过 ssh 使用 Ansible 配置 Windows Server 2019 vagrant box
【发布时间】:2021-01-30 02:58:55
【问题描述】:

我使用https://github.com/rgl/windows-vagrant 创建了一个 Windows server 2019 Vagrant 盒子。 我没有做任何更改。

重现步骤:

git clone git@github.com:rgl/windows-vagrant.git
cd windows-vagrant
make build-windows-2019-virtualbox

然后我添加了新的盒子

vagrant box add -f windows-2019-amd64 windows-2019-amd64-virtualbox.box

Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "windows-2019-amd64"
  config.vm.provision :ansible do |ansible|
    ansible.playbook = "./provisioners/ansible/ansible_playbook.yml"
    ansible.config_file = "./provisioners/ansible/ansible.cfg"
    ansible.inventory_path = "vagrant_ansible_inventory"
    config.vm.network "forwarded_port", guest: 22, host: 2222
  end
end

vagrant_ansible_inventory:

默认 ansible_connection=ssh ansible_host=127.0.0.1 ansible_port=2222 ansible_user='vagrant' ansible_password='vagrant' ansible_ssh_pass='vagrant'

我可以启动盒子了:

vagrant up --no-provision

我可以通过 ssh 连接到盒子:

vagrant ssh

(让我进入 Windows Server 2019 上的 DOS shell,我可以在其中执行 DOS shell 命令,例如 dir

不能提供:

vagrant provision

导致错误

==> default: Running provisioner: ansible...
    default: Running ansible-playbook...

PLAY [Jenkins node playbook] ***************************************************

TASK [Gathering Facts] *********************************************************
Friday 29 January 2021  14:21:36 +0100 (0:00:00.015)       0:00:00.015 ******** 
fatal: [default]: 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 ~/.ansible/tmp/ansible-tmp-1611926496.6021953-399403-196553100140944
&& echo ansible-tmp-1611926496.6021953-399403-196553100140944=\"` echo
~/.ansible/tmp/ansible-tmp-1611926496.6021953-399403-196553100140944 `\" ),
exited with result 1", "unreachable": true}

PLAY RECAP *********************************************************************
default                    : ok=0    changed=0    unreachable=1    failed=0
    skipped=0    rescued=0    ignored=0   

Friday 29 January 2021  14:21:36 +0100 (0:00:00.320)       0:00:00.335 ******** 
=============================================================================== 
Gathering Facts --------------------------------------------------------- 0.32s
Playbook run took 0 days, 0 hours, 0 minutes, 0 seconds
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

我该如何解决这个问题?

【问题讨论】:

    标签: windows ssh ansible vagrant


    【解决方案1】:

    解决方案:

    A.在框中,将 Powershell 设置为默认 shell:

    Write-Host 'Set the default shell to PowerShell'
    Set-ItemProperty `
        -Path 'HKLM:\SOFTWARE\OpenSSH' `
        -Name DefaultShell `
        -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    

    B.在库存文件中,添加

    ansible_shell_type='powershell'
    

    为什么?

    因为 Ansible 试图执行:

    umask 77 && mkdir -p \"` echo ~/.ansible/tmp
    `\"&& mkdir ~/.ansible/tmp/ansible-tmp-1611926496.6021953-399403-196553100140944
    && echo ansible-tmp-1611926496.6021953-399403-196553100140944=\"` echo
    ~/.ansible/tmp/ansible-tmp-1611926496.6021953-399403-196553100140944 `\"
    

    这些都是 Linux shell 命令。

    我用vagrant ssh登录盒子并尝试手动执行shell命令,显然失败得很惨。甚至是umask 命令。这不需要任何特权。所以我突然意识到 Ansible 在 DOS shell 中使用 Linux 命令。

    通过将默认的 Windows ssh shell 设置为 Powershell,并告诉 Ansible 使用 powershell 作为 shell 类型,Ansible 终于能够与 Windows 通信了。

    【讨论】:

      猜你喜欢
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 2018-05-08
      • 2021-07-23
      • 2016-01-25
      • 2018-11-18
      相关资源
      最近更新 更多