3

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.

  • 2
    What did you try? Please add a packer snippet. – 030 Feb 28 '18 at 21:31
1

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

  • 1
    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 Feb 28 '18 at 19:44
  • 1
    My host OS is macOS - I don't think it's advisable to use Windows as your host OS (docs.ansible.com/ansible/latest/intro_windows.html) – Rekovni Mar 1 '18 at 9:12
  • 1
    I have to build windows containers. I don't have any choices besides windows AFAIK here – David West Mar 1 '18 at 13:59
  • 1
    Maybe I need to consider other configuration management tools – David West Mar 1 '18 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.