Rapidly Build & Test Ansible Roles with Molecule + Docker

Molecule is a tool originally developed by retr0h and now maintained by Ansible/Red Hat that automates testing of Ansible roles. Instead of a highly manual process, testing a role against any number of distros is as simple as running molecule test. Molecule has many drivers for testing your role on different kinds of infrastructure such as Docker, Vagrant and EC2. I suggest using the Docker driver since spinning up and tearing down instances is orders of magnitude faster than other drivers.

In this tutorial video I build a super simple redis role to show how you can use Molecule for both development and testing.

– 1:09 — Setup & Get Started
– 2:26 — Empty Molecule Project
– 4:05 — Test the New Role
– 7:08 — Role Development Workflow
– 8:49 — Running on Multiple Platforms
– 10:02 — Start Developing Redis Role
– 11:49 — Write Your First Test
– 14:07 — Manage Services on Docker
– 16:07 — Converge On New Images
– 16:49 — Test Services Are Started
– 17:50 — Using a Non-Root User


✅ Jeff Geerling’s Images on Docker Hub == https://hub.docker.com/u/geerlingguy
✅ My Images on Docker Hub == https://hub.docker.com/u/percygrunwald
✅ Molecule on GitHub == https://github.com/ansible/molecule/
✅ TestInfra on ReadTheDocs == https://testinfra.readthedocs.io/en/latest/


✅ Visual Studio Code (https://code.visualstudio.com/)
✅ Ansible extension by Microsoft (http://bit.ly/2SdnqoK).

Thanks for watching and hope it’s entertaining/useful for you! Hit me up in the comments if you have any questions or feedback 💪.

Related & Channel
▶️ http://bit.ly/2VWid3D == Playlist of all Ansible videos
⭐️ http://bit.ly/2HavqCu == Subscribe to our YouTube Channel

Our Site & Socials
🌐 http://bit.ly/2shtKwZ == TopTechSkills.com
🐦 http://bit.ly/2RkgZkk == @TopTechSkills on Twitter
🐦 http://bit.ly/2AA19aN == @PercyGrunwald on Twitter
😃 http://bit.ly/2ACQgEW == @TopTechSkills on Facebook
🕹 http://bit.ly/2CWR3ST == @PercyGrunwald on Twitch


39 thoughts on “Rapidly Build & Test Ansible Roles with Molecule + Docker”
  1. Nice intro! But how do you manage SystemD services in rootless containers within Molecule?

  2. Does molecule works ok with mitogen? I know the connection is local to your docker container but as you may know ansible creates and kills the connection for each task, instead mitogen changes this behaviour to one single stable connection per each target. So when testing multiple tasks/hosts this might speed up the process

  3. Can you update this Video the driver-name docker is not supported anymore.

    Error: Invalid value for "–driver-name" / "-d": invalid choice: docker. (choose from delegated)

    But awesome Video. I liked it very much!!

  4. Do you plan to update this tutorial? because the steps does not work anymore on the latest molecule version (molecule 3.1.5) 🙁

  5. Thanks for the great video! I would be interested to know how far you can push this? Would it be possible to test a shell script with it? I have one that calls local ansilbe playbooks.

  6. hello, thanks for this great video. Could you please fork and add geerlingguy centos8 and debian10 docker image for non root user ? thanks

  7. I'm running Ansible on Amazon-linux2 and roles under it, I installed molecule using pip and docker-py as well. When I say molecule test it's giving me an error saying some apiversion error, can anyone pls help

  8. I have a problem with molecule test…. Please help, thinks —


    name: galaxy


    name: docker


    name: yamllint


    image: centos:7

    name: instance



    name: ansible-lint

    name: ansible


    name: default



    name: flake8

    name: testinfra

    ERROR: Failed to pre-validate.

    {'lint': ['must be of string type'], 'platforms': ['must be of list type']}

  9. Hi Percy,hope you are doing great
    Just wanted to know is that mitogen is a open source tools or do we have to pay for it

  10. Hey Percy! I'm just now learning molecule myself, and I realized that since making this video, they've released Molecule 3! Some of the syntax and things are different, I would LOVE to see a video that shows how to get started in the new version. Thanks!

  11. well explained and I appreciate you breaking down the steps, helps demystify Molecule and give me a better gist and outline of what's happening

  12. Hi, how to run an integration test with Molecule? let's say I have a role that setup database replication and I need one container run as a master DB before my molecule test run?.

  13. I have following error running
    molecule test
    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}
    fatal: [localhost]: FAILED! => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}
    Any idea?
    Thank you.

  14. I seem to get the following FAILED error. Is there a way to stop this from happening?

    TASK [Wait for instance(s) creation to complete] *****************************

        FAILED – RETRYING: Wait for instance(s) creation to complete (300 retries left).

        changed: [localhost] => (item=None)

        changed: [localhost] => (item=None)

        changed: [localhost]

  15. sir any videos like this for ansible test kitchen? how to write test cases for my roles using test kitchen .

  16. The video is great, but I'm just not sure if I can forgive the absence of a night theme in VScode.

  17. Hi Percy. What is the best place to include ansible script which are part of a git project? Meaning I have a code base whether its scripts of compiled code and I want to install this after building. So Project123 has my code. Where would I include Project1234-ansible.yml? Should that go into the Project123 repo or some other outside project?

  18. I’ve tried molecule but can’t figure out a way to use it for my job. Basically our software suite is automation of building a suite of virtual machines. We use VMware to create these vms. So I would need molecule to not create a docker image but instead run our build vm playbook. Do you know how this would work?

  19. I found that setting up molecule outside of virtualenv is very buggy. When I use virtualenv, everything works as expected.

  20. How do I write tests if I have different tasks for different instances ? Just one testing file for all instances ? If yes, how to define the testings for each instance in the testing file ?

Leave a Reply

Your email address will not be published.

Captcha loading...