I have an ansible repo with a playbook and roles for deploying ELK stack:
.
|-elk.yml
|-roles/elasticsearch
|-roles/logstash
|-roles/kibana
I have a unit tests in each role, but additionally, I'd like to create a molecule configuration to perform integration tests (for example generting some example logs, pushing them to elasticsearch with logstash, and checking with sellenium if they're searchable in kibana) on entire stack running. How can I run entire stack in multiple containers and test if everything works? If it cannot be done, then I'd like to open a feature request for that.
The only way I can think of to do something like that would be to have one role contain a playbook.yml that invokes all the other roles. The respective molecule folder with that playbook.yml would just run include_roles.
I need to create an examples document. I had hoped linking to the scenarios tests in the docs would be sufficient, but I don't believe it is.
So, the Molecule pattern for this is.
This is a pattern we use quite frequently. Basically it just requires some understanding about ansible's role pathing. If you have your roles laid out logically, like you do above, then you simply need to tell Molecule about that path. Then your playbook can install the roles, and run a set of tests on the combined output.
Here is an example. As you see we set the ANSIBLE_ROLES_PATH.
Thanks for the advice. So finally I decided to make a molecule directory for integration tests in the main ansible repo directory. I've tried to use the ANSIBLE_ROLES_PATH, but couldn't make it work, so I added a symlink to roles:
ln -s ../../roles/ ./molecule/default/roles
This way running molecule test in the main directory executes the integration tests for the stack.
Yeah, Ansible in 2.0 did some weird stuff with pathing. ANSIBLE_ROLES_PATH is relative to the directory where inventory lives. In Molecule's case that is molecule/$scenario_name/.molecule/. So be sure to do your pathing based off that relative path.
my ansible repo is similar
|-abc.yml
|-roles/elasticsearch
|-roles/logstash
|-roles/kibana
|-group_vars/
|xyz.yml
i want to dry run / test all roles and playbooks inside my repo.
i tried setting up molecule/default/ and created sym link
molecule/default/roles -> ../../roles/
i tried executing molecule test and it always failing with yaml lint errors, for now i commented yaml lint and tried running molecule check but this does not run the roles which i sym linked. i am just learning ansible and trying to adopt molecule. please advise if am in wrong track.
lastly, some of my roles has mount commands, when i run through molecule test, it fails as the mount point is not available at the moment - can we bypass this type of known issues ?
Thank you
I don't know what else to tell you. I pointed you to a molecule.yml example to accomplish what you want. I didn't say anything about symlinks.
Started working on examples for these things.
Awesome! This is what I was looking for. Thanks, @retr0h !
Sorry for reviving an year old bug but I don't find the example from https://molecule.readthedocs.io/en/latest/examples.html as explanatory enough. After reading it still have no idea how to configure molecule for testing 2+ roles existing inside the same repository.
I would say that the defacto rule is to have more than one role inside the repository and to configure molecule individually for each of these roles creates a huge ammount of duplication, not to mention that the idea of calling molecule inside each of these folders does not sound really appealing.
If you could point to a working example where molecule is used to test two roles located under /roles in somerepo it would really be great.
Working on something similar, please provide a complete example.
Just started exploring molecule for a week now and mostly I have been trying to figure out how to use it. Documentation is quite thin and missing examples.
I agree with @ssbarnea that it's difficult to find a proper way to structure tests and avoid writing same things for each role.
Most of the ansible projects I have been working on have a lot of roles (>20) and roles can be quite small. It really is not an option to repeate all molecule files for all of those roles. And "hiding" some kind of multi-role test to some of those roles is not user friendly.
I would like to have examples of playbook level of testing that are testing all roles on single playbook.
Is it feasible to place molecule files to completely different dir e.g. test dir on top level of ansible project same way many other languages does? Why need to place molecule files into roles' folders itself as part of source code?
This is really solid feedback! Feel like create a fresh ticket to restart the conversation? Lots to be done!
Hello,
we separated the source code of Molecule and Ansible roles with tests.
Everything what it needs to be customized is in root directory of our repository.
Leave only install.yml as it is.
Just check out our auto-molecule repository.
It needs to make more improvements yet but at the end it will be very powerful add-on for Molecule.
Now it scans roles folder for all roles and play them. As bonus it fixes trailing spaces in yml files as well.
More features will be added in future. Roles takes from git repository (repositories) by Gilt.
Sorry to revive this but google likes this page.
I tried solving this with a bit of shell work:
├── _tests
│  └── molecule
│    ├── onprem
│    │  ├── Dockerfile.j2
│    │  ├── molecule.yml
│    │  ├── playbooks
│    │  │  ├── elk.yml
│    │ │  └── logstash.yml
│    │  └── tests
│    │  └── test_default.py
│    └── cloud
│    ├── Dockerfile.j2
│    ├── molecule.yml
│    ├── playbooks
│    │  └── elk.yml
│    └── tests
│    └── test_default.py
├── elasticsearch
├── logstash
└── kibana
Then in the molecule you can specify:
provisioner:
name: ansible
playbooks:
converge: playbooks/${PLAYBOOK}
env:
ANSIBLE_ROLES_PATH: ../../../../../roles/
And run molecule commands like:
cd .../roles/_tests
export PLAYBOOK=elk.yml
molecule converge -s cloud
I would call this YOLO mode. Current design is to followrole_dir/molecule/default/molecule.yaml, diverge from this layout and expect things to break.
On the other hand, feel free to use relative-symlinks to avoid duplication, they well well.
Most helpful comment
Started working on examples for these things.
https://molecule.readthedocs.io/en/latest/examples.html