Search code examples
ansiblevirtualenvpipenv

ansible not finding roles when installed using pipenv


I have been looking at different ways to test ansible playbooks and came across this blog post by Jeff Geerling.

Replicating his exact example works, however, when I replicate it but managing python (and ansible) via a pipenv install I get the following error

ERROR! the role 'ansible-test' was not found in /Users/philip/Desktop/ansible-test/tests/roles:/Users/philip/ansible-test:/Users/philip/Desktop/ansible-test/tests

The error appears to have been in '/Users/philip/Desktop/ansible-test/tests/test.yml': line 4, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  roles:
    - ansible-test
      ^ here

The content of test.yml is

- hosts: localhost
  remote_user: root
  roles:
    - ansible-test

which passes yamllint fine.

ansible.cfg contains

[defaults]
roles_path = ../../ansible-test

I'm guessing it's something to do with being in a Virtualenv, but can't explain it.


Solution

  • The roles path is the path for all roles.

    Ansible searches for a directory with the name of the role in the roles_path. If the role is called ansible-test, it is an error to put the name of the role into the roles_path.

    Set roles_path this way:

    [defaults]
    roles_path = ../..
    

    And make sure, that you are in the right directory, when you call ansible-playbook.