{"id":15140774,"url":"https://github.com/geerlingguy/molecule-playbook-testing","last_synced_at":"2025-10-23T17:31:38.842Z","repository":{"id":43422183,"uuid":"263673523","full_name":"geerlingguy/molecule-playbook-testing","owner":"geerlingguy","description":"This is an example from the Ansible 101 livestream","archived":false,"fork":false,"pushed_at":"2022-08-24T14:24:14.000Z","size":15,"stargazers_count":74,"open_issues_count":3,"forks_count":40,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-15T22:35:11.464Z","etag":null,"topics":["ansible","ansible-101","example","github-actions","molecule"],"latest_commit_sha":null,"homepage":"https://www.jeffgeerling.com/blog/2020/ansible-101-jeff-geerling-youtube-streaming-series","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/geerlingguy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-13T15:44:06.000Z","updated_at":"2024-11-09T08:30:18.000Z","dependencies_parsed_at":"2022-07-11T03:33:05.947Z","dependency_job_id":null,"html_url":"https://github.com/geerlingguy/molecule-playbook-testing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geerlingguy%2Fmolecule-playbook-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geerlingguy%2Fmolecule-playbook-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geerlingguy%2Fmolecule-playbook-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geerlingguy%2Fmolecule-playbook-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/geerlingguy","download_url":"https://codeload.github.com/geerlingguy/molecule-playbook-testing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237869057,"owners_count":19379257,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansible","ansible-101","example","github-actions","molecule"],"created_at":"2024-09-26T08:41:04.641Z","updated_at":"2025-10-23T17:31:38.495Z","avatar_url":"https://github.com/geerlingguy.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ansible 101 - Molecule playbook testing\n\n\u003e _This example is derived from Chapter 12 of [Ansible for DevOps](https://www.ansiblefordevops.com), version 1.23_\n\nThis playbook installs Apache and should work on the latest versions of Red Hat-based and Debian-based OSes, at a minimum:\n\n  - CentOS 8\n  - Debian 10\n\nCreate a new molecule scenario:\n\n    molecule init scenario\n\nEdit the `converge.yml` playbook:\n\n```yaml\n---\n- name: Converge\n  hosts: all\n\n  tasks:\n    - name: Update apt cache (on Debian).\n      apt:\n        update_cache: true\n        cache_valid_time: 3600\n      when: ansible_os_family == 'Debian'\n\n- import_playbook: ../../main.yml\n```\n\nThen run:\n\n    molecule converge\n\nUh oh... something failed. Log in and check it out:\n\n```\n$ molecule login\n[root@instance /]# systemctl status httpd\nFailed to get D-Bus connection: Operation not permitted\n```\n\nYikes, I can't take this entire episode to explain the details of what's going on, but at a basic level, Molecule's default configuration sets up a container and runs the command:\n\n    bash -c \"while true; do sleep 10000; done\"\n\n(use `docker ps` to confirm that.)\n\nUnfortunately, this means the process that's running in this container is _not_ systemd's init system, meaning you can't use systemd to manage services like Apache.\n\nIn normal container usage, this is perfectly fine—running an init system in a container is kind of an anti-best-practice.\n\nBut in our case, we're using containers for testing. And lucky for you, I maintain a large set of Ansible testing containers that have systemd built in and ready to go! So for these tests, since we want to make sure Ansible can manage the Apache service correctly, we need to change a few things in Molecule's `molecule.yml` configuration.\n\nGo ahead and exit and destroy the environment.\n\n```\n[root@instance /]# exit\n$ molecule destroy\n```\n\nNow edit the `platforms` in `molecule.yml`:\n\n```yaml\nplatforms:\n  - name: instance\n    image: \"geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest\"\n    command: \"\"\n    volumes:\n      - /sys/fs/cgroup:/sys/fs/cgroup:ro\n    privileged: true\n    pre_build_image: true\n```\n\nNow try running it again:\n\n    molecule test\n\nYay, it works!\n\nNow, since we added an environment variable, `MOLECULE_DISTRO`, we can substitute whatever distro we want there, assuming I maintain a Docker image for that distro:\n\n    MOLECULE_DISTRO=debian10 molecule test\n\nYay, still works!\n\n## Verify with Molecule\n\nNow add to `verify.yml`:\n\n```yaml\n---\n- name: Verify\n  hosts: all\n\n  tasks:\n  - name: Verify Apache is serving web requests.\n    uri:\n      url: http://localhost/\n      status_code: 200\n```\n\nYou can just run the verify playbook with `molecule verify`.\n\n## Add lint configuration\n\nAssuming you have `yamllint` and `ansible-lint` installed, you can configure Molecule to run them in the `lint` build stage:\n\n```yaml\nlint: |\n  set -e\n  yamllint .\n  ansible-lint\n```\n\nThen run `molecule lint`. It doesn't even need to build an environment to do the linting.\n\n## GitHub Actions Testing\n\nGo ahead and slap this repo up on GitHub. Do it!\n\nNow add a `.github/workflows` folder.\n\nAdd a `ci.yml` workflow file.\n\nHere's the whole workflow we're targeting:\n\n```yaml\n---\nname: CI\n'on':\n  pull_request:\n  push:\n    branches:\n      - master\n\njobs:\n\n  test:\n    name: Molecule\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        distro:\n          - centos8\n          - debian10\n\n    steps:\n      - name: Check out the codebase.\n        uses: actions/checkout@v2\n\n      - name: Set up Python 3.\n        uses: actions/setup-python@v2\n        with:\n          python-version: '3.x'\n\n      - name: Install test dependencies.\n        run: pip3 install molecule docker yamllint ansible-lint\n\n      - name: Run Molecule tests.\n        run: molecule test\n        env:\n          PY_COLORS: '1'\n          ANSIBLE_FORCE_COLOR: '1'\n          MOLECULE_DISTRO: ${{ matrix.distro }}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeerlingguy%2Fmolecule-playbook-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeerlingguy%2Fmolecule-playbook-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeerlingguy%2Fmolecule-playbook-testing/lists"}