Molecule: Can't run a basic test.

Created on 27 Mar 2018  ·  17Comments  ·  Source: ansible-community/molecule

Issue Type

Bug report

Molecule and Ansible Details

(.venv) [root@molecule httpd]# ansible --version
ansible 2.5.0
  config file = None
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /root/.venv/lib/python2.7/site-packages/ansible
  executable location = /root/.venv/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
(.venv) [root@molecule httpd]# molecule --version
molecule, version 2.11.0

Molecule installation method (one of): pip

Ansible installation method (one of): pip

Desired Behavior

I followed all the steps on the quick-start but it didn't go as smoothly as it should've. (CentOS 7)

yum install -y epel-release
yum install -y python-pip gcc python-devel openssl-devel
pip install virtualenv
virtualenv --no-site-packages .venv
source .venv/bin/activate
pip install molecule ansible docker-py
molecule init role -r httpd -d docker
cd httpd
molecule test

Actual Behaviour (Bug report only)

(.venv) [root@molecule httpd]# molecule test
--> Validating schema /root/httpd/molecule/default/molecule.yml.
Validation completed successfully.
--> Test matrix

└── default
    ├── lint
    ├── destroy
    ├── dependency
    ├── syntax
    ├── create
    ├── prepare
    ├── converge
    ├── idempotence
    ├── side_effect
    ├── verify
    └── destroy

