{"id":16179283,"url":"https://github.com/jonashackt/molecule-ansible-azure","last_synced_at":"2025-03-19T01:30:57.286Z","repository":{"id":38743801,"uuid":"178794907","full_name":"jonashackt/molecule-ansible-azure","owner":"jonashackt","description":"Example project showing how to do test-driven development with Ansible \u0026 Molecule using Microsoft Azure","archived":false,"fork":false,"pushed_at":"2019-04-01T06:06:28.000Z","size":771,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T13:47:16.060Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonashackt.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}},"created_at":"2019-04-01T05:56:10.000Z","updated_at":"2023-02-13T02:24:50.000Z","dependencies_parsed_at":"2022-08-25T04:12:57.832Z","dependency_job_id":null,"html_url":"https://github.com/jonashackt/molecule-ansible-azure","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/jonashackt%2Fmolecule-ansible-azure","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonashackt%2Fmolecule-ansible-azure/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonashackt%2Fmolecule-ansible-azure/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonashackt%2Fmolecule-ansible-azure/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonashackt","download_url":"https://codeload.github.com/jonashackt/molecule-ansible-azure/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243960361,"owners_count":20375102,"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-10-10T05:26:35.235Z","updated_at":"2025-03-19T01:30:56.983Z","avatar_url":"https://github.com/jonashackt.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# molecule-ansible-azure\n[![Build Status](https://travis-ci.org/jonashackt/molecule-ansible-azure.svg?branch=master)](https://travis-ci.org/jonashackt/molecule-ansible-azure)\n[![versionansible](https://img.shields.io/badge/ansible-2.7.9-brightgreen.svg)](https://docs.ansible.com/ansible/latest/index.html)\n[![versionmolecule](https://img.shields.io/badge/molecule-2.20.0-brightgreen.svg)](https://molecule.readthedocs.io/en/latest/)\n[![versiontestinfra](https://img.shields.io/badge/testinfra-1.19.0-brightgreen.svg)](https://testinfra.readthedocs.io/en/latest/)\n[![versionazurecli](https://img.shields.io/badge/azurecli-2.0.60-brightgreen.svg)](https://aws.amazon.com/cli/)\n\nExample projects showing how to do test-driven development of Ansible roles and running those tests on multiple Cloud providers at the same time\n\nThis project build on top of [molecule-ansible-docker-vagrant](https://github.com/jonashackt/molecule-ansible-docker-vagrant), where all the basics on how to do test-driven development of Ansible roles with Molecule is described. Have a look into the blog series so far:\n\n* [Test-driven infrastructure development with Ansible \u0026 Molecule](https://blog.codecentric.de/en/2018/12/test-driven-infrastructure-ansible-molecule/)\n* [Continuous Infrastructure with Ansible, Molecule \u0026 TravisCI](https://blog.codecentric.de/en/2018/12/test-driven-infrastructure-ansible-molecule/)\n* [Continuous cloud infrastructure with Ansible, Molecule \u0026 TravisCI on AWS](https://blog.codecentric.de/en/2019/01/ansible-molecule-travisci-aws/)\n\n## What about Multicloud?\n\nDeveloping infrastructure code according to prinicples like test-driven development and continuous integration is really great! But what about pushing this to the next level? As [Molecule](https://molecule.readthedocs.io/en/latest/) is able to handle everything Ansible is albe to access, why not run our test automatically on all major cloud platforms at the same time?\n\nWith this, we would not only have a security net for our infrastructure code, but would also be safe regarding a switch of our current cloud or data center provider. Lot's of people talk about the unclear costs of this switch. **** If our infrastructure code would be able to run on every cloud platform possible, we would simply be able to switch to whatever platform we want - and all with just the virtually no expenses.Why not just reduce these to zero?!\n\n\n## Add Azure to the game\n\nLet's start with AWS by just forking [molecule-ansible-google-cloud](https://github.com/jonashackt/molecule-ansible-google-cloud), since there should be mostly everything needed to use Molecule with a Cloud provider.\n\n\nFirst, you'll need a valid [Azure account](https://azure.microsoft.com), which happens to be a Microsoft account (you could also use that to access Office 365 and other things). If everything is fine with your account, you should be able to access the Azure Portal at [https://portal.azure.com/#home](https://portal.azure.com/#home):\n\n![azure-portal](screenshots/azure-portal.png) \n\n\nAccording to the Molecule docs about [the Azure driver](https://molecule.readthedocs.io/en/latest/configuration.html#azure), we then we need to install Azure support for Molecule:\n\n```\npip3 install ansible[azure]\n```\n\nCompared to AWS and GCE this time we install pip's Ansible package with Azure support, not Molecule itself.\n\n\nNow let's initialize a new Molecule scenario calles `azure-ubuntu` inside our Ansible role:\n\n```\ncd molecule-ansible-azure/docker\n\nmolecule init scenario --driver-name azure --role-name docker --scenario-name azure-ubuntu\n```\n\nThat should create a new directory `azure-ubuntu` inside the `docker/molecule` folder.  We'll integrate the results into our multi scenario project in a second.\n\nNow let's dig into the generated [molecule.yml](docker/molecule/azure-ubuntu/molecule.yml):\n\n```yaml\nscenario:\n  name: azure-ubuntu\n\ndriver:\n  name: azure\nplatforms:\n  - name: azure-ubuntu\n\nprovisioner:\n  name: ansible\n  lint:\n    name: ansible-lint\n    enabled: false\n  playbooks:\n    converge: ../playbook.yml\n\nlint:\n  name: yamllint\n  enabled: false\n\nverifier:\n  name: testinfra\n  directory: ../tests/\n  env:\n    # get rid of the DeprecationWarning messages of third-party libs,\n    # see https://docs.pytest.org/en/latest/warnings.html#deprecationwarning-and-pendingdeprecationwarning\n    PYTHONWARNINGS: \"ignore:.*U.*mode is deprecated:DeprecationWarning\"\n  lint:\n    name: flake8\n  options:\n    # show which tests where executed in test output\n    v: 1\n\n```\n\nAs we already tuned the `molecule.yml` files for our other scenarios like `aws-ec2-ubuntu`, we know what to change here. `provisioner.playbook.converge` needs to be configured, so the one `playbook.yml` could be found.\n\nAlso the `verifier` section has to be enhanced to gain all the described advantages like supressed deprecation warnings and the better test result overview.\n\nAs you may noticed, the driver now uses `azure` and the platform is pre-configured with only a concrete `name`. Here we just tune the instance name to `azure-ubuntu`.\n\n\n\n### Configure Azure Ressource Manager (RM) image\n\nCompared to the AWS and GCE Molecule drivers, these are only one configuration parameters. But there have to be more, just think about region or image configuration! Therefore we need to dive into the [create.yml](docker/molecule/azure-ubuntu/create.yml):\n\n```yaml\n\n- name: Create\n  hosts: localhost\n  connection: local\n  gather_facts: false\n  no_log: \"{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}\"\n  vars:\n    resource_group_name: molecule\n    location: westus\n    ssh_user: molecule\n    ssh_port: 22\n    virtual_network_name: molecule_vnet\n    subnet_name: molecule_subnet\n    keypair_path: \"{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key\"\n  tasks:\n  ...\n  - name: Create molecule instance(s)\n        azure_rm_virtualmachine:\n          resource_group: \"{{ resource_group_name }}\"\n          name: \"{{ item.name }}\"\n          vm_size: Standard_A0\n          admin_username: \"{{ ssh_user }}\"\n          public_ip_allocation_method: Dynamic\n          ssh_password_enabled: false\n          ssh_public_keys:\n            - path: \"/home/{{ ssh_user }}/.ssh/authorized_keys\"\n              key_data: \"{{ keypair.ssh_public_key }}\"\n          image:\n            offer: CentOS\n            publisher: OpenLogic\n            sku: '7.4'\n            version: latest\n        register: server\n        with_items: \"{{ molecule_yml.platforms }}\"\n        async: 7200\n        poll: 0\n  ...\n```\n\nAnd there we are! Molecule makes heavy usage of Ansible's [azure_rm_virtualmachine](https://docs.ansible.com/ansible/latest/modules/azure_rm_virtualmachine_module.html) module.\n\nNow as we chose to implement the use case of a [standard Ubuntu Docker installation](https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-docker-ce-1), we need to switch the `image.offer` to another fitting one. So let's adhere to the docs' standard way and use Azure CLI to list available image configurations for our location. \n\nTherefore Azure CLI needs to be available on your machine. On my Mac I use `brew install azure-cli` to install it. Now we can do:\n\n```\n$ az vm image list --output table\nYou are viewing an offline list of images, use --all to retrieve an up-to-date list\nOffer          Publisher               Sku                 Urn                                                             UrnAlias             Version\n-------------  ----------------------  ------------------  --------------------------------------------------------------  -------------------  ---------\nCentOS         OpenLogic               7.5                 OpenLogic:CentOS:7.5:latest                                     CentOS               latest\nCoreOS         CoreOS                  Stable              CoreOS:CoreOS:Stable:latest                                     CoreOS               latest\nDebian         credativ                9                   credativ:Debian:9:latest                                        Debian               latest\nopenSUSE-Leap  SUSE                    42.3                SUSE:openSUSE-Leap:42.3:latest                                  openSUSE-Leap        latest\nRHEL           RedHat                  7-RAW               RedHat:RHEL:7-RAW:latest                                        RHEL                 latest\nSLES           SUSE                    15                  SUSE:SLES:15:latest                                             SLES                 latest\nUbuntuServer   Canonical               18.04-LTS           Canonical:UbuntuServer:18.04-LTS:latest                         UbuntuLTS            latest\nWindowsServer  MicrosoftWindowsServer  2019-Datacenter     MicrosoftWindowsServer:WindowsServer:2019-Datacenter:latest     Win2019Datacenter    latest\nWindowsServer  MicrosoftWindowsServer  2016-Datacenter     MicrosoftWindowsServer:WindowsServer:2016-Datacenter:latest     Win2016Datacenter    latest\nWindowsServer  MicrosoftWindowsServer  2012-R2-Datacenter  MicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latest  Win2012R2Datacenter  latest\nWindowsServer  MicrosoftWindowsServer  2012-Datacenter     MicrosoftWindowsServer:WindowsServer:2012-Datacenter:latest     Win2012Datacenter    latest\nWindowsServer  MicrosoftWindowsServer  2008-R2-SP1         MicrosoftWindowsServer:WindowsServer:2008-R2-SP1:latest         Win2008R2SP1         latest\n```\n\nAs there we can spot a fitting Ubuntu 18.04 image inside the list, we should be able to configure Molecule. As we already saw inside the generated [create.yml](docker/molecule/azure-ubuntu/create.yml), the `azure_rm_virtualmachine` module uses a `with_items: \"{{ molecule_yml.platforms }}\"` configuration, so we only need to change the `create.yml` sligthly:\n\n```yaml\n  ...\n  - name: Create molecule instance(s)\n        azure_rm_virtualmachine:\n          ...\n          image: \"{{ item.image }}\"\n        register: server\n        with_items: \"{{ molecule_yml.platforms }}\"\n        async: 7200\n        poll: 0\n  ...\n```\n\nWith this, we can now move to our [molecule.yml](docker/molecule/azure-ubuntu/molecule.yml) and configure the Azure image:\n\n```yaml\nscenario:\n  name: gcp-gce-ubuntu\n\ndriver:\n  name: azure\nplatforms:\n  - name: azure-ubuntu\n    image:\n      offer: UbuntuServer\n      publisher: Canonical\n      sku: 18.04-LTS\n      version: latest\n  ...\n```\n\n\n### Configure Azure location\n\nWe shouldn't forget the configuration of our Azure location (compare with GCP's zone and AWS' region). To find out the correct location, we can leverage the Azure CLI again. As the docs state, there is `az account list-locations` to list the configured location for your account. Running the command will maybe result in the following error:\n\n```\n$ az account list-locations\nPlease run 'az login' to setup account.\n```\n\nNow to interact with the `az account` commands, you'll need a valid Azure subscription. You can start with [the free 12 month subscription](https://azure.microsoft.com/en-us/free/) for example. If you entered everything, the subscription should be available inside the Azure Portal and the `azure login` command should work like this:\n\n```\n$ az login\nNote, we have launched a browser for you to login. For old experience with device code, use \"az login --use-device-code\"\nYou have logged in. Now let us find all the subscriptions to which you have access...\n[\n  {\n    \"cloudName\": \"AzureCloud\",\n    \"id\": \"1f0021b3-xxx-xxxx-xxxx-xxxxxxxxxxx\",\n    \"isDefault\": true,\n    \"name\": \"Free Trial\",\n    \"state\": \"Enabled\",\n    \"tenantId\": \"xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx\",\n    \"user\": {\n      \"name\": \"jonas.hecht@codecentric.de\",\n      \"type\": \"user\"\n    }\n  }\n]\n```\n\nHaving the Azure CLI successfully logged in, we should try to run `az account list-locations` again. Just pick a location, which suits you best. I took `westeurope` for example and change the [create.yml](docker/molecule/azure-ubuntu/create.yml):\n\n```yaml\n- name: Create\n  ...\n  vars:\n    resource_group_name: molecule\n    location: westeurope\n    ssh_user: molecule\n    ssh_port: 22\n```\n\n\n\n### Creating a Azure RM VM instance with Molecule\n\n\nNow we should have everything prepared. Let's try to run our first Molecule test on Azure (including `--debug` so that we see what's going on):\n\n```\nmolecule --debug create --scenario-name azure-ubuntu\n```\n\nOpen your Google Cloud Compute Engine dashboard and you should see the instance beeing created by Molecule:\n\n![google-cloud-first-running-instance](screenshots/google-cloud-first-running-instance.png)\n\n\n\n\n### Configure Travis CI to run our Molecule test automatically on Azure\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonashackt%2Fmolecule-ansible-azure","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonashackt%2Fmolecule-ansible-azure","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonashackt%2Fmolecule-ansible-azure/lists"}