{"id":18797799,"url":"https://github.com/networktocode/interop2020-ansible-molecule","last_synced_at":"2025-04-13T16:32:24.135Z","repository":{"id":42650142,"uuid":"295443778","full_name":"networktocode/interop2020-ansible-molecule","owner":"networktocode","description":"Ansible Molecule demo for Network Automation","archived":true,"fork":false,"pushed_at":"2023-01-24T23:24:53.000Z","size":51,"stargazers_count":14,"open_issues_count":2,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-05T17:39:58.508Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","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/networktocode.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-09-14T14:35:45.000Z","updated_at":"2024-06-20T16:18:43.000Z","dependencies_parsed_at":"2023-02-14T02:31:25.437Z","dependency_job_id":null,"html_url":"https://github.com/networktocode/interop2020-ansible-molecule","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/networktocode%2Finterop2020-ansible-molecule","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/networktocode%2Finterop2020-ansible-molecule/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/networktocode%2Finterop2020-ansible-molecule/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/networktocode%2Finterop2020-ansible-molecule/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/networktocode","download_url":"https://codeload.github.com/networktocode/interop2020-ansible-molecule/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248743941,"owners_count":21154773,"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":[],"created_at":"2024-11-07T22:09:38.938Z","updated_at":"2025-04-13T16:32:23.872Z","avatar_url":"https://github.com/networktocode.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Molecule for Testing Network Automation Ansible Projects\n\n[Molecule](https://molecule.readthedocs.io/en/latest/index.html) is a project designed to aid in the development of Ansible projects. This demo aims to show its testing framework capabilities in a Network Automation context.\n\n## Overview\n\nThe Molecule project is designed to aid the development and testing of Ansible playbooks, roles and collections. And for this it provides support for testing with multiple instances, operating systems and distributions, virtualization providers, test frameworks and scenarios.\n\nIn the network infrastructure/automation domain testing can be particularly hard, connectivity and data collection mechanism, network device virtualization, difference between NOSes, among other issues. And here is where the flexibility molecule gives, alongside the capabilities that ansible already provides, help building test scenarios for your ansible projects.\n\nTo follow along with the example of the demos you need to clone the project:\n\n```shell\ngit clone https://github.com/davidban77/ansible-molecule-demo.git\ncd ansible-molecule-demo\n```\n\nWhen molecule is run, it is normally in charge of:\n\n- Creating the instances to test your ansible playbooks, roles and/or collections.\n- (Optionally) Preparing the instances, like installing pre-requisite packages or setting up the environment.\n- Converging the playbooks, roles or collections you want to test.\n- Performing idempotency check by running again your ansible project.\n- Verify the final state of the instances.\n- Destroy the instances.\n\n## Requirements\n\nIn a python virtual environment install the dependencies stated in `requirement.txt`\n\n```shell\npip install -r requirements.txt\n```\n\n## How it works\n\nThis repository has 2 playbooks which follow the following workflow:\n\n```shell\nCollect Interface Raw Data \u003e Parse data into structured format \u003e Save to DB\n```\n\nThere are 2 molecule scenarios built that test these playbooks.\n\n---\n\n## Test: Static Data Scenario\n\nThis scenarios aims to show how molecule can be used with `docker` plugin. It instantiates a `centos:7` docker container where a interface data `txt` file has the information and the playbook is in charge of reading it, parsing the data and save it to an Elasticsearch DB container.\n\nTo run all scneario's phases:\n\n```shell\nmolecule test -s static\n```\n\n---\n\n## Test: Mocked Data Scenario\n\nIn this scenario I am leveraging the awesome project [cisshgo](https://github.com/tbotnz/cisshgo), which give the ability to mock an SSH connection to a network device and has some predefined data which you can use to parse and test.\n\nAs a pre-requisite for this test scenario you need to build a docker image locally so it can be used:\n\n```shell\ngit clone https://github.com/tbotnz/cisshgo.git\ncd cisshgo\ndocker build -t cisshgo:latest .\ndocker tag cisshgo:latest molecule_local/cisshgo:latest\n```\n\nThe tag is needed when molecule uses it.\n\nTo run the test scenario:\n\n```shell\nmolecule test -s mock\n```\n\n---\n\n## Test: GNS3 Scenario\n\nIn this interesting scenario you can leverage a GNS3 server as a resource provider for the molecule scenario, meaning that **molecule will create, prepare and destroy GNS3 labs and routers** for testing the ansible playbooks.\n\n### Extra requirements:\n\n- **GNS3 server and valid router image installed**: In this case the GNS3 server is on the same network as the host running molecule and it has the IOSv image and template installed. This means as well that connectivty from the molecule machine to GNS3 server must exist to be able to connect to its API.\n- [gns3fy](https://github.com/davidban77/gns3fy)\n\n```shell\npip install gns3fy\n```\n\n- [Ansible collection for gns3](https://galaxy.ansible.com/davidban77/gns3) for the creation and destroy playbooks to be succesfully executed\n\n```shell\nansible-galaxy collection install davidban77.gns3\n```\n\n- [netaddr](https://github.com/netaddr/netaddr) python package to successfully run some jinja2 filters in ansible\n\n```shell\npip install netaddr\n```\n\n### Molecule procedure in GNS3 scenario\n\n![molecule-gns3-scenario](docs/interop2020-molecule-gns3.svg)\n\nThe steps shown in the diagram are executed in the following molecule phases:\n\n- **Create and prepare**: Step 1 and 2. Uses the GNS3 ansible modules to connect to server, create lab, create router and push initial bootstrap SSH config over console.\n- **Converge**: Step 3 and 4. Where the playbook being tested is executed. It follows the same procedure as the steps above. Important note here is that the connection is now `network_cli`.\n- **Verify**: Step 5. Could have been the same `verify.yml` of the other scenarios but since the interfaces were different, I just copied and replaced the interfaces to be verified.\n- **Destroy**: Step 6. It deletes the GNS3 lab and router **AND** the docker networks and container (elasticsearch). So all resources being used are released.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetworktocode%2Finterop2020-ansible-molecule","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnetworktocode%2Finterop2020-ansible-molecule","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetworktocode%2Finterop2020-ansible-molecule/lists"}