Search code examples
dockerdocker-composeansibleansible-inventorywinrm

Issue with Running Docker Compose on Windows via Ansible from Ubuntu


I am trying to manage a Windows machine from Ubuntu using Ansible. I am able to successfully transfer my target files, including Docker Compose files, to the Windows machine. However, when I try to start the Docker Compose files via Ansible, I encounter the following error:

error getting credentials - err: exit status 1, out: The specified session does not exist. It may have been closed earlier.

When I manually run the docker compose up -d command on the Windows machine, it works successfully.

Setup:

Ansible Playbook:

  • Install Docker on the Windows machine
  • Copy the necessary files Start the application with Docker Compose

Defaults:

  • I am connecting to the Windows machine via WinRM.
  • Docker Desktop is installed on the Windows machine.
  • I face issues running docker compose via Ansible, but it works when I run it manually on the Windows machine.

docker-compose.yml file:

---
- name: Deploy Redis on Windows
  hosts: redis_hosts
  vars_files:
    - ./vars.yml

  tasks:
    - name: Ensure Docker is installed
      win_chocolatey:
        name: docker-desktop
        state: present

    - name: Create application directory
      win_file:
        path: "{{ app_files_dest }}"
        state: directory

    - name: Copy application files
      win_copy:
        src: "{{ app_files_src }}/"
        dest: "{{ app_files_dest }}"

 
    - name: Start the application with Docker Compose
      win_command: docker compose -f redis.yml up -d
      args:
        chdir: "{{ app_files_dest }}"

inventory.ini:

[windows] 192.168.2.152 ansible_user=w10 ansible_password=1 ansible_connection=winrm ansible_winrm_transport=basic ansible_port=5985 ansible_winrm_scheme=http

vars.yml:

app_files_src: "{{ file_path }}/cache"

app_files_dest: 'C:\app\cache'

Why do I encounter this error when running docker compose via Ansible? How can I resolve this issue?


Solution

  • Solve is simple.

    because doing it via ansible doesnt run on the same user as when you try it on the windows host

    https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_privilege_escalation.html

    give the become and become user a go

    become: yes
    become_user: <windows user>
    

    add that to your playbook