'Unable to override role name with meta/main.yml role_name for molecule test
I created an example role project with this command:
molecule init role cranberry --driver-name docker
If you pull it:
git clone [email protected]:natemarks/cranberry.git
and run it:
make clean-venv && make molecule-test
It should set up the virtualenv and run the test successfully because it uses the project directory name 'cranberry' as the role name. If I want to override that so that my role_name is still 'cranberry' but my project directory is 'role-cranberry'. I should be able to just rename/move the project directory to 'role-cranberry' and set meta/main.yaml role_name: cranberry. This doesn't work.
similarly , I should be able to break the test without changing the project directory by just adding some garbage value to meta/main.yml role_name,like;
galaxy_info:
role_name: badrole
but that doesn't work either.
I think I'm using the latest python packages. Thanks in advance for tips
Solution 1:[1]
You are mixing up two things: the repository name in which the role is versioned remotely and the directory name in which the role is stored on your disk and which makes-up the name by which ansible knows that role on your local machine.
The role_name in your metadata only gives an indication to https://galaxy.ansible.com (i.e. the online service) of which name it should use when importing it. That name will be used later if you install the role from the ansible-galaxy command line (e.g. ansible-galaxy role install geerlingguy.security).
When molecule runs from your current role dir, it adds the current directory to the ANSIBLE_ROLES_PATH. From there, ansible is simply doing a normal role lookup and will know your current role after its containing directory name.
You gave as an example a role and its molecule test security by Jeff Geerling. The git repository name is ansible-role-security.
- If you do a basic clone (
git clone https://github.com/geerlingguy/ansible-role-security) and run the test from the freshly cloned directory (cd ansible-role-security && molecule test), it will fail complaining thatgeerlingguy.securitywas not found. - If you rename the directory to the expected name (
cd .. && mv ansible-role-security geerlinguy.security && cd geerlinguy.security && molecule test), it will now succeed.
Note that the best practice to import such a role is to install it through ansible galaxy either by
- installing it from the galaxy web site
ansible-galaxy role install geerlingguy.security - installing it from a git repo using a requirement file
roles/requirements.yml- src: https://github.com/geerlingguy/ansible-role-security scm: git version: main name: geerlingguy.security- Install role keeping the scm metadata (to interact with git, commit...)
ansible-galaxy role install -fgr roles/requirements.yaml
To finish with, you can have a look at the ci-pipeline in the same example repository where you will see that the author has forced the working directory name as well as the git clone directory target to align the directory name to what is expected for running the tests.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
