Search code examples
ansiblemolecule

Molecule - test roles from other directory


I want to test my roles which I have in other directory. Below my project structure: Project Structure

When I try use molecule, it can't find roles which are in roles directory.

❯ sudo molecule converge
--> Test matrix

└── default
    ├── dependency
    ├── create
    ├── prepare
    └── converge

--> Scenario: 'default'
--> Action: 'dependency'
Skipping, missing the requirements file.
Skipping, missing the requirements file.
--> Scenario: 'default'
--> Action: 'create'
Skipping, instances already created.
--> Scenario: 'default'
--> Action: 'prepare'
Skipping, prepare playbook not configured.
--> Scenario: 'default'
--> Action: 'converge'
--> Sanity checks: 'docker'
ERROR! the role 'curl' was not found in /home/belluu/programming/Ansible-Posthog/molecule/default/roles:/root/.cache/molecule/Ansible-Posthog/default/roles:/home/belluu/programming:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/home/belluu/programming/Ansible-Posthog/molecule/default

The error appears to be in '/home/belluu/programming/Ansible-Posthog/molecule/default/converge.yml': line 5, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  roles:
    - role: curl
      ^ here

Molecule trying find right directory but without success. Is it possible to give him path to directory with roles?


Solution

  • Simple answer: Yes

    In your molecule.yml file add the following lines under env::

    provisioner:
      name: ansible
      ...
      env:
        ANSIBLE_ROLES_PATH: "../../roles"
    

    Additionally, you can link Molecule to other "paths" as well:

    provisioner:
      name: ansible
      ...
      env:
        ANSIBLE_CONFIG: ../../ansible.cfg
      inventory:
        links:
          group_vars: ../../inventory/group_vars
          host_vars: ../../inventory/host_vars