Ansible Automation Ansible Testing with Molecule 1 — Questions and Answers
Question 1: What is Ansible Molecule used for?
- Testing Ansible roles and collections by automatically provisioning test instances, running playbooks, and verifying the resulting state (Correct answer)
- Monitoring Ansible playbook performance metrics
- Managing molecule-based chemical process automation with Ansible
- Converting Ansible playbooks to Terraform configurations
Correct answer: Testing Ansible roles and collections by automatically provisioning test instances, running playbooks, and verifying the resulting state
Molecule provides a structured framework for testing Ansible roles across multiple platforms and scenarios, enabling TDD for infrastructure code.
Ansible Molecule is the official testing framework for Ansible roles and collections. Its workflow: 1. Creates test infrastructure (VMs, containers, cloud instances) 2. Runs the role's converge playbook to apply the role 3. Optionally runs idempotency checks (runs converge twice to verify no changes on second run) 4. Executes verify playbooks to assert the resulting system state 5. Destroys test infrastructure Molecule supports multiple drivers (Docker, Podman, VirtualBox, EC2, Azure, etc.) and verifiers (Ansible, Testinfra, Goss). It integrates with CI/CD pipelines for automated role testing.
Question 2: What is the default driver used by Molecule when initializing a new scenario?
- Docker (Correct answer)
- Vagrant
- VirtualBox
- AWS EC2
Correct answer: Docker
Docker is Molecule's default driver because containers start quickly and are lightweight, making them ideal for rapid test iteration.
Molecule's default driver is Docker, chosen because: - Containers start in seconds (vs minutes for VMs) - Lightweight resource usage - Reproducible environments - Easy cleanup (just remove the container) - Wide availability in CI/CD systems The Docker driver requires the Docker daemon and the 'molecule-plugins' package with Docker support. For testing roles that require systemd (service management), containers must be run in privileged mode with systemd as the init system. Alternative drivers include Podman (rootless container testing), Vagrant (for full VM testing), and cloud drivers for production-like testing.
Question 3: What command initializes a new Molecule scenario inside an existing Ansible role?
- molecule init scenario (Correct answer)
- molecule new scenario
- molecule create scenario
- molecule setup scenario
Correct answer: molecule init scenario
'molecule init scenario' creates the molecule/ directory structure with default configuration files for a new test scenario.
'molecule init scenario' creates the Molecule scaffolding: $ cd roles/myrole $ molecule init scenario This creates: molecule/ default/ molecule.yml # Scenario configuration (driver, platforms) converge.yml # Playbook that applies the role under test verify.yml # Assertions to verify correct state requirements.yml # Collections/roles needed for testing 'default' is the scenario name. Multiple scenarios can test different platforms or configurations: molecule init scenario --scenario-name centos8 molecule init scenario --scenario-name ubuntu22
Question 4: What does 'molecule converge' do?
- It applies the role to the test instance by running the converge.yml playbook, without destroying the instance afterward (Correct answer)
- It creates test instances and runs all Molecule phases including verify and destroy
- It merges multiple Molecule scenarios into a single test run
- It synchronizes the role's code from source control to test instances
Correct answer: It applies the role to the test instance by running the converge.yml playbook, without destroying the instance afterward
'molecule converge' applies the role to existing test instances, leaving them running for iterative development and debugging.
'molecule converge' is the core development command: 1. Creates instances if they don't exist (calls 'molecule create' first) 2. Runs 'molecule prepare' if a prepare.yml exists 3. Runs converge.yml (which includes/applies your role) 4. Does NOT run verify or destroy This is the primary command during role development: - Edit role code - Run 'molecule converge' to apply changes - Check results (or run 'molecule verify') - Repeat 'molecule converge' is faster than 'molecule test' because it skips create/destroy on each run. When done, manually run 'molecule destroy' to clean up.
Question 5: What is the difference between 'molecule test' and 'molecule converge'?
- 'molecule test' runs the full lifecycle (create→converge→verify→destroy) for CI; 'molecule converge' only applies the role for interactive development (Correct answer)
- 'molecule test' checks syntax only; 'molecule converge' applies the role
- 'molecule test' destroys instances before and after; 'molecule converge' preserves them permanently
- They are identical commands with different names
Correct answer: 'molecule test' runs the full lifecycle (create→converge→verify→destroy) for CI; 'molecule converge' only applies the role for interactive development
'molecule test' is for CI pipelines (full ephemeral lifecycle); 'molecule converge' is for development (persistent instances for iteration).
Full lifecycle comparison: molecule test (CI/CD): 1. dependency → syntax → create → prepare → converge 2. idempotency check (converge again, assert no changes) 3. verify → destroy Always creates fresh instances; always destroys on completion. molecule converge (development): 1. create (if not exists) → prepare (if not exists) → converge Leaves instances running for inspection and re-testing. For CI pipelines, always use 'molecule test'. For local development, use 'molecule converge' + 'molecule verify' + 'molecule destroy' when finished.
Question 6: Which Molecule verifier uses Python test code with assertions about the remote system state?
- Testinfra (via pytest) (Correct answer)
- Ansible (built-in default verifier)
- Goss
- ServerSpec
Correct answer: Testinfra (via pytest)
Testinfra (integrated with pytest) allows writing Python assertions to verify file contents, service states, packages, users, and more on test instances.
Testinfra is a Python testing library (built on pytest) for infrastructure verification: # tests/test_default.py def test_nginx_is_installed(host): nginx = host.package('nginx') assert nginx.is_installed assert nginx.version.startswith('1.18') def test_nginx_running_and_enabled(host): nginx = host.service('nginx') assert nginx.is_running assert nginx.is_enabled def test_nginx_config(host): f = host.file('/etc/nginx/nginx.conf') assert f.exists assert f.contains('worker_processes') Testinfra connects to instances via SSH or Docker. The Ansible verifier (default) uses Ansible tasks for assertions, which is simpler for Ansible-focused teams.
What is Ansible Molecule used for?