'ansible playbook error was: ModuleNotFoundError: No module named 'azure.mgmt.monitor.version' although the module is installed

After upgrading ansible to version 2.10.5 and python3.8.10 my playbook.yml fails with this error.

ModuleNotFoundError: No module named 'azure.mgmt.monitor.version'
fatal: [localhost]: FAILED! => {"attempts": 1, "changed": false, "msg": "Failed to import the required Python library (ansible[azure] (azure >= 2.0.0)) on certrenewplay's Python /usr/bin/python3`

The module is there if I run python3 -c "import azure.mgmt.monitor" and if I run pip3 list I see it installed as azure-mgmt-monitor==2.0.0

The exact part of the playbook code that is erroring is this:

 - name: Create _acme-challenge record for zone "{{ env_name_dot }}"
   azure_rm_dnsrecordset:
    subscription_id: "{{ mgmt_subscription }}"
    client_id: "{{ mgmt_vault_azure_client_id }}"
    tenant: "{{ mgmt_vault_azure_tenant_id }}"
    secret: "{{ mgmt_vault_azure_client_secret }}"
    resource_group: "{{ mgmt_rg }}"
    relative_name: "_acme-challenge.{{ env_name }}"
    zone_name: "{{ dns_zone_name }}.{{ dns_zone_domain }}"
    record_type: TXT
    state: present
    records:
      - entry: "{{ cn_challenge_data }}"
    time_to_live: 60
  when: dns_zone_name != 'activedrop'
  register: add_record
  retries: 1
  delay: 10

  until: add_record is succeeded

I'm not sure what I'm doing wrong-can anyone advise please or help me on this please? Thanks



Solution 1:[1]

This same issue happened to me because Ansible now ships with its own version of the Azure collection and it was conflicting with the version I had manually installed in my own playbook using the "ansible-galaxy collection" command.

What I suggest you do is only use the version that ships with Ansible and then install its requirements like so:

pip install -r /usr/lib/python3/dist-packages/ansible_collections/azure/azcollection/requirements-azure.txt

It is easier to setup correctly on a freshly installed system (e.g in Docker) than it is to fix a broken system.

Solution 2:[2]

I think that you did not follow the instruction about installing azure collection from https://github.com/ansible-collections/azure

Installing collection itself does not install python dependencies, these are installed using python pip and you need to be sure you are installing them inside the same python (v)env where ansible is installed, or ansible will give you the error that you seen when trying to load the module.

Solution 3:[3]

Unfortunately the azure-mgmt-monitor package is bugged, even on 3.0.0 to not properly create a version submodule. Haven't been able to track down exactly where in the code it's busted, but it is and there is a direct import of that submodule in the Ansible Galaxy module causing it to fail. Unfortunately you should use the Azure CLI at this time and forget about using azure_rm

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick G
Solution 2 sorin
Solution 3 Ben Kett