skip to Main Content

I have created a docker container with a base image of Alpine Linux. I try to run whatever Ansible task on that container

Every time I try that, I get the following error:

fatal: [mx]: FAILED! => {"msg": "failed to resolve remote temporary directory from ansible-tmp-1705229690.3186123-3468-162624220881664: `( umask 77 && mkdir -p "` echo ~/.ansible/tmp `"&& mkdir "` echo ~
/.ansible/tmp/ansible-tmp-1705229690.3186123-3468-162624220881664 `" && echo ansible-tmp-1705229690.3186123-3468-162624220881664="` echo ~/.ansible/tmp/ansible-tmp-1705229690.3186123-3468-162624220881664 `" )` returned empty string"}

What may I have done wrong?

First of all, I’ve created the container with the appropriate task. Additionally, in order to prevent the container from exiting immediately after creation, I’ve set the "tty: true" parameter, like this:

- name: Create VM container
  community.docker.docker_container:
    name: "my_alpine_cont"
    image: alpine
    tty: true

In order to run a task in the container, I have added it to a new inventory called my_grp:

- name: add to inventory
  hosts: localhost
  gather_facts: false
  tasks:
    - name: add to inventory
      add_host:
        name: my_alpine_cont
        ansible_connection: docker
        groups:
          - my_grp

Then I try to execute whatever Ansible task on that container:

- name: check container
  hosts: my_grp
  tasks:
    - name: list root dir
      command: ls -l /

This tasks always returns the error shown above, and it doesn’t depend on the fact that I set gather_facts on or off.