--> Scenario: 'default'
--> Action: 'lint'
--> Executing Yamllint on files found in /root/httpd/...
Lint completed successfully.
--> Executing Flake8 on files found in /root/httpd/molecule/default/tests/...
Lint completed successfully.
--> Executing Ansible Lint on /root/httpd/molecule/default/playbook.yml...
Lint completed successfully.
--> Scenario: 'default'
--> Action: 'destroy'

    PLAY [Destroy] *****************************************************************

    TASK [Destroy molecule instance(s)] ********************************************
    changed: [localhost] => (item=None)

    TASK [Wait for instance(s) deletion to complete] *******************************
    failed: [localhost] (item=None) => {"attempts": 1, "censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}

    PLAY RECAP *********************************************************************
    localhost                  : ok=1    changed=1    unreachable=0    failed=1


ERROR:
An error occured during the test sequence.  Cleaning up.
--> Scenario: 'default'
--> Action: 'destroy'

    PLAY [Destroy] *****************************************************************

    TASK [Destroy molecule instance(s)] ********************************************
    changed: [localhost] => (item=None)

    TASK [Wait for instance(s) deletion to complete] *******************************
    failed: [localhost] (item=None) => {"attempts": 1, "censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}

    PLAY RECAP *********************************************************************
    localhost                  : ok=1    changed=1    unreachable=0    failed=1


ERROR:

Here's the full output with --debug.

Most helpful comment

As of March 12 2019 there is a published shim package for selinux modules: https://pypi.org/project/selinux/

pip install selinux should now fix this issue instead of copying /usr/lib/python*/site-packages/selinux to your virtual environments.

I was able to build a working molecule 2.20.1 testing environment with:

pip install testinfra==3.0.5 molecule ansible docker selinux

All 17 comments

Looks like a problem connecting to docker. If you're using linux you need to ensure docker is setup to run without sudo.

@retr0h I'm using the Centos 7 base image on Digitalocean.

Also, I'm running everything as root so it shouldn't be an issue I guess?

Where would I go about adding the user to the docker group in this case? I haven't used virtualenv before so I'm not sure if adding the user outside the env would work since docker is only installed inside the env.

Hi @chopraaa

Have you installed docker on the host used for executing the molecule test command? I see this message in the end of the log file: "msg": "Error connecting: Error while fetching server API version: ('Connection aborted.', error(2, 'No such file or directory'))" so it seems to me no docker is installed and thus no /var/run/docker.sock is available.

@dj-wasabi Thank you for the heads up. I realised I hadn't started the service. >.>

Running molecule test afterwards gives me this:

(.venv) [sammy@molecule httpd]$ molecule test
--> Validating schema /home/sammy/httpd/molecule/default/molecule.yml.
Validation completed successfully.
--> Test matrix

└── default
    ├── lint
    ├── destroy
    ├── dependency
    ├── syntax
    ├── create
    ├── prepare
    ├── converge
    ├── idempotence
    ├── side_effect
    ├── verify
    └── destroy

--> Scenario: 'default'
--> Action: 'lint'
--> Executing Yamllint on files found in /home/sammy/httpd/...
Lint completed successfully.
--> Executing Flake8 on files found in /home/sammy/httpd/molecule/default/tests/...
Lint completed successfully.
--> Executing Ansible Lint on /home/sammy/httpd/molecule/default/playbook.yml...
Lint completed successfully.
--> Scenario: 'default'
--> Action: 'destroy'

    PLAY [Destroy] *****************************************************************

    TASK [Destroy molecule instance(s)] ********************************************
    changed: [localhost] => (item=None)

    TASK [Wait for instance(s) deletion to complete] *******************************
    ok: [localhost] => (item=None)

    TASK [Delete docker network(s)] ************************************************

    PLAY RECAP *********************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0


--> Scenario: 'default'
--> Action: 'dependency'
Skipping, missing the requirements file.
--> Scenario: 'default'
--> Action: 'syntax'

    playbook: /home/sammy/httpd/molecule/default/playbook.yml

--> Scenario: 'default'
--> Action: 'create'

    PLAY [Create] ******************************************************************

    TASK [Log into a Docker registry] **********************************************
    skipping: [localhost] => (item=None)

    TASK [Create Dockerfiles from image names] *************************************
    failed: [localhost] (item=None) => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}

    PLAY RECAP *********************************************************************
    localhost                  : ok=0    changed=0    unreachable=0    failed=1


ERROR:
An error occured during the test sequence.  Cleaning up.
--> Scenario: 'default'
--> Action: 'destroy'

    PLAY [Destroy] *****************************************************************

    TASK [Destroy molecule instance(s)] ********************************************
    changed: [localhost] => (item=None)

    TASK [Wait for instance(s) deletion to complete] *******************************
    ok: [localhost] => (item=None)

    TASK [Delete docker network(s)] ************************************************

    PLAY RECAP *********************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0

Output with --debug:

(.venv) [sammy@molecule httpd]$ molecule --debug create
--> Validating schema /home/sammy/httpd/molecule/default/molecule.yml.
Validation completed successfully.
--> Test matrix

└── default
    ├── create
    └── prepare

--> Scenario: 'default'
--> Action: 'create'
DEBUG: ANSIBLE ENVIRONMENT
---
ANSIBLE_CONFIG: /home/sammy/httpd/molecule/default/.molecule/ansible.cfg
ANSIBLE_FILTER_PLUGINS: /home/sammy/.venv/lib/python2.7/site-packages/molecule/provisioner/ansible/plugins/filters:/home/sammy/httpd/molecule/default/.molecule/plugins/filters:/home/sammy/httpd/plugins/filters
ANSIBLE_LIBRARY: /home/sammy/.venv/lib/python2.7/site-packages/molecule/provisioner/ansible/plugins/libraries:/home/sammy/httpd/molecule/default/.molecule/library:/home/sammy/httpd/library
ANSIBLE_ROLES_PATH: /home/sammy/httpd/molecule/default/.molecule/roles:/home/sammy

DEBUG: MOLECULE ENVIRONMENT
---
MOLECULE_DEBUG: 'True'
MOLECULE_DEPENDENCY_NAME: galaxy
MOLECULE_DRIVER_NAME: docker
MOLECULE_EPHEMERAL_DIRECTORY: /home/sammy/httpd/molecule/default/.molecule
MOLECULE_FILE: /home/sammy/httpd/molecule/default/molecule.yml
MOLECULE_INSTANCE_CONFIG: /home/sammy/httpd/molecule/default/.molecule/instance_config.yml
MOLECULE_INVENTORY_FILE: /home/sammy/httpd/molecule/default/.molecule/ansible_inventory.yml
MOLECULE_LINT_NAME: yamllint
MOLECULE_PROVISIONER_NAME: ansible
MOLECULE_SCENARIO_DIRECTORY: /home/sammy/httpd/molecule/default
MOLECULE_SCENARIO_NAME: default
MOLECULE_VERIFIER_NAME: testinfra

DEBUG: SHELL REPLAY
ANSIBLE_CONFIG=/home/sammy/httpd/molecule/default/.molecule/ansible.cfg ANSIBLE_FILTER_PLUGINS=/home/sammy/.venv/lib/python2.7/site-packages/molecule/provisioner/ansible/plugins/filters:/home/sammy/httpd/molecule/default/.molecule/plugins/filters:/home/sammy/httpd/plugins/filters ANSIBLE_LIBRARY=/home/sammy/.venv/lib/python2.7/site-packages/molecule/provisioner/ansible/plugins/libraries:/home/sammy/httpd/molecule/default/.molecule/library:/home/sammy/httpd/library ANSIBLE_ROLES_PATH=/home/sammy/httpd/molecule/default/.molecule/roles:/home/sammy MOLECULE_DEBUG=True MOLECULE_DEPENDENCY_NAME=galaxy MOLECULE_DRIVER_NAME=docker MOLECULE_EPHEMERAL_DIRECTORY=/home/sammy/httpd/molecule/default/.molecule MOLECULE_FILE=/home/sammy/httpd/molecule/default/molecule.yml MOLECULE_INSTANCE_CONFIG=/home/sammy/httpd/molecule/default/.molecule/instance_config.yml MOLECULE_INVENTORY_FILE=/home/sammy/httpd/molecule/default/.molecule/ansible_inventory.yml MOLECULE_LINT_NAME=yamllint MOLECULE_PROVISIONER_NAME=ansible MOLECULE_SCENARIO_DIRECTORY=/home/sammy/httpd/molecule/default MOLECULE_SCENARIO_NAME=default MOLECULE_VERIFIER_NAME=testinfra

DEBUG: COMMAND
/home/sammy/.venv/bin/ansible-playbook --inventory=/home/sammy/httpd/molecule/default/.molecule/ansible_inventory.yml --diff /home/sammy/httpd/molecule/default/create.yml -vvv

    ansible-playbook 2.5.0
      config file = /home/sammy/httpd/molecule/default/.molecule/ansible.cfg
      configured module search path = [u'/home/sammy/.venv/lib/python2.7/site-packages/molecule/provisioner/ansible/plugins/libraries', u'/home/sammy/httpd/molecule/default/.molecule/library', u'/home/sammy/httpd/library']
      ansible python module location = /home/sammy/.venv/lib/python2.7/site-packages/ansible
      executable location = /home/sammy/.venv/bin/ansible-playbook
      python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
    Using /home/sammy/httpd/molecule/default/.molecule/ansible.cfg as config file
    Parsed /home/sammy/httpd/molecule/default/.molecule/ansible_inventory.yml inventory source with yaml plugin

    PLAYBOOK: create.yml ***********************************************************
    1 plays in /home/sammy/httpd/molecule/default/create.yml

    PLAY [Create] ******************************************************************
    META: ran handlers

    TASK [Log into a Docker registry] **********************************************
    task path: /home/sammy/httpd/molecule/default/create.yml:13
    skipping: [localhost] => (item={'image': u'centos:7', 'name': u'instance'})  => {
        "changed": false,
        "item": {
            "image": "centos:7",
            "name": "instance"
        },
        "skip_reason": "Conditional result was False"
    }

    TASK [Create Dockerfiles from image names] *************************************
    task path: /home/sammy/httpd/molecule/default/create.yml:25
    <127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: sammy
    <127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
    <127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683 `" && echo ansible-tmp-1522138911.41-192194340823683="` echo /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683 `" ) && sleep 0'
    Using module file /home/sammy/.venv/lib/python2.7/site-packages/ansible/modules/files/stat.py
    <127.0.0.1> PUT /home/sammy/.ansible/tmp/ansible-local-26949ABFrg3/tmpnkYqhS TO /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/stat.py
    <127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/ /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/stat.py && sleep 0'
    <127.0.0.1> EXEC /bin/sh -c '/home/sammy/.venv/bin/python2 /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/stat.py && sleep 0'
    Using module file /home/sammy/.venv/lib/python2.7/site-packages/ansible/modules/files/file.py
    <127.0.0.1> PUT /home/sammy/.ansible/tmp/ansible-local-26949ABFrg3/tmplS1aiw TO /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/file.py
    <127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/ /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/file.py && sleep 0'
    <127.0.0.1> EXEC /bin/sh -c '/home/sammy/.venv/bin/python2 /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/file.py && sleep 0'
    <127.0.0.1> PUT /home/sammy/.ansible/tmp/ansible-local-26949ABFrg3/tmpZVNBjU/Dockerfile.j2 TO /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/source
    <127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/ /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/source && sleep 0'
    Using module file /home/sammy/.venv/lib/python2.7/site-packages/ansible/modules/files/copy.py
    <127.0.0.1> PUT /home/sammy/.ansible/tmp/ansible-local-26949ABFrg3/tmppZKRTx TO /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/copy.py
    <127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/ /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/copy.py && sleep 0'
    <127.0.0.1> EXEC /bin/sh -c '/home/sammy/.venv/bin/python2 /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/copy.py && sleep 0'
    <127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/ > /dev/null 2>&1 && sleep 0'
    failed: [localhost] (item={'image': u'centos:7', 'name': u'instance'}) => {
        "changed": false,
        "checksum": "92bbda64c044759fddbb2411f0ec18cc32be7181",
        "diff": [
            {
                "after": "# Molecule managed\n\nFROM centos:7\n\nRUN if [ $(command -v apt-get) ]; then apt-get update && apt-get upgrade -y && apt-get install -y python sudo bash ca-certificates && apt-get clean; \\\n    elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python2-dnf bash && dnf clean all; \\\n    elif [ $(command -v yum) ]; then yum makecache fast && yum update -y && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \\\n    elif [ $(command -v zypper) ]; then zypper refresh && zypper update -y && zypper install -y python sudo bash python-xml && zypper clean -a; \\\n    elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; fi\n",
                "after_header": "/home/sammy/.ansible/tmp/ansible-local-26949ABFrg3/tmpZVNBjU/Dockerfile.j2",
                "before": ""
            }
        ],
        "invocation": {
            "module_args": {
                "attributes": null,
                "backup": false,
                "checksum": "92bbda64c044759fddbb2411f0ec18cc32be7181",
                "content": null,
                "delimiter": null,
                "dest": "/home/sammy/httpd/molecule/default/.molecule/Dockerfile_centos_7",
                "directory_mode": null,
                "follow": false,
                "force": true,
                "group": null,
                "local_follow": null,
                "mode": null,
                "original_basename": "Dockerfile.j2",
                "owner": null,
                "regexp": null,
                "remote_src": null,
                "selevel": null,
                "serole": null,
                "setype": null,
                "seuser": null,
                "src": "/home/sammy/.ansible/tmp/ansible-tmp-1522138911.41-192194340823683/source",
                "unsafe_writes": null,
                "validate": null
            }
        },
        "item": {
            "image": "centos:7",
            "name": "instance"
        },
        "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
    }

    PLAY RECAP *********************************************************************
    localhost                  : ok=0    changed=0    unreachable=0    failed=1


ERROR:

Using the default molecule.yml:

(.venv) [sammy@molecule httpd]$ cat molecule/default/molecule.yml
---
dependency:
  name: galaxy
driver:
  name: docker
lint:
  name: yamllint
platforms:
  - name: instance
    image: centos:7
provisioner:
  name: ansible
  lint:
    name: ansible-lint
scenario:
  name: default
verifier:
  name: testinfra
  lint:
    name: flake8

My docker version: Docker version 18.03.0-ce, build 0520e24

Fixed by copying /usr/lib64/python2.7/site-packages/selinux to .venv/lib/python2.7/site-packages/. Quite a pain just to get this running. ._.

Starting the Docker service would indeed help yes. 😄
Ok, or install the libselinux-python package with yum or apt-get would suffice as well.

@dj-wasabi Na, the host already had libselinux-python already installed. Here's a more detailed explanation of what went wrong.

I just experiment the same issue, but I have selinux disabled.

Molecule 2.12.1
Python 2.7.5
CentoOS 7
Docker 17.12
Ansible 2.4.2.0
I don't use virtualenv

I don't use virtualenv

You should

Now using it. Tried to copy file like mentioned before, but still no result

Just been hitting the selinux issue whilst using Molecule on a Fedora 28 laptop.

SELinux is set to: Permissive and the python2-libselinux package is installed.

Managed to get this working by copying the directory over as mentioned above, however I also had to copy over the .so file too.

cp -r /usr/lib64/python2.7/site-packages/selinux .venv/lib/python2.7/site-packages
cp /usr/lib64/python2.7/site-packages/_selinux.so .venv/lib/python2.7/site-packages

ty @rosstimson works well here with python3.6 too.

I also have the same issue, use "molecule --debug test", can find this error message, "Failed to import docker or docker-py - No module named docker. Try pip install docker or `pip install docker- Py", and "pip install docker" can solve it.

@zengshing Check if your user is added to the docker group.

@chopraaa I am noticing this issue on my workstation and even followed your workaround and it isn't working... the odd thing is this was working earlier, but stopped working in the middle of development.

Edit: fixed it, was apparently copying over python2.7 files instead of 3.7

I also had this problem - without using SELINUX or anything and no-sudo access for docker given.
my resolution was eventually very similar to @zengshing , but to remove docker

pip uninstall docker
pip install docker-py

As of March 12 2019 there is a published shim package for selinux modules: https://pypi.org/project/selinux/

pip install selinux should now fix this issue instead of copying /usr/lib/python*/site-packages/selinux to your virtual environments.

I was able to build a working molecule 2.20.1 testing environment with:

pip install testinfra==3.0.5 molecule ansible docker selinux

I hit this myself on Ubuntu 19.04 whilst trying to run molecule converge.

I ensured that I was in the correct group for docker first (newish system) by running:
docker run hello-world. However, the error persisted. On further investigation it is indeed SELinux related.

Versions:

ansible: 2.7.8
virtualenv: 16.7.2
python: 2.7.16
pip: pip 19.2.1 

I can confirm that the above suggestion worked for me:

pip install testinfra==3.0.5 molecule ansible docker selinux
Was this page helpful?
0 / 5 - 0 ratings

Related issues

dfinninger picture dfinninger  ·  5Comments

ssbarnea picture ssbarnea  ·  4Comments

surfer190 picture surfer190  ·  3Comments

decentral1se picture decentral1se  ·  3Comments

ssbarnea picture ssbarnea  ·  4Comments