4

I found similar case here, that I am using molecule to test my ansible roles, but for some reason it is skipping "creation" part and gives error like:

fatal: [rabbitmq]: 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 ~/.ansible/tmp `\"&& mkdir \"` echo ~/.ansible/tmp/ansible-tmp-1638541586.6239848-828-250053975102429 `\" && echo ansible-tmp-1638541586.6239848-828-250053975102429=\"` echo ~/.ansible/tmp/ansible-tmp-1638541586.6239848-828-250053975102429 `\" ), exited with result 1", "unreachable": true}

It is skipping the create process: Skipping, instances already created. However, nothing is running:

name@EEW00438:~/.cache$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
name@EEW00438:~/.cache$

what I tried:

  • molecule destroy
  • molecule reset
  • restart
  • rm -rf ~/.cache/
  • changed remote_tmp to /tmp/.ansible/ in /etc/ansible/ansible.cfg
  • reinstall molecule

This issue is only with one role.

UPDATE: it is failing on step:

mkdir \"` echo ~/.ansible/tmp/ansible-tmp-1638782939.31706-2913-12516475286623 `\" && echo ansible-tmp-1638782939.31706-2913-12516475286623=

mkdir: cannot create directory ‘"/home/user/.ansible/tmp/ansible-tmp-1638782939.31706-2913-12516475286623"’: No such file or directory
Eik
  • 41
  • 4

2 Answers2

0

I stumbled upon this issue as well.
When you create the role you need to create it as molecule init role --driver-name docker ns.myrole to enable docker. Be sure to install the docker driver too if you haven't pip install --upgrade molecule-docker

So if you need to tweak the container that runs, edit molecule.yml. It defaults to centos. I switched to ubuntu in there, an created a Dockerfile to provision the container with things that need to exist.

molecule.yml

---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: instance
    image: ubuntu:22.04  # this is required but ignored since I specify a `dockerfile`
    pre_build_image: false
    dockerfile: Dockerfile
provisioner:
  name: ansible
verifier:
  name: ansible

For example, Ubuntu 22.04 doesn't use python anymore, so I added an alias at the end of what molecule renders so that Ansible can use python and have it redirect to python3

FROM ubuntu:22.04

RUN if [ $(command -v apt-get) ]; then export DEBIAN_FRONTEND=noninteractive && apt-get update && apt-get install -y python3 sudo bash ca-certificates iproute2 python3-apt aptitude && apt-get clean && rm -rf /var/lib/apt/lists/*; \
    elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install /usr/bin/python3 /usr/bin/python3-config /usr/bin/dnf-3 sudo bash iproute && dnf clean all; \
    elif [ $(command -v yum) ]; then yum makecache fast && yum install -y /usr/bin/python /usr/bin/python2-config sudo yum-plugin-ovl bash iproute && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
    elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python3 sudo bash iproute2 && zypper clean -a; \
    elif [ $(command -v apk) ]; then apk update && apk add --no-cache python3 sudo bash ca-certificates; \
    elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python3 sudo bash ca-certificates iproute2 && xbps-remove -O; fi

RUN echo 'alias python=python3' >> ~/.bashrc

It's been years since I last used Molecule, and I must say... it's gone downhill. It used to be easy/clear/direct to get things working. Sigh. I guess I should stick to containers and force the migration off VMs sooner!

Chris
  • 754
  • 9
  • 16
0

The problem may be caused by a Docker context change performed at the start of Docker Desktop. Despite this, Molecule does create a container, but in an inactive context.

At startup, Docker Desktop automatically switches the context from default to desktop-linux [1]. The active context determines which containers are available from CLI.

The context cannot be set in the molecule, i.e. the default context is always used to create containers [2].

$ molecule create --scenario-name test
...  # The output with the error is skipped because it duplicates the output from the question

$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

$ docker context ls
NAME                TYPE                DESCRIPTION                               DOCKER ENDPOINT                                    KUBERNETES ENDPOINT   ORCHESTRATOR
default             moby                Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                                              swarm
desktop-linux *     moby                                                          unix:///home/bkarpov/.docker/desktop/docker.sock                         

$ docker context use default

$ docker ps -a
CONTAINER ID   IMAGE                                   COMMAND                  CREATED         STATUS         PORTS     NAMES
a71bfd28992f   geerlingguy/docker-ubuntu2004-ansible   "bash -c 'while true…"   5 minutes ago   Up 5 minutes             some-instance

$ molecule login --scenario-name test
INFO     Running test > login
root@some-instance:/# 

Solutions

  1. Switch the context back to default manually
docker context use default

This solution is suitable for one-time execution, since the context will need to be switched every time Docker Desktop is started. Docker Desktop service will continue to work using the desktop-linux context.

Issue with the request to add context switching to Docker Desktop - https://github.com/docker/roadmap/issues/47

  1. Stop Docker Desktop
systemctl --user stop docker-desktop

Stopping the Docker Desktop service will automatically switch to the default context.

  1. Set DOCKER_CONTEXT so that Docker Desktop does not change the context in the current shell
export DOCKER_CONTEXT=default
systemctl --user restart docker-desktop

When stopping, the context returns to default, and when starting, it does not switch to desktop-linux.

References

  1. https://docs.docker.com/desktop/install/ubuntu/#launch-docker-desktop
  2. https://github.com/ansible-community/molecule-docker#faq
bkarpov
  • 21
  • 4