After reading Alex`s comment below about needing Python to be installed in the managed nodes, I changed the creation play slightly, to include a step to install python in the Alpine container:

- name: Create VM container
  community.docker.docker_container:
    name: "my_cont"
    image: alpine
    tty: true
    networks:
      - ...

- name: install Python 3 in docker container
  community.docker.docker_container_exec:
    container: "my_cont"
    command: /bin/sh -c "apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python"

- name: adding container to inventory
  add_host:
    name: "my_cont"
    ansible_connection: docker
    groups:
      - "my_grp"

The result anyway doesn’t change, I always get the same error:

fatal: [my_cont]: FAILED! => {"msg": "failed to resolve remote temporary directory from ansible-tmp-1705350902.8582165-2965-162057400712473: `( umask 77 && mkdir -p "` echo ~/.ansible/tmp `"&& mkdir "` echo ~
/.ansible/tmp/ansible-tmp-1705350902.8582165-2965-162057400712473 `" && echo ansible-tmp-1705350902.8582165-2965-162057400712473="` echo ~/.ansible/tmp/ansible-tmp-1705350902.8582165-2965-162057400712473 `" )` returned empty string"}

I think this happens before any actual Python code from Ansible is executed.

Update: this is also happing when choosing other base images, like ubuntu

Full updated command output:

#!/bin/sh
docker run -it –rm -v /var/run/docker.sock:/var/run/docker.sock alpine ash -c ‘
apk add docker-cli ansible py3-requests
ansible-playbook <(
cat <<EOF

  • name: Manage docker container
    hosts: localhost
    connection: local
    gather_facts: false
    vars:
    container_name: "my_alpine_cont"

    tasks:

    • name: Create VM container
      community.docker.docker_container:
      name: "{{ container_name }}"
      image: alpine
      tty: true

    • name: Install Python3
      community.docker.docker_container_exec:
      container: "{{ container_name }}"
      command: apk add python3 # needed for ansible to work

    • name: Add to inventory
      add_host:
      name: "{{ container_name }}"
      ansible_connection: docker
      ‘OF debug: var=out.stdout_lines
      fetch https://dl-cdn.alpinelinux.org/alpine/v3.19/main/x86_64/APKINDEX.tar.gz
      fetch https://dl-cdn.alpinelinux.org/alpine/v3.19/community/x86_64/APKINDEX.tar.gz
      (1/58) Installing libbz2 (1.0.8-r6)
      (2/58) Installing libexpat (2.5.0-r2)
      (3/58) Installing libffi (3.4.4-r3)
      (4/58) Installing gdbm (1.23-r1)
      (5/58) Installing xz-libs (5.4.5-r0)
      (6/58) Installing libgcc (13.2.1_git20231014-r0)
      (7/58) Installing libstdc++ (13.2.1_git20231014-r0)
      (8/58) Installing mpdecimal (2.5.1-r2)
      (9/58) Installing ncurses-terminfo-base (6.4_p20231125-r0)
      (10/58) Installing libncursesw (6.4_p20231125-r0)
      (11/58) Installing libpanelw (6.4_p20231125-r0)
      (12/58) Installing readline (8.2.1-r2)
      (13/58) Installing sqlite-libs (3.44.2-r0)
      (14/58) Installing python3 (3.11.6-r1)
      (15/58) Installing python3-pycache-pyc0 (3.11.6-r1)
      (16/58) Installing pyc (3.11.6-r1)
      (17/58) Installing py3-cparser (2.21-r4)
      (18/58) Installing py3-cparser-pyc (2.21-r4)
      (19/58) Installing py3-cffi (1.16.0-r0)
      (20/58) Installing py3-cffi-pyc (1.16.0-r0)
      (21/58) Installing py3-cryptography (41.0.7-r0)
      (22/58) Installing py3-cryptography-pyc (41.0.7-r0)
      (23/58) Installing py3-markupsafe (2.1.3-r0)
      (24/58) Installing py3-markupsafe-pyc (2.1.3-r0)
      (25/58) Installing py3-jinja2 (3.1.2-r3)
      (26/58) Installing py3-jinja2-pyc (3.1.2-r3)
      (27/58) Installing py3-parsing (3.1.1-r0)
      (28/58) Installing py3-parsing-pyc (3.1.1-r0)
      (29/58) Installing py3-packaging (23.2-r0)
      (30/58) Installing py3-packaging-pyc (23.2-r0)
      (31/58) Installing py3-bcrypt (4.1.1-r0)
      (32/58) Installing py3-bcrypt-pyc (4.1.1-r0)
      (33/58) Installing py3-pynacl (1.5.0-r4)
      (34/58) Installing py3-pynacl-pyc (1.5.0-r4)
      (35/58) Installing py3-paramiko (3.4.0-r0)
      (36/58) Installing py3-paramiko-pyc (3.4.0-r0)
      (37/58) Installing py3-resolvelib (1.0.1-r0)
      (38/58) Installing py3-resolvelib-pyc (1.0.1-r0)
      (39/58) Installing yaml (0.2.5-r2)
      (40/58) Installing py3-yaml (6.0.1-r1)
      (41/58) Installing py3-yaml-pyc (6.0.1-r1)
      (42/58) Installing ansible-core (2.16.1-r0)
      (43/58) Installing ansible-core-pyc (2.16.1-r0)
      (44/58) Installing ansible-pyc (8.6.1-r0)
      (45/58) Installing python3-pyc (3.11.6-r1)
      (46/58) Installing ansible (8.6.1-r0)
      (47/58) Installing ca-certificates (20230506-r0)
      (48/58) Installing docker-cli (24.0.7-r0)
      (49/58) Installing py3-certifi (2023.7.22-r0)
      (50/58) Installing py3-certifi-pyc (2023.7.22-r0)
      (51/58) Installing py3-charset-normalizer (3.3.2-r0)
      (52/58) Installing py3-charset-normalizer-pyc (3.3.2-r0)
      (53/58) Installing py3-idna (3.6-r0)
      (54/58) Installing py3-idna-pyc (3.6-r0)
      (55/58) Installing py3-urllib3 (1.26.18-r0)
      (56/58) Installing py3-urllib3-pyc (1.26.18-r0)
      (57/58) Installing py3-requests (2.31.0-r1)
      (58/58) Installing py3-requests-pyc (2.31.0-r1)
      Executing busybox-1.36.1-r15.trigger
      Executing ca-certificates-20230506-r0.trigger
      OK: 674 MiB in 73 packages
      [WARNING]: No inventory was parsed, only implicit localhost is available
      [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match ‘all’

PLAY [Manage docker container] *******************************************************************************************************************************************************************************

TASK [Create VM container] ***********************************************************************************************************************************************************************************
changed: [localhost]

TASK [Install Python3] ***************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Add to inventory] **************************************************************************************************************************************************************************************
changed: [localhost]

PLAY [Check container] ***************************************************************************************************************************************************************************************

TASK [List root dir] *****************************************************************************************************************************************************************************************
fatal: [my_alpine_cont]: FAILED! => {"msg": "failed to resolve remote temporary directory from ansible-tmp-1705775539.6491027-49-222816465754760: ( umask 77 && mkdir -p " echo ~/.ansible/tmp "&& mkdir " echo ~/.ansible/tmp/ansible-tmp-1705775539.6491027-49-222816465754760 " && echo ansible-tmp-1705775539.6491027-49-222816465754760=" echo ~/.ansible/tmp/ansible-tmp-1705775539.6491027-49-222816465754760 " ) returned empty string"}

PLAY RECAP ***************************************************************************************************************************************************************************************************
localhost : ok=3 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
my_alpine_cont : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

2

Answers


  1. Chosen as BEST ANSWER

    Ok guys, I found out what was wrong here.

    In my setup, I was running Ansible in a WSL VM in Windows 11. As for the docker service, I was sharing the one from the Windows host which I installed via Rancher Desktop. It seems that this setup is not working properly for Docker interactions...

    So this is what I did:

    • I uninstalled Rancher Desktop
    • I installed and configured vanilla Docker in the Ubuntu WSL

    After that, everything is working properly.

    Here is the playbook I successfully ran:

    • name: Manage docker container hosts: localhost connection: local gather_facts: false vars: container_name: "my_cont"

      tasks:

      • name: Create VM container community.docker.docker_container: name: "{{ container_name }}" image: alpine tty: true

      • name: Install Python3 community.docker.docker_container_exec: container: "{{ container_name }}" command: apk add python3

      • name: Add to inventory add_host: name: "{{ container_name }}" ansible_connection: docker groups: - my_grp

    • name: Check container hosts: my_grp gather_facts: false tasks:

      • name: List root dir command: ls -l / register: out

      • debug: var=out.stdout_lines

    So, bottom line for Windows users is: don't use the Docker installation from your Windows host (like the one in Rancher Desktop). Only use the local WSL installation.


  2. Instead of trying to add docker container to the hosts, I suggest you using module called community.docker.docker_container_exec that will allow you execute anything inside docker container. As creation of the container is already fully managed by Ansible name is also know.

    Only missing part can be that you need to execute very specific command that is not possible throught exec, so if that is the point please provide more details what you are trying to execute.

    Example that I tested to execute ls inside container

    - name: Manage docker container
      hosts: localhost
      connection: local
      vars:
        container_name: "my_alpine_cont"
    
      tasks:
      - name: Create VM container
        community.docker.docker_container:
          name: "{{ container_name }}"
          image: alpine
          tty: true
    
      - name: list root dir
        community.docker.docker_container_exec:
          container: "{{ container_name }}"
          command: ls -l /
        register: out
    
      - debug: var=out.stdout_lines
    

    UPDATE
    Based on commend I see that you want to test your playbook inside Docker container, here is what is working for me (Docker instance is on localhost)

    - name: Manage docker container
      hosts: localhost
      connection: local
      gather_facts: false
      vars:
        container_name: "my_alpine_cont"
    
      tasks:
      - name: Create VM container
        community.docker.docker_container:
          name: "{{ container_name }}"
          image: alpine
          tty: true
    
      - name: Install Python3
        community.docker.docker_container_exec:
          container: "{{ container_name }}"
          command: apk add python3 # needed for ansible to work
    
      - name: Add to inventory
        add_host:
          name: "{{ container_name }}"
          ansible_connection: docker
          groups:
            - my_grp
    
    - name: Check container
      hosts: my_grp
      gather_facts: false
      tasks:
      - name: List root dir
        command: ls -l /
        register: out
    
      - debug: var=out.stdout_lines
    

    I’ve compied and pasted the proposed solution into a new playbook but I’m still getting the same error:

    fatal: [my_alpine_cont]: FAILED! => {"msg": "failed to resolve remote temporary directory from ansible-tmp-1705770597.158343-899-137825663447904: ( umask 77 && mkdir -p " echo ~/.ansible/tmp "&& mkdir " echo ~/.ansible/tmp/ansible-tmp-1705770597.158343-899-137825663447904 " && echo ansible-tmp-1705770597.158343-899-137825663447904=" echo ~/.ansible/tmp/ansible-tmp-1705770597.158343-899-137825663447904 " ) returned empty string"}

    I’m running in the host Ansible version [core 2.16.2] and Python version 3.10.12

    UPDATE 2
    Try run this script it will isolate possible issues with your local Ansible instance, it this will work it’s issues with your local

    #!/bin/sh
    docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock alpine ash -c '
    apk add docker-cli ansible py3-requests
    ansible-playbook <(
    cat <<EOF
    - name: Manage docker container
      hosts: localhost
      connection: local
      gather_facts: false
      vars:
        container_name: "my_alpine_cont"
    
      tasks:
      - name: Create VM container
        community.docker.docker_container:
          name: "{{ container_name }}"
          image: alpine
          tty: true
    
      - name: Install Python3
        community.docker.docker_container_exec:
          container: "{{ container_name }}"
          command: apk add python3 # needed for ansible to work
    
      - name: Add to inventory
        add_host:
          name: "{{ container_name }}"
          ansible_connection: docker
          groups:
            - my_grp
    
    - name: Check container
      hosts: my_grp
      gather_facts: false
      tasks:
      - name: List root dir
        command: ls -l /
        register: out
    
      - debug: var=out.stdout_lines
    EOF
    )
    '
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search