134 lines
2.5 KiB
Markdown
134 lines
2.5 KiB
Markdown
# Ansible Role Development & Testing with Molecule (Podman)
|
|
|
|
This guide explains how to create an Ansible role and test it locally using [Molecule](https://molecule.readthedocs.io/) with the Podman driver.
|
|
|
|
---
|
|
|
|
## 1. Prerequisites
|
|
|
|
- [Ansible](https://docs.ansible.com/)
|
|
- [Molecule](https://molecule.readthedocs.io/)
|
|
- [Podman](https://podman.io/)
|
|
- [Python](https://www.python.org/) (with `pip`)
|
|
|
|
Install Molecule and Podman driver:
|
|
```sh
|
|
pip install molecule molecule-podman
|
|
```
|
|
|
|
---
|
|
|
|
## 2. Create a New Role
|
|
|
|
Navigate to your roles directory and create a new role:
|
|
```sh
|
|
cd ansible/roles
|
|
ansible-galaxy init myrole
|
|
cd myrole
|
|
```
|
|
|
|
---
|
|
|
|
## 3. Initialize Molecule with Podman
|
|
|
|
```sh
|
|
molecule init scenario default -d podman
|
|
```
|
|
|
|
---
|
|
|
|
## 4. Edit Molecule Configuration
|
|
|
|
Edit `molecule/default/molecule.yml` to set your platform and image, for example:
|
|
```yaml
|
|
---
|
|
dependency:
|
|
name: galaxy
|
|
driver:
|
|
name: podman
|
|
platforms:
|
|
- name: instance
|
|
image: ubuntu:20.04
|
|
privileged: true
|
|
provisioner:
|
|
name: ansible
|
|
options:
|
|
vvv: true
|
|
env:
|
|
ANSIBLE_ROLES_PATH: ../../../
|
|
verifier:
|
|
name: ansible
|
|
|
|
```
|
|
|
|
---
|
|
|
|
## 5. Write Your Role Tasks
|
|
|
|
Edit `tasks/main.yml` to add your automation logic.
|
|
|
|
---
|
|
|
|
## 6. Configure Molecule to Use Your Role
|
|
|
|
Edit `molecule/default/converge.yml`:
|
|
```yaml
|
|
---
|
|
- name: Converge
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Include role
|
|
ansible.builtin.include_role:
|
|
name: myrole
|
|
```
|
|
|
|
---
|
|
|
|
## 6. Configure preparation steps (required for python and sudo)
|
|
|
|
```yaml
|
|
---
|
|
---
|
|
- name: Prepare
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Update cache # noqa no-changed-when
|
|
ansible.builtin.raw: apt update
|
|
|
|
- name: Install required packages # noqa no-changed-when
|
|
ansible.builtin.raw: apt install -y python3 sudo
|
|
|
|
### Insert here other preparation steps
|
|
````
|
|
|
|
---
|
|
|
|
## 8. Run Molecule Tests
|
|
|
|
From the root of your role directory, run:
|
|
```sh
|
|
molecule test
|
|
```
|
|
|
|
This will:
|
|
- Build the container
|
|
- Apply your role
|
|
- Run verification steps
|
|
- Destroy the container
|
|
|
|
---
|
|
|
|
## 8. Notes
|
|
|
|
- **Systemd:** Most Podman containers do not support `systemd` by default. Use `when: ansible_service_mgr == "systemd"` to skip service tasks in containers.
|
|
- **Role Path:** Always run Molecule commands from the root of your role directory.
|
|
|
|
---
|
|
|
|
## References
|
|
|
|
- [Molecule Documentation](https://molecule.readthedocs.io/)
|
|
- [Podman Documentation](https://podman.io/)
|
|
- [Ansible Role Best Practices](https://docs.ansible.com/ansible/latest/dev_guide/developing_roles.html) |