I've always had issues with using ansible
to provision Windows machines with Packer due to the winrm
connections breaking, example issue.
A workaround which you can do is to use the shell-local
on your host machine which calls the Ansible playbook with the new host (although you would need to give it the IP address of your Windows Docker machine).
I've added the example which I did to get a test working. This uploads ip.cmd
to the Windows virtual machine, which gets its IP address, and is then downloaded back to the destination
. The ansible playbook is then called against that.
"provisioners": [
{
"type": "file",
"source": "ip.cmd",
"destination": "C:/tmp/ip.cmd"
},
{
"type": "windows-shell",
"inline": [
"echo [{{ user `ansible_group` }}] > C:/tmp/hosts",
"C:/tmp/ip.cmd"
]
},
{
"type": "file",
"direction": "download",
"source": "C:/tmp/hosts",
"destination": "./ansible/hosts"
},
{
"type": "shell-local",
"command": "ANSIBLE_CONFIG=./ansible.cfg ansible-playbook -v -i ./ansible/hosts -l \"{{ user `ansible_group` }}\" -e \"ansible_user={{ user `username` }} ansible_password={{ user `password` }} ansible_become_pass={{ user `password` }} ansible_port=5986 ansible_connection=winrm ansible_winrm_server_cert_validation=ignore \" ./ansible/site.yml"
}
where ip.cmd
gets the IP address of the Windows host:
@echo off
FOR /F "tokens=2,3" %%A IN ('ping %computername% -n 1 -4') DO IF "from"== "%%A" set "IP=%%~B"
echo %IP:~0,-1% >> C:/tmp/hosts
source