0

Is it possible to control Windows-containers from Ansible with "docker" connection-plugin? I.e. with ansible_connection=docker in inventory-file?

My current environment:

  1. Running dockerd -H npipe:// -H tcp://0.0.0.0:2375 on Windows-host to allow remote control from Ansible host;
  2. Running original mcr.microsoft.com/windows/servercore:ltsc2019 container with name "test" on Windows-host;
  3. Ansible-host v2.9 with inventory file:
[host]
<windows_host_ip> ansible_connection=winrm ansible_winrm_transport=ntlm ansible_port=5985 ansible_user=Administrator ansible_password=<pass>

[containers]
test ansible_connection=docker ansible_docker_extra_args="-H tcp://<windows_host_ip>:2375" ansible_shell_executable='c:\windows\system32\cmd.exe' ansible_remote_tmp='c:\windows\temp'
  1. Dirty manual hack in Ansible connection plugin /usr/lib/python3.6/site-packages/ansible/plugins/connection/docker.py : replace "-c" to "/c" in this string: local_cmd = self._build_exec_cmd([self._play_context.executable, '-c', cmd])

With all these steps I can use 'raw' module https://docs.ansible.com/ansible/2.9/modules/raw_module.html#raw-module with syntax: ansible-3 containers -i hosts-docker -m raw -a 'ipconfig' on Windows-container. But any other modules don't work. For example: ansible-3 containers -i hosts-docker -m win_shell -a 'ipconfig' -vvv

<test> ESTABLISH DOCKER CONNECTION FOR USER: ?
<test> EXEC ['/usr/bin/docker', '-H', 'tcp://172.16.215.95:2375', b'exec', b'-i', 'test', 'c:\\windows\\system32\\cmd.exe', '/c', 'c:\\windows\\system32\\cmd.exe -c \'( umask 77 && mkdir -p "` echo c:\\windows\\temp `"&& mkdir "` echo c:\\windows\\temp/ansible-tmp-1636709868.350519-9706-78534593647277 `" && echo ansible-tmp-1636709868.350519-9706-78534593647277="` echo c:\\windows\\temp/ansible-tmp-1636709868.350519-9706-78534593647277 `" ) && sleep 0\'']
test | 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 c:\\windows\\temp `\"&& mkdir \"` echo c:\\windows\\temp/ansible-tmp-1636709868.350519-9706-78534593647277 `\" && echo ansible-tmp-1636709868.350519-9706-78534593647277=\"` echo c:\\windows\\temp/ansible-tmp-1636709868.350519-9706-78534593647277 `\" ), exited with result 1, stdout output: Microsoft Windows [Version 10.0.17763.2237]\r\n(c) 2018 Microsoft Corporation. All rights reserved.\r\n\r\nC:\\>",
    "unreachable": true
}

If I add ansible_shell_type=cmd to inventory-file, then I have another error:

<test> ESTABLISH DOCKER CONNECTION FOR USER: ?
<test> EXEC ['/usr/bin/docker', '-H', 'tcp://172.16.215.95:2375', b'exec', b'-i', 'test', 'c:\\windows\\system32\\cmd.exe', '/c', "c:\\windows\\system32\\cmd.exe -c 'PowerShell -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -EncodedCommand UwBlAHQALQBTAHQAcgBpAGMAdABNAG8AZABlACAALQBWAGUAcgBzAGkAbwBuACAATABhAHQAZQBzAHQACgAkAHQAbQBwAF8AcABhAHQAaAAgAD0AIABbAFMAeQBzAHQAZQBtAC4ARQBuAHYAaQByAG8AbgBtAGUAbgB0AF0AOgA6AEUAeABwAGEAbgBkAEUAbgB2AGkAcgBvAG4AbQBlAG4AdABWAGEAcgBpAGEAYgBsAGUAcwAoACcAYwA6AFwAdwBpAG4AZABvAHcAcwBcAHQAZQBtAHAAJwApAAoAJAB0AG0AcAAgAD0AIABOAGUAdwAtAEkAdABlAG0AIAAtAFQAeQBwAGUAIABEAGkAcgBlAGMAdABvAHIAeQAgAC0AUABhAHQAaAAgACQAdABtAHAAXwBwAGEAdABoACAALQBOAGEAbQBlACAAJwBhAG4AcwBpAGIAbABlAC0AdABtAHAALQAxADYAMwA2ADcAMQAwADEAMgAwAC4ANAA2ADQAMQA5ADIAOQAtADkAOQA0ADMALQAzADEANgAyADAAOQAyADAANAA2ADAAMgA0ADcAJwAKAFcAcgBpAHQAZQAtAE8AdQB0AHAAdQB0ACAALQBJAG4AcAB1AHQATwBiAGoAZQBjAHQAIAAkAHQAbQBwAC4ARgB1AGwAbABOAGEAbQBlAAoASQBmACAAKAAtAG4AbwB0ACAAJAA/ACkAIAB7ACAASQBmACAAKABHAGUAdAAtAFYAYQByAGkAYQBiAGwAZQAgAEwAQQBTAFQARQBYAEkAVABDAE8ARABFACAALQBFAHIAcgBvAHIAQQBjAHQAaQBvAG4AIABTAGkAbABlAG4AdABsAHkAQwBvAG4AdABpAG4AdQBlACkAIAB7ACAAZQB4AGkAdAAgACQATABBAFMAVABFAFgASQBUAEMATwBEAEUAIAB9ACAARQBsAHMAZQAgAHsAIABlAHgAaQB0ACAAMQAgAH0AIAB9AA== && sleep 0'"]
test | 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: PowerShell -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -EncodedCommand UwBlAHQALQBTAHQAcgBpAGMAdABNAG8AZABlACAALQBWAGUAcgBzAGkAbwBuACAATABhAHQAZQBzAHQACgAkAHQAbQBwAF8AcABhAHQAaAAgAD0AIABbAFMAeQBzAHQAZQBtAC4ARQBuAHYAaQByAG8AbgBtAGUAbgB0AF0AOgA6AEUAeABwAGEAbgBkAEUAbgB2AGkAcgBvAG4AbQBlAG4AdABWAGEAcgBpAGEAYgBsAGUAcwAoACcAYwA6AFwAdwBpAG4AZABvAHcAcwBcAHQAZQBtAHAAJwApAAoAJAB0AG0AcAAgAD0AIABOAGUAdwAtAEkAdABlAG0AIAAtAFQAeQBwAGUAIABEAGkAcgBlAGMAdABvAHIAeQAgAC0AUABhAHQAaAAgACQAdABtAHAAXwBwAGEAdABoACAALQBOAGEAbQBlACAAJwBhAG4AcwBpAGIAbABlAC0AdABtAHAALQAxADYAMwA2ADcAMQAwADEAMgAwAC4ANAA2ADQAMQA5ADIAOQAtADkAOQA0ADMALQAzADEANgAyADAAOQAyADAANAA2ADAAMgA0ADcAJwAKAFcAcgBpAHQAZQAtAE8AdQB0AHAAdQB0ACAALQBJAG4AcAB1AHQATwBiAGoAZQBjAHQAIAAkAHQAbQBwAC4ARgB1AGwAbABOAGEAbQBlAAoASQBmACAAKAAtAG4AbwB0ACAAJAA/ACkAIAB7ACAASQBmACAAKABHAGUAdAAtAFYAYQByAGkAYQBiAGwAZQAgAEwAQQBTAFQARQBYAEkAVABDAE8ARABFACAALQBFAHIAcgBvAHIAQQBjAHQAaQBvAG4AIABTAGkAbABlAG4AdABsAHkAQwBvAG4AdABpAG4AdQBlACkAIAB7ACAAZQB4AGkAdAAgACQATABBAFMAVABFAFgASQBUAEMATwBEAEUAIAB9ACAARQBsAHMAZQAgAHsAIABlAHgAaQB0ACAAMQAgAH0AIAB9AA==, exited with result 1, stdout output: Microsoft Windows [Version 10.0.17763.2237]\r\n(c) 2018 Microsoft Corporation. All rights reserved.\r\n\r\nC:\\>",
    "unreachable": true
}

Maybe there are another vars to change Ansible behavior when composing commands?

Your Answer

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

Browse other questions tagged or ask your own question.