error: ModuleNotFoundError No module named msrest msrestazure

Recently I have updated my Ansible to the latest 2.10.9 version and when I try to execute a playbook, but failed with the below error. The Ansible playbook is to provision resources in the Azure cloud and Ansible is installed on Macbook.

Error message

MacBook-Pro:Automation$ ansible-playbook site_grafana.yaml 

[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [Deploying the resources] **************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]

TASK [grafana_provision : Create a grafana virtual machine] *************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'msrest'

failed: [localhost] (item=1) => {"ansible_loop_var": "item", "changed": false, "item": 1, "msg": "Failed to import the required Python library (msrestazure) on MacBook-Pro.local's Python /Users/anaconda/anaconda3/bin/python. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

PLAY RECAP **********************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0 

The tutorial is how I fixed the above error in my MacBook.  From the error message, I have noticed the line The error was: ModuleNotFoundError: No module named ‘msrest'”  so I went ahead and installed the package “msrest”. “msrest” is an Azure dependency required for this Ansible version. To install this dependency execute the below command.

 

MacBook-Pro:Automation$ pip install msrest
Collecting msrest
  Downloading msrest-0.6.21-py2.py3-none-any.whl (85 kB)
     |████████████████████████████████| 85 kB 1.7 MB/s 
Collecting isodate>=0.6.0
  Downloading isodate-0.6.0-py2.py3-none-any.whl (45 kB)
     |████████████████████████████████| 45 kB 3.0 MB/s 
Requirement already satisfied: requests~=2.16 in /Users/anaconda/anaconda3/lib/python3.8/site-packages (from msrest) (2.24.0)
Requirement already satisfied: certifi>=2017.4.17 in /Users/anaconda/anaconda3/lib/python3.8/site-packages (from msrest) (2020.6.20)
Collecting requests-oauthlib>=0.5.0
  Downloading requests_oauthlib-1.3.0-py2.py3-none-any.whl (23 kB)
Requirement already satisfied: six in /Users/anaconda/anaconda3/lib/python3.8/site-packages (from isodate>=0.6.0->msrest) (1.15.0)
<1.26,>=1.21.1 in /Users/anaconda/anaconda3/lib/python3.8/site-packages (from requests~=2.16->msrest) (1.25.11)
Collecting oauthlib>=3.0.0
  Downloading oauthlib-3.1.0-py2.py3-none-any.whl (147 kB)
     |████████████████████████████████| 147 kB 6.7 MB/s 
Installing collected packages: isodate, oauthlib, requests-oauthlib, msrest
Successfully installed isodate-0.6.0 msrest-0.6.21 oauthlib-3.1.0 requests-oauthlib-1.3.0

I triggered my ansible script again, thinking that the installation of the above package will resolve the issue.

MacBook-Pro:Automation$ ansible-playbook site_grafana.yaml 

PLAY [Deploying the resources] **************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]

TASK [infra_grafana_provision : Create a grafana virtual machine] *************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'msrestazure'

failed: [localhost] (item=1) => {"ansible_loop_var": "item", "changed": false, "item": 1, "msg": "Failed to import the required Python library (ansible[azure] (azure >= 2.0.0)) on MacBook-Pro.local's Python /Users/anaconda/anaconda3/bin/python. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

PLAY RECAP **********************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

This time also failed with another Azure/ python dependency which is “msrestazure”. So I understand that installing Azure dependency one by one will not help, I have installed all dependency in a single command.

 

curl -O https://raw.githubusercontent.com/ansible-collections/azure/dev/requirements-azure.txt

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0pip install -r requirements-azure.txt

100  1220  100  1220    0     0   3012      0 --:--:-- --:--:-- --:--:--  3012

MacBook-Pro:Automation$ pip install -r requirements-azure.txt

Requirement already satisfied: packaging in /Users/anaconda/anaconda3/lib/python3.8/site-packages (from -r requirements-azure.txt (line 1)) (20.4)

Requirement already satisfied: requests[security] in /Users/anaconda/anaconda3/lib/python3.8/site-packages (from -r requirements-azure.txt (line 2)) (2.24.0)

Requirement already satisfied: xmltodict in /Users/anaconda/anaconda3/lib/python3.8/site-packages (from -r requirements-azure.txt (line 3)) (0.12.0)
Collecting azure-cli-core==2.11.1
  Downloading azure_cli_core-2.11.1-py3-none-any.whl (150 kB)

I triggered the ansible-playbook command again and this time it worked.

$ ansible-playbook site_grafana.yaml

PLAY [Deploying the resources] **************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]

TASK [infra_grafana_provision : Create a grafana virtual machine] *************************************************************
changed: [localhost] => (item=1)

TASK [infra_grafana_provision : Create a VM NIC and Public IP] ***********************************************************************
changed: [localhost] => (item=1)

TASK [infra_grafana_provision : Creating Public ip address] *********************************************************************************************************
changed: [localhost] => (item=1)

PLAY RECAP **********************************************************************************************************************************************************
localhost                  : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

Leave a Reply

Your email address will not be published. Required fields are marked *