When I first started using [ansible](https://www.ansible.com/), I did not know about [molecule](https://molecule.readthedocs.io/en/latest/). It was a bit daunting to start a _role_ from scratch and trying to develop it without having the ability to test it. Then a co-worker of mine told me about molecule and everything changed.
<!--more-->
I do not have any of the tools I need installed on this machine, so I will go through, step by step, how I set up ansible and molecule on any new machine I come across for writing ansible roles.
## Requirements {#requirements}
What we are trying to achieve in this post, is a working ansible role that can be tested inside a docker container. To be able to achieve that, we need to install docker on the system. Follow the instructions on [installing docker](https://docs.docker.com/install/) found on the docker website.
## Good Practices {#good-practices}
First thing's first. Let's start by making sure that we have python installed properly on the system.
```text
$ python --version
Python 3.7.1
```
Because in this case I have _python3_ installed, I can create a _virtualenv_ easier without the use of external tools.
## Creating your first ansible role {#creating-your-first-ansible-role}
Once all the steps above are complete, we can start by creating our first ansible role.
```text
$ molecule init role -r example-role
--> Initializing new role example-role...
Initialized role in /home/elijah/sandbox/test-roles/example-role successfully.
$ tree example-role/
example-role/
├── defaults
│ └── main.yml
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── molecule
│ └── default
│ ├── Dockerfile.j2
│ ├── INSTALL.rst
│ ├── molecule.yml
│ ├── playbook.yml
│ └── tests
│ ├── __pycache__
│ │ └── test_default.cpython-37.pyc
│ └── test_default.py
├── README.md
├── tasks
│ └── main.yml
└── vars
└── main.yml
9 directories, 12 files
```
You can find what each directory is for and how ansible works by visiting [docs.ansible.com](https://docs.ansible.com).
### `meta/main.yml` {#meta-main-dot-yml}
The meta file needs to modified and filled with information about the role. This is not a required file to modify if you are keeping this for yourself, for example. But it is a good idea to have as much information as possible if this is going to be released. In my case, I don't need any fanciness as this is just sample code.
```yaml
---
galaxy_info:
author: Elia el Lazkani
description: This is an example ansible role to showcase molecule at work
license: license (BDS-2)
min_ansible_version: 2.7
galaxy_tags: []
dependencies: []
```
### `tasks/main.yml` {#tasks-main-dot-yml}
This is where the magic is set in motion. Tasks are the smallest entities in a role that do small and idempotent actions. Let's write a few simple tasks to create a user and install a service.
```yaml
---
# Create the user example
- name: Create 'example' user
user:
name: example
comment: Example user
shell: /bin/bash
state: present
create_home: yes
home: /home/example
# Install nginx
- name: Install nginx
apt:
name: nginx
state: present
update_cache: yes
notify: Restart nginx
```
### `handlers/main.yml` {#handlers-main-dot-yml}
If you noticed, we are notifying a handler to be called after installing _nginx_. All handlers notified will run after all the tasks complete and each handler will only run once. This is a good way to make sure that you don't restart _nginx_ multiple times if you call the handler more than once.
It's time to configure molecule to do what we need. We need to start an ubuntu docker container, so we need to specify that in the molecule YAML file. All we need to do is change the image line to specify that we want an `ubuntu:bionic` image.
PLAY RECAP *********************************************************************
instance : ok=4 changed=3 unreachable=0 failed=0
```
It looks like the **converge** step succeeded.
## Writing Tests {#writing-tests}
It is always a good practice to write unittests when you're writing code. Ansible roles should not be an exception. Molecule offers a way to run tests, which you can think of as unittest, to make sure that what the role gives you is what you were expecting. This helps future development of the role and keeps you from falling in previously solved traps.
Molecule leverages the [testinfra](https://testinfra.readthedocs.io/en/latest/) project to run its tests. You can use other tools if you so wish, and there are many. In this example we will be using _testinfra_.
PLAY RECAP *********************************************************************
localhost : ok=2 changed=2 unreachable=0 failed=0
```
I have a few warning messages (that's likely because I am using _python 3.7_ and some of the libraries still don't fully support the new standards released with it) but all my tests passed.
## Conclusion {#conclusion}
Molecule is a great tool to test ansible roles quickly and while developing
them. It also comes bundled with a bunch of other features from different
projects that will test all aspects of your ansible code. I suggest you start