2

I have a lot of roles that I want to test via molecule, and I'm a molecule beginner.

If I create a molecule scenario in each role, there will be significant duplicated code.

Is there a pattern that people use to avoid have many roles with same Dockerfile.j2, molecule.yml, etc.

Perhaps some kind of composition (through dependency injection) or if that's not feasible some kinda inheritance?

$ molecule init role -r stackoverflow
--> Initializing new role stackoverflow...
Initialized role in /private/tmp/stackoverflow successfully.

$ tree stackoverflow
stackoverflow
├── README.md
├── defaults
│   └── main.yml
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── molecule
│   └── default
│       ├── Dockerfile.j2
│       ├── INSTALL.rst
│       ├── molecule.yml
│       ├── playbook.yml
│       └── tests
│           ├── test_default.py
│           └── test_default.pyc
├── tasks
│   └── main.yml
└── vars
    └── main.yml

8 directories, 12 files
k1eran
  • 4,492
  • 8
  • 50
  • 73

1 Answers1

3

In the molecule.yml file, you can specify from where to fetch Dockerfile.js. So, you can have a single docker configuration file in the parent folder and share it among all the roles. For example:

driver:
  name: docker
platforms:
  - name: instance
    image: ubuntu:18.04
    dockerfile: ../common/Dockerfile.j2

More info on the Docker configuration for Molecule on the official documentation.

You can also share files like create.yml, destroy.yml or prepare.yml among roles, and specify the right path in molecule.yml.

provisioner:
  name: ansible
  playbooks:
    create: ../common/create.yml
    destroy: ../common/destroy.yml
    converge: playbook.yml

More info on the Ansible configuration for Molecule on the official documentation.

Furthermore, you can share playbooks and tests among different scenarios for the same role.

verifier:
name: testinfra
directory: ../resources/tests/
lint:
  name: flake8

More info on sharing across scenarios on the official documentation.

I'm not aware of any straightforward mechanism to share a molecule.yml file across different roles. It would be useful though.

Thomas Vitale
  • 1,588
  • 10
  • 13