<p>I've always had issues with using <code>ansible</code> to provision Windows machines with Packer due to the <code>winrm</code> connections breaking, <a href="https://github.com/hashicorp/packer/issues/4904" rel="nofollow noreferrer">example issue</a>.</p>

<p>A workaround which you can do is to use the <code>shell-local</code> 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).</p>

<p>I've added the example which I did to get a test working. This uploads <code>ip.cmd</code> to the Windows virtual machine, which gets its IP address, and is then downloaded back to the <code>destination</code>. The ansible playbook is then called against that.</p>

<pre><code>"provisioners": [
        {
            "type": "file",
            "source": "ip.cmd",
            "destination": "C:/tmp/ip.cmd"
        },
        {
            "type": "windows-shell",
            "inline": [
                "echo [{{ user `ansible_group` }}] &gt; 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"
        }
</code></pre>

<p>where <code>ip.cmd</code> gets the IP address of the Windows host:</p>

<pre><code>@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% &gt;&gt; C:/tmp/hosts
</code></pre>

<p><a href="https://groups.google.com/forum/#!starred/packer-tool/AXZSY6UoBNo" rel="nofollow noreferrer">source</a></p>

CC BY-SA 3.0
2018-02-28T19:44:20.427Z
I'm trying to provision a Windows Docker container using Ansible and my host OS is Windows. What host OS are you using in your example above?
David West
CC BY-SA 3.0
2018-03-01T09:12:51.970Z
My host OS is macOS - I don't think it's advisable to use Windows as your host OS (https://docs.ansible.com/ansible/latest/intro_windows.html)
Rekovni
CC BY-SA 3.0
2018-03-01T13:59:22.357Z
I have to build windows containers. I don't have any choices besides windows AFAIK here
David West
CC BY-SA 3.0
2018-03-01T14:00:31.487Z
Maybe I need to consider other configuration management tools
David West
CC BY-SA 3.0
2018-02-28T16:49:36.013Z
2018-02-28T16:49:36.013Z
Rekovni
<p>I want to provision a Windows Docker container with Ansible, but I am confused about how exactly it will work.</p>

<p>For instance if I use Packer with Linux, I know I can run <code>ansible</code> (which is ansible remote) and <code>ansible-local</code>. I have to build my Windows Docker containers from a Windows computer and the Windows computer cannot be an Ansible master at this point.</p>

<p>Does any tool, possibly Packer for Windows handle things automatically and spin up a Linux Ansible Master VM to run the playbooks automatically?</p>

<p>How can I easily provision Windows containers with Ansible as part of my image build process?</p>

<p>I am currently using Windows 10 Enterprise and Windows Server 2016 is on the horizon.</p>

CC BY-SA 3.0
2018-02-28T21:31:07.943Z
What did you try? Please add a packer snippet.
030
<docker><ansible><windows>
How do I provision a Windows Docker container with Ansible?
David West
# How do I provision a Windows Docker container with Ansible?

(Asked by: username_0 on 2018-02-28)

I want to provision a Windows Docker container with Ansible, but I am confused about how exactly it will work.

For instance if I use Packer with Linux, I know I can run `ansible` (which is ansible remote) and `ansible-local`. I have to build my Windows Docker containers from a Windows computer and the Windows computer cannot be an Ansible master at this point.

Does any tool, possibly Packer for Windows handle things automatically and spin up a Linux Ansible Master VM to run the playbooks automatically?

How can I easily provision Windows containers with Ansible as part of my image build process?

I am currently using Windows 10 Enterprise and Windows Server 2016 is on the horizon.

**Tags:** docker, ansible, windows

> **Comments:**
> **username_1**: What did you try? Please add a packer snippet.  

---

## 1 answers

### Answer by username_2 on 2018-02-28. Score: 1 | Accepted answer

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

> **Comments:**
> **username_0**: I'm trying to provision a Windows Docker container using Ansible and my host OS is Windows. What host OS are you using in your example above?  
> **username_2**: My host OS is macOS - I don't think it's advisable to use Windows as your host OS (https://docs.ansible.com/ansible/latest/intro_windows.html)  
> **username_0**: I have to build windows containers. I don't have any choices besides windows AFAIK here  
> **username_0**: Maybe I need to consider other configuration management tools  

---

