{"id":27442391,"url":"https://github.com/drpsychick/ansible-testing","last_synced_at":"2025-07-16T16:33:47.928Z","repository":{"id":39732351,"uuid":"507144631","full_name":"DrPsychick/ansible-testing","owner":"DrPsychick","description":"An Ansible role that allows creating SystemD instances for testing Ansible roles against - for use with molecule","archived":false,"fork":false,"pushed_at":"2024-10-28T00:12:01.000Z","size":56,"stargazers_count":14,"open_issues_count":2,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-28T01:43:15.934Z","etag":null,"topics":["ansible","ansible-role","centos","fedora","molecule","systemd","testing","ubuntu","windows"],"latest_commit_sha":null,"homepage":"","language":"PowerShell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DrPsychick.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-24T21:09:07.000Z","updated_at":"2024-10-28T00:05:45.000Z","dependencies_parsed_at":"2024-09-15T03:30:12.044Z","dependency_job_id":null,"html_url":"https://github.com/DrPsychick/ansible-testing","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrPsychick%2Fansible-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrPsychick%2Fansible-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrPsychick%2Fansible-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DrPsychick%2Fansible-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DrPsychick","download_url":"https://codeload.github.com/DrPsychick/ansible-testing/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248982954,"owners_count":21193491,"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-role","centos","fedora","molecule","systemd","testing","ubuntu","windows"],"created_at":"2025-04-15T00:27:15.489Z","updated_at":"2025-04-15T00:27:16.130Z","avatar_url":"https://github.com/DrPsychick.png","language":"PowerShell","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=FTXDN7LCDWUEA\u0026source=url","https://github.com/sponsors/DrPsychick"],"categories":[],"sub_categories":[],"readme":"# Ansible testing\n[![Build Status](https://img.shields.io/github/actions/workflow/status/DrPsychick/ansible-testing/ci.yml\n)](https://github.com/DrPsychick/ansible-testing/actions/workflows/ci.yml)\n[![license](https://img.shields.io/github/license/drpsychick/ansible-testing.svg)](https://github.com/drpsychick/ansible-testing/blob/main/LICENSE)\n[![Paypal](https://img.shields.io/badge/donate-paypal-00457c.svg?logo=paypal)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=FTXDN7LCDWUEA\u0026source=url)\n[![GitHub Sponsor](https://img.shields.io/badge/github-sponsor-blue?logo=github)](https://github.com/sponsors/DrPsychick)\n\nCreates fully functional SystemD docker containers which you can use to test your Ansible roles with. \nWith `libvirt` you can also automatically provision Windows virtual machines to test your roles with `WinRM`.\nIntended to be run locally on a Linux box with Docker (and optionally libvirt) installed.\n\n## Configuration\nCheck [defaults/main.yml](defaults/main.yml) see how to define containers and adjust it to your needs.\n* Define your own `work_dir`. The contents are temporary and will be removed with `destroy`!\n* Define the containers you want to spin up.\n\n## What it does (in a nutshell)\nThis role has `create` and `destroy` tasks for containers and virtual machines. It loops over the configured lists \n(`containers` or `virtual_machines` in `vars.yml`) and creates the instances accordingly. \nEach Molecule scenario must either use containers **or** virtual machines, because Molecule only supports a single driver per scenario.\n\nLinux: It creates privileged docker containers that are started with `/sbin/init` to have a fully SystemD capable instance.\n\nWindows: It creates the VM (unattended install of Windows) and configures WinRM for Ansible (plain HTTP) upon first start.\n\n**Hint:** Creating Windows VMs is expensive and takes over 10 Minutes.\n\n# Contributing\nIf you have other systems you want to test, feel free to provide a PR with additional Dockerfiles or libvirt configuration.\n\nContributing is really easy:\n1. Fork the project on GitHub : https://github.com/DrPsychick/ansible-testing\n1. Checkout the fork on your Linux box\n1. Symlink the fork in your roles Molecule scenario (i.e. `./molecule/default/`)\n1. Make changes and test your role with them until you're happy - commit and create a pull-request\n1. You can run GitHub Actions locally for fast feedback with `act`: https://nektosact.com/installation/index.html\n\n```shell\nGitHubName=YourName\nYourRoleDir=/This/Is/Your/Role/Directory/MyRole\nYourRoleName=MyRole\nWhereYourForkIs=/This/Is/Where/You/Clone/Your/Fork\n\n# clone your fork\ncd $WhereYourForkIs\ngit clone https://github.com/$GitHubName/ansible-testing.git\n\n# symlink your local version in your molecule scenario\ncd $YourRoleDir/molecule/default\nln -s $WhereYourForkIs/ansible-testing drpsychick.ansible_testing\n\n# comment out role in requirements and delete downloaded version\nsed -i -e 's/^ /# /' requirements.yml\nrm -rf ~/.cache/molecule/$YourRoleName/default/roles/drpsychick.ansible_testing\n```\n\nNow, when you run `molecule` it will use the symlink to include the `drpsychick.ansible_testing` role.\nMake your changes, commit regularly and when you're done, don't forget to create a pull-request so others can benefit \nfrom your improvements as well. What's more is that you can test the role itself with Molecule: \njust execute `molecule test` in your local fork directory.\n\n# Usage\nRequirements: \n* Linux (as it spawns containers in `privileged` mode and binds `/sys/fs/cgroup`)\n* Docker\n* libvirt (for Windows virtual machines)\n\n# Test with Docker containers (Linux)\nRequirements\n* `pip3 install -U molecule molecule-docker`\n\n## With Ansible `molecule`\nCreate a new role with `molecule init role \u003cname\u003e` or initialize the Molecule scenario in an existing role directory with\n`molecule init scenario default`.\n\nDownload the example files from this repo which make use of this role (in `create` and `destroy`):\n```shell\nfor f in create destroy molecule requirements vars; do\n  curl -o molecule/default/$f.yml https://raw.githubusercontent.com/DrPsychick/ansible-testing/main/docs/molecule/default/$f.yml\ndone\n```\n\nAdjust the `molecule/default/vars.yml` to define which containers to provision.\nThen adjust the `platforms` in `molecule/default/molecule.yml` accordingly.\n\n`vars.yml`\n```yaml\nwork_dir: \"/tmp/ansible-testrole-default\"\ncontainers:\n  - { name: fedora40, os: fedora, dockerfile: Dockerfile_Fedora, files: [\"entrypoint.sh\"], args: { VERSION: 40 } }\n  - { name: ubuntu2404, os: ubuntu, dockerfile: Dockerfile_Ubuntu, files: [\"entrypoint.sh\"], args: { VERSION: 24.04 } }\n  - { name: centos7, os: centos, dockerfile: Dockerfile_CentOS, files: [\"entrypoint.sh\"], args: { VERSION: 7 } }\n```\n\n`molecule.yml`\n```yaml\n[...]\nplatforms:\n  - name: fedora40\n  - name: ubuntu2404\n  - name: centos7\n[...]\n```\n\nRun molecule\n```shell\n# steps separately\nmolecule dependency\nmolecule create\nmolecule prepare\nmolecule converge\nmolecule idempotence\nmolecule verify\nmolecule cleanup\nmolecule destroy\n\n# or everything in one go\nmolecule test\n```\n\n## Standalone\nWrite your own playbook or use the playbooks in `tests`\n```shell\nansible-galaxy install -r requirements.yml\n\n# edit tests/provision.yml to your needs\necho \"[defaults]\nroles_path = ..\" \u003e ansible.cfg\n\n# create containers\nansible-playbook tests/create.yml\n\n# destroy containers\nansible-playbook tests/destroy.yml\n```\n\n\n# Test with Windows VMs\nRequirements\n* install `libvirt`, `libvirt-clients`, `virtinst`\n* ansible-galaxy `community.libvirt`\n* for Ansible to connect with WinRM: `python3-winrm`\n* see [defaults/main.yml](defaults/main.yml)\n  * Download the Windows Image of your choice (Test is setup for Windows 2016)\n  * Download the VirtIO ISO\n  * Put both ISOs into the `libvirt_iso_dir`\n\n```shell\n# download the ISOs\nsudo curl -Lo /var/lib/libvirt/isos/WindowsServer2016.iso http://care.dlservice.microsoft.com/dl/download/1/6/F/16FA20E6-4662-482A-920B-1A45CF5AAE3C/14393.0.160715-1616.RS1_RELEASE_SERVER_EVAL_X64FRE_EN-US.ISO\nsudo curl -Lo /var/lib/libvirt/isos/virtio-win.iso https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso\n```\n\n## Use libvirt as user (no become/sudo)\nCreate `image` and `iso` pool that is writeable by the user and set the permissions of the pool accordingly.\nMake sure the user is part of \n```shell\nsudo virsh pool-create-as myisos dir --target /mydir/libvirt/isos\nsudo virsh pool-edit isos # set permissions\n\nsudo virsh pool-create-as myimages dir --target /mydir/libvirt/images\nsudo virsh pool-edit images2 # set permissions\n```\n\nReference the directories and pool in your `molecule/libvirt/vars.yml`\n```shell\nlibvirt_image_dir: \"/mydir/libvirt/images\"\nlibvirt_iso_dir: \"/mydir/libvirt/isos\"\nlibvirt_disk_pool: \"myimages\"\n```\n\n\n## With Ansible `molecule`\nCreate a new role with `molecule init role \u003cname\u003e` or initialize the Molecule scenario in an existing role directory with\n`molecule init scenario default`.\n\nDownload the example files from this repo which make use of this role (in `create` and `destroy`):\n```shell\nfor f in create destroy molecule requirements vars; do\n  curl -o molecule/libvirt/$f.yml https://raw.githubusercontent.com/DrPsychick/ansible-testing/main/docs/molecule/libvirt/$f.yml\ndone\n```\n\nAdjust the `molecule/libvirt/vars.yml` to define which containers to provision.\nThen adjust the `platforms` in `molecule/libvirt/molecule.yml` accordingly.\n\n```shell\n# run the scenario \"libvirt\"\nmolecule test -s libvirt\n```\n\n## Standalone\n```shell\nansible-galaxy install -r requirements.yml\n\n# edit tests/provision.yml to your needs\necho \"[defaults]\nroles_path = ..\" \u003e ansible.cfg\n\n# create virtual machine\nansible-playbook tests/create_vm.yml\n\n# destroy virtual machine\nansible-playbook tests/destroy_vm.yml\n```\n\n## Using predefined Windows images to speed up provisioning\nA full spin-up (create) run for 2 Windows instances with predefined images took **less than 4 minutes** on my i7.\n1. Create a `qcow2` image or simply provision a VM once with unattended install\n2. Create a zip from the ready to use VM: `zip windows2016-clean.qcow2.zip windows2016.qcow2`\n   (the filename must match and be in the root of the zip file - with no path)\n3. Move the zip file to `libvirt_iso_dir` or provide it via URL (`disk_image_url`)\n\n# Test locally with different Ansible versions\n\nRequires `python3-venv`\n\n```shell\n# version 16 and latest fail with\n# ERROR! Unexpected Exception, this is probably a bug: cannot import name 'should_retry_error' from 'ansible.galaxy.api'\nANSIBLE_VERSION=15\npython3 -m venv .venv\n. .venv/bin/activate\npip3 install --upgrade pip setuptools wheel\npip3 install --requirement requirements-ansible_${ANSIBLE_VERSION}.txt\n\nmolecule test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrpsychick%2Fansible-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrpsychick%2Fansible-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrpsychick%2Fansible-testing/lists"}