{"id":15978620,"url":"https://github.com/greg-hellings/molecule_example","last_synced_at":"2026-03-07T16:02:29.763Z","repository":{"id":137513913,"uuid":"310656062","full_name":"greg-hellings/molecule_example","owner":"greg-hellings","description":null,"archived":false,"fork":false,"pushed_at":"2020-11-06T16:59:23.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-08T00:11:04.133Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/greg-hellings.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-06T16:57:13.000Z","updated_at":"2020-11-06T16:59:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"e54db796-1319-400c-a020-ad1bea913372","html_url":"https://github.com/greg-hellings/molecule_example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/greg-hellings/molecule_example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-hellings%2Fmolecule_example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-hellings%2Fmolecule_example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-hellings%2Fmolecule_example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-hellings%2Fmolecule_example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/greg-hellings","download_url":"https://codeload.github.com/greg-hellings/molecule_example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-hellings%2Fmolecule_example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30221193,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T14:02:48.375Z","status":"ssl_error","status_checked_at":"2026-03-07T14:02:43.192Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-10-07T23:21:34.211Z","updated_at":"2026-03-07T16:02:29.756Z","avatar_url":"https://github.com/greg-hellings.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"molecule_example\n===========\n\nA sample role that we will use to create Molecule scenarios and train\npeople on how to properly use Molecule\n\nRequirements\n------------\n\nAnsible 2.8 or higher\n\nRole Variables\n--------------\n\nCurrently the following variables are supported:\n\n### General\n\n* `molecule_example_user` - Default: test\\_user. Name of the user and group\n  to create\n* `molecule_example_become` - Default: true. If this role needs administrator\n  privileges, then use the Ansible become functionality (based off sudo).\n* `molecule_example_become_user` - Default: root. If the role uses the become\n  functionality for privilege escalation, then this is the name of the target\n  user to change to.\n\nDependencies\n------------\n\nNone\n\nExample Playbook\n----------------\n\n```yaml\n- hosts: molecule_example-servers\n  roles:\n    - role: molecule_example\n```\n\nLicense\n-------\n\nGPLv3\n\nAuthor Information\n------------------\n\nGreg Hellings \u003cgreg.hellings@gmail.com\u003e\n\nWalkthrough\n-----------\n\n### Create A Role\n\nTo start from the base role, checkout the role at that level:\n\n```bash\ngit checkout bare_role\n# Be sure that ansible and molecule are installed\npip install molecule ansible\n```\n\n### Create default scenario\n\nNext, we create a molecule scenario\n\n```bash\n# Be sure that the driver is installed\npip install molecule-podman\nmolecule init scenario --driver-name podman\n# Populate test files as appropriate\n```\n\nChange the `verify.yml` to check that the user directory has been\ncreated properly.\n\n```yaml\n- name: Verify\n  hosts: all\n  gather_facts: false\n  tasks:\n    - name: Run stat\n      stat:\n        path: /home/test_user\n      register: _is_test_user\n\n    - name: Be sure it exists\n      assert:\n        that: _is_test_user.stat.exists\n```\n\nNow run the default scenario with `molecule test`\n\nThe repo can be seen at that stage by doing a `git checkout first_scenario`\n\n### Create Scenario With New Name\n\nNow create a new scenario, with a new name, to test that the name of\nthe user created is correct.\n\n```bash\nmolecule init scenario --driver-name podman configure_name\n```\n\nAdd a var to the `molecule/configure_name/converge.yml` to set the username\nto something other than the default. And add the same verify.yml code above,\nbut adjust it to look for the proper pathname to match the name you've\nconfigured.\n\nNow run the scenario by specifying the name\n\n```bash\nmolecule test -s configure_name\n```\n\nThis can also be found, now, with `git checkout second_scenario`\n\n### Consolidate molecule.yml into shared file\n\nNote that there is significant overlap in the molecule.yml files of these\ntwo scenarios. With molecule we can specify a root configuration file. The\ntool will do a deep merge with the molecule.yml file that's in the scenario\nfolder, so anything can be overridden at even the lowest level.\n\nCurrently the two files are identical, so we can just copy one of the\nmolecule.yml files to the root and now we can completely empty out the files\nin each scenario. It's best to keep some minimal information in the file,\nbut it is not necessary.\n\nLet's improve things by forcing Ansible to output color. We do this by\nsetting `ansible.cfg` values in the provisioner section. We do it in this\nmanner:\n```yaml\nprovisioner:\n  config_options:\n    defaults:\n      force_color: true\n```\nAny config options that would go into `ansible.cfg` can live here. Some of\nmy favorites are `stdout_callback: yaml` and `force_color: true` and\n`remote_tmp: /tmp/${USER}/ansible`.\n\nNow we can test by running the two scenarios:\n\n```bash\nmolecule -c molecule.yml test\nmolecule -c molecule.yml test -s configure_name\n```\n\nThis can also be found with `git checkout consolidated_molecule`\n\n### Combine converge.yml and verify.yml\n\nSo, in an effort to reduce duplication, let's use a single converge.yml and\nverify.yml file. How can we do this? By using `group_vars` in the\nmolecule.yml.\n\nMove `molecule/default/converge.yml` into `molecule/shared/converge.yml`. Do\nthe same with `molecule/default/verify.yml` and let's get rid of the\n`molecule/configure_name/{converge,verify}.yml`.\n\nNow, tell Molecule where to find the converge and verify files in the\nroot molecule.yml by modifying the `provisioner` section in this manner.\n\n```yaml\nprovisioner:\n  playbooks:\n    converge: ../shared/converge.yml\n    verify:  ../shared/verify.yml\n```\n\nNow, go into `molecule/configure_name/molecule.yml` and let's take advantage\nof the deep merging that Molecule does. We can add this code to that file,\nand it will set the `group_vars` appropriately. The relevant file should now\nlook like this\n\n```yaml\nprovisioner:\n  inventory:\n    group_vars:\n      all:\n        molecule_example_user: greg\n```\n\nNow you should notice that the default scenario runs properly, but the\nconfigure\\_name scenario is failing. This is because `verify.yml` is hard\ncoded to look for a particular path. To parameterize this we can add another\nvariable telling the path to look for. This time, we can add it to both\nfiles in the same section as above. Add a line with the name \"verify\\_path\"\nand the value \"/home/greg\" to the `molecule/configure_name/molecule.yml`\nfile. Add the whole above section, minus the `molecule_example_user` line\nto `molecule/default/molecule.yml` and set the value of `verify_path` to\n`/home/test_user`.\n\nThis portion can be seen as `git checkout deduplicate`.\n\n### Other Advanced Uses\n\nThere are many other advanced uses you can configure with Molecule. You can\nset dependencies on other roles or collections in Ansible Galaxy. These are\nconfigured in a `requirements.yml` that lives in the root of the Molecule\nscenario. Its location can be changed by setting these values:\n\n```yaml\ndependency:\n  options:\n    role-file: path/relative/to/role_dir  # For roles\n    requirements-file: path/relative/to/role_dir  # For collections.\n```\n\nOf course, those two files can be combined into one if you use the proper\nsyntax inside of it like\n\n```yaml\nroles:\n  - some.role\ncollections:\n  - some.collection\n```\n\nIt is possible to write custom create and destroy playbooks. These can be\nconfigured in the `provisioner.playbooks` section with names like one might\nexpect (`create`, `destroy`, etc). You can even have different provision\nscripts for each driver type. That structure would look something like this.\nPaths are relative to the scenario's directory.\n\n```yaml\nprovisioner:\n  playbooks:\n    podman:\n      create: podman-create.yml\n      destroy: podman-destroy.yml\n    docker:\n      create: ../path/to/docker-create.yml\n      destroy: ../path/to/docker-destroy.yml\n```\n\nIn addition to Ansible verifiers, you can also write verifiers in Python\nusing the testinfra package. This is the old default for Molecule 2 code,\nbut the default has changed to using Ansible playbooks for the verify stage.\n\nWriting linters is possible. Currently lint is just a string that is passed\nas a shell script to the underlying system shell. You specify it in a\n`molecule.yml` file as such:\n\n```yaml\nlint: |-\n  set -ex\n  ansible-lint .\n  yamllint .\n  # etc, commands executed from role directory\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreg-hellings%2Fmolecule_example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgreg-hellings%2Fmolecule_example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreg-hellings%2Fmolecule_example/lists"}