Skip to content

Instantly share code, notes, and snippets.

@Madic-
Created March 28, 2023 19:49
Show Gist options
  • Save Madic-/72831f668e27dca7ed137fafa02c00ad to your computer and use it in GitHub Desktop.
Save Madic-/72831f668e27dca7ed137fafa02c00ad to your computer and use it in GitHub Desktop.
Ansible Molecule with systemd in docker

This gist describes how to run systemd within a container (podman or docker) created by the ansible testing framework molecule.

Folder structure

The files in this gist need to be placed into the following structure:

📂 .
├── 📂 molecule
│   ├── 📂 default
│   │   ├── 🗒 converge.yml
│   │   └── 🗒 molecule.yml
│   └── 🗒 requirements.txt
├── 🗒 ansible-lint.yml
└── 🗒 yamllint.yml

Molecule prefers running containers with podman. To configure the prefered container runtime set the environment variable MOLECULE_CONTAINERS_BACKEND=podman,docker.

Installation within a python virtual environment

python3 -m venv --copies molecule
source molecule/bin/activate
# Updating all python packages within the venv
python3 -m pip list --outdated --format=json | jq -r '.[] | "\(.name)==\(.latest_version)"' | xargs --no-run-if-empty -n1 python3 -m pip install -U
# Installing molecule and dependencies
python3 -m pip install -r molecule/requirements.txt

Building scenario

MOLECULE_CONTAINERS_BACKEND=docker molecule create
# or
MOLECULE_CONTAINERS_BACKEND=podman molecule create

Linting

molecule lint

Testing / running playbooks

molecule converge

Logging into virtual machine

molecule login -h instance-1

Idempotence test

molecule idempotence

Destroying scenario

molecule destroy

Everything in one step, without possible login

molecule test
---
parseable: true
skip_list:
- skip_ansible_lint
- "403"
- "204"
- no-handler
- empty-string-compare
- var-naming
- name[casing]
warn_list:
- risky-shell-pipe
- ignore-errors
# exclude_paths:
---
- name: Converge
hosts: all
tasks:
- name: Installing | nginx
ansible.builtin.package:
name: nginx
state: present
- name: Configuring | systemd | Enabling and starting nginx
ansible.builtin.systemd:
name: nginx
enabled: true
state: started
daemon_reload: true
---
dependency:
name: galaxy
driver:
name: containers
lint: |
set -e
yamllint -c ./yamllint.yml ./*.yml ./molecule/
ansible-lint -c ./ansible-lint.yml ./*.yml ./molecule/*/*.yml -v
platforms:
- name: instance-1
image: "docker.io/geerlingguy/docker-${MOLECULE_DISTRO:-centos8}-ansible:latest"
command: ${MOLECULE_DOCKER_COMMAND:-""}
### Docker specific begin
docker_networks:
- name: dremio
networks:
- name: dremio
### Docker specific end
### Podman specific begin
network: dremio
### Podman specific end
### Systemd preparation begin
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
tmpfs:
- /run
- /tmp
### Systemd preparation end
privileged: false
pre_build_image: true
groups:
- webserver
provisioner:
name: ansible
playbooks:
converge: ${MOLECULE_PLAYBOOK:-converge.yml}
config_options:
defaults:
interpreter_python: auto_silent
callbacks_enabled: profile_tasks, timer, yaml
connection:
pipelining: true
ssh_connection:
scp_if_ssh: true
env:
PY_COLORS: 1
inventory:
host_vars:
instance-1:
zookeeper_id: 1
postgres_port: 5433
verifier:
name: ansible
ansible-core==2.13.8
yamllint==1.30.0
molecule-plugins[docker,podman]==23.0.0
molecule==4.0.4
ansible-lint==6.14.2
paramiko==3.0.0 # https://github.com/ansible/creator-ee/issues/140
---
# Based on ansible-lint config
extends: default
# ignore: |
rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
colons:
max-spaces-after: -1
level: error
commas:
max-spaces-after: -1
level: error
comments:
require-starting-space: false
# comments: disable
comments-indentation: disable
# document-start: disable
empty-lines:
max: 3
level: error
hyphens:
level: error
# indentation: disable
key-duplicates: enable
line-length: disable
# new-line-at-end-of-file: disable
new-lines:
type: unix
# trailing-spaces: disable
truthy: disable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment