Testing of ansible roles playbooks using docker

Hello,

i want to begin with molecule but the info i find is realy out dated can some help me out how to get started.

i am running my dev envriroment in dev container where i have installed the following packages

  • molecule
  • molecule-docker
  • flake8
  • flake8-bugbear
  • flake8-docstrings
  • flake8-import-order
  • flake8-pylint
  • pytest
  • pytest-testinfra
  • docker
  • yamllint
    i have veriefied if docker is installed in the devcontainer
root@da34f990ccba:/workspaces/awx/networking/mywebapp# docker --version
Docker version 26.1.4, build 5650f9b

same for ansible and molecule

root@da34f990ccba:/workspaces/awx/networking/mywebapp# ansible --version
ansible [core 2.17.3]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.10/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.10.12 (main, Jun 13 2023, 15:51:40) [GCC 8.3.0] (/usr/local/bin/python3)
  jinja version = 3.1.4
  libyaml = True
root@da34f990ccba:/workspaces/awx/networking/mywebapp# molecule --version
molecule 24.8.0 using python 3.10 
    ansible:2.17.3
    default:24.8.0 from molecule
    docker:2.1.0 from molecule_docker requiring collections: community.docker>=3.0.2 ansible.posix>=1.4.0

verified if the drivers are present

root@da34f990ccba:/workspaces/awx/networking/mywebapp# molecule drivers
default
docker

when i run docker image list i see the used images but this does not reflect in the containers.

root@da34f990ccba:/workspaces/awx/networking/mywebapp# docker image list
REPOSITORY                                                                 TAG       IMAGE ID       CREATED        SIZE
vsc-awx-682cbfca39f2fc364ae70633c3f451640094960dc3abb842c5945f31746c9129   latest    02cb851405e9   6 hours ago    1.66GB
geerlingguy/docker-amazonlinux2-ansible                                    latest    b476c6c635f9   35 hours ago   780MB
geerlingguy/docker-debian12-ansible                                        latest    b3a517821a96   35 hours ago   1.02GB

root@da34f990ccba:/workspaces/awx/networking/mywebapp# docker container list
CONTAINER ID   IMAGE                                                                      COMMAND                  CREATED             STATUS             PORTS     NAMES
87d81eb678c8   geerlingguy/docker-amazonlinux2-ansible                                    "/usr/lib/systemd/sy…"   11 minutes ago      Up 11 minutes                amazonlinux2
da34f990ccba   vsc-awx-682cbfca39f2fc364ae70633c3f451640094960dc3abb842c5945f31746c9129   "/bin/sh -c 'echo Co…"   About an hour ago   Up About an hour             nice_wozniak

These should be amazonlinux2 and a debian one

these are my configs i have used
molecule\default\molecule.yml

---
driver:
  name: docker
platforms:
  - name: amazonlinux2
    image: geerlingguy/docker-amazonlinux2-ansible
    pre_build_image: true
    command: ${MOLECULE_DOCKER_COMMAND:-""}
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup:ro
    privileged: true

  - name: debian
    image: geerlingguy/docker-debian12-ansible
    pre_build_image: true
    command: ${MOLECULE_DOCKER_COMMAND:-""}
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup:ro
    privileged: true

provisioner:
  name: ansible
verifier:
  name: ansible
lint: |
  set -e
  yamllint .
  ansible-lint .

converge.yml

---
- name: Converge
  hosts: all
  gather_facts: false
  tasks:
    - name: Execute main.yml
      ansible.builtin.import_role:
        name: mywebapp
      

task\main.yml

---
# tasks file for mywebapp
- name: Install package
  ansible.builtin.package:
    name: httpd
    state: present

- name: Web Content
  ansible.builtin.copy:
    dest: /var/www/html/index.html
    content: |
      <html>
      <head></head>
      <body>
        <h2>Ansible</h2>
        <hr />
        <p>This page is deployed by ansible.</p>
      </body>
      </html>

- name: Service status
  ansible.builtin.service:
    name: httpd
    state: started
    enabled: true

this is the output of converge

`root@da34f990ccba:/workspaces/awx/networking/mywebapp# molecule converge
WARNING  Driver docker does not provide a schema.
INFO     default scenario test matrix: dependency, create, prepare, converge
INFO     Performing prerun with role_name_check=0...
INFO     Running default > dependency
WARNING  Skipping, missing the requirements file.
WARNING  Skipping, missing the requirements file.
INFO     Running default > create
WARNING  Skipping, instances already created.
INFO     Running default > prepare
WARNING  Skipping, prepare playbook not configured.
INFO     Running default > converge
INFO     Sanity checks: 'docker'

PLAY [Converge] ****************************************************************

TASK [mywebapp : Install package] **********************************************
fatal: [debian]: 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-1724678439.5787213-24303-157287474604759 `\" && echo ansible-tmp-1724678439.5787213-24303-157287474604759=\"` echo ~/.ansible/tmp/ansible-tmp-1724678439.5787213-24303-157287474604759 `\" ), exited with result 1", "unreachable": true}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NoneType: None
fatal: [amazonlinux2]: FAILED! => {"changed": false, "msg": "Could not find a matching action for the \"unknown\" package manager."}

PLAY RECAP *********************************************************************
amazonlinux2               : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
debian                     : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

CRITICAL Ansible return code was 4, command was: ansible-playbook --inventory /root/.cache/molecule/mywebapp/default/inventory --skip-tags molecule-notest,notest /workspaces/awx/networking/mywebapp/molecule/default/converge.yml
root@da34f990ccba:/workspaces/awx/networking/mywebapp# 

if there are some clear instructions regarding the docker side ths should be helpfull the info i have found now is realy outdated. And the docker way should realy make de development process much faster.

after some fideling around i have solved the issue of the container that is not starting by using the latest tag and give the container read and write permissions.

but if there is any clear info i would love to read it :slight_smile:

What are you running this on?