azure icon indicating copy to clipboard operation
azure copied to clipboard

AttributeError: 'MSIAuthentication' object has no attribute 'get_token' fatal: [localhost]: FAILED!

Open Singhdeo opened this issue 2 years ago • 5 comments

Error while trying to deploy a machine using ansible.

We're using the script provided in the MS doc, Refer

  • name: Create Azure VM hosts: localhost connection: local tasks:
    • name: Create resource group azure_rm_resourcegroup: name: myResourceGroup location: eastus
    • name: Create virtual network azure_rm_virtualnetwork: resource_group: myResourceGroup name: myVnet address_prefixes: "10.0.0.0/16"
    • name: Add subnet azure_rm_subnet: resource_group: myResourceGroup name: mySubnet address_prefix: "10.0.1.0/24" virtual_network: myVnet
    • name: Create public IP address azure_rm_publicipaddress: resource_group: myResourceGroup allocation_method: Static name: myPublicIP register: output_ip_address
    • name: Public IP of VM debug: msg: "The public IP is {{ output_ip_address.state.ip_address }}."
    • name: Create Network Security Group that allows SSH azure_rm_securitygroup: resource_group: myResourceGroup name: myNetworkSecurityGroup rules: - name: SSH protocol: Tcp destination_port_range: 22 access: Allow priority: 1001 direction: Inbound
    • name: Create virtual network interface card azure_rm_networkinterface: resource_group: myResourceGroup name: myNIC virtual_network: myVnet subnet: mySubnet public_ip_name: myPublicIP security_group: myNetworkSecurityGroup
    • name: Create VM azure_rm_virtualmachine: resource_group: myResourceGroup name: myVM vm_size: Standard_DS1_v2 admin_username: azureuser ssh_password_enabled: false ssh_public_keys: - path: /home/azureuser/.ssh/authorized_keys key_data: "<key_data>" network_interfaces: myNIC image: offer: CentOS publisher: OpenLogic sku: '7.5' version: latest

++the resource group is being created, but the process is failing while tryin to create a Virtual network with the below error.

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'MSIAuthentication' object has no attribute 'get_token' fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File "/home/cyril/.ansible/tmp/ansible-tmp-1657197449.1435483-319-253545730603953/AnsiballZ_azure_rm_virtualnetwork.py", line 100, in \n _ansiballz_main()\n File "/home/cyril/.ansible/tmp/ansible-tmp-1657197449.1435483-319-253545730603953/AnsiballZ_azure_rm_virtualnetwork.py", line 92, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File "/home/cyril/.ansible/tmp/ansible-tmp-1657197449.1435483-319-253545730603953/AnsiballZ_azure_rm_virtualnetwork.py", line 41, in invoke_module\n run_name='main', alter_sys=True)\n File "/usr/lib/python3.7/runpy.py", line 205, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File "/usr/lib/python3.7/runpy.py", line 96, in _run_module_code\n mod_name, mod_spec, pkg_name, script_name)\n File "/usr/lib/python3.7/runpy.py", line 85, in _run_code\n exec(code, run_globals)\n File "/tmp/ansible_azure_rm_virtualnetwork_payload_irkl2j2a/ansible_azure_rm_virtualnetwork_payload.zip/ansible_collections/azure/azcollection/plugins/modules/azure_rm_virtualnetwork.py", line 386, in \n File "/tmp/ansible_azure_rm_virtualnetwork_payload_irkl2j2a/ansible_azure_rm_virtualnetwork_payload.zip/ansible_collections/azure/azcollection/plugins/modules/azure_rm_virtualnetwork.py", line 382, in main\n File "/tmp/ansible_azure_rm_virtualnetwork_payload_irkl2j2a/ansible_azure_rm_virtualnetwork_payload.zip/ansible_collections/azure/azcollection/plugins/modules/azure_rm_virtualnetwork.py", line 240, in init\n File "/tmp/ansible_azure_rm_virtualnetwork_payload_irkl2j2a/ansible_azure_rm_virtualnetwork_payload.zip/ansible_collections/azure/azcollection/plugins/module_utils/azure_rm_common.py", line 465, in init\n File "/tmp/ansible_azure_rm_virtualnetwork_payload_irkl2j2a/ansible_azure_rm_virtualnetwork_payload.zip/ansible_collections/azure/azcollection/plugins/modules/azure_rm_virtualnetwork.py", line 264, in exec_module\n File "/opt/ansible/lib/python3.7/site-packages/azure/mgmt/network/v2021_03_01/operations/_virtual_networks_operations.py", line 209, in get\n pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs)\n File "/opt/ansible/lib/python3.7/site-packages/azure/core/pipeline/_base.py", line 211, in run\n return first_node.send(pipeline_request) # type: ignore\n File "/opt/ansible/lib/python3.7/site-packages/azure/core/pipeline/_base.py", line 71, in send\n response = self.next.send(request)\n File "/opt/ansible/lib/python3.7/site-packages/azure/core/pipeline/_base.py", line 71, in send\n response = self.next.send(request)\n File "/opt/ansible/lib/python3.7/site-packages/azure/core/pipeline/_base.py", line 71, in send\n response = self.next.send(request)\n [Previous line repeated 2 more times]\n File "/opt/ansible/lib/python3.7/site-packages/azure/mgmt/core/policies/_base.py", line 47, in send\n response = self.next.send(request)\n File "/opt/ansible/lib/python3.7/site-packages/azure/core/pipeline/policies/_redirect.py", line 158, in send\n response = self.next.send(request)\n File "/opt/ansible/lib/python3.7/site-packages/azure/core/pipeline/policies/_retry.py", line 446, in send\n response = self.next.send(request)\n File "/opt/ansible/lib/python3.7/site-packages/azure/core/pipeline/policies/_authentication.py", line 117, in send\n self.on_request(request)\n File "/opt/ansible/lib/python3.7/site-packages/azure/core/pipeline/policies/_authentication.py", line 94, in on_request\n self._token = self._credential.get_token(*self._scopes)\nAttributeError: 'MSIAuthentication' object has no attribute 'get_token'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

++we are using user assigned identity.

++the identity have the permissions/role to access the resources in subscription.

Singhdeo avatar Jul 12 '22 08:07 Singhdeo

@Singhdeo It should be fixed by #870, available for local update if you need it, it will be released in the next version (v1.14.0). Thank you very much!

Fred-sun avatar Jul 14 '22 09:07 Fred-sun

wget https://raw.githubusercontent.com/Fred-sun/ansible_collections_azure/Add_msiauthentication_support_to_track2/plugins/module_utils/azure_rm_common.py

cp azure_rm_common.py /home/jakub/.local/lib/python3.7/site-packages/ansible/module_utils/

sirkubax avatar Jul 29 '22 15:07 sirkubax

@sirkubax Could you please help to provide the following information? First: provide "pip3 list" information Second: the version of "azure. azcollection" you installed! Third: The directory you copied is not the directory used by the execution file Fourth: the file header is added as follows

---
- name: Create Azure VM
  hosts: localhost
  connection: local
  collections:
    - azure.azcollection

Fred-sun avatar Aug 04 '22 06:08 Fred-sun

Sure Just in case - it comes directly from Azure Cloud Shell - in case you'd like to replicate it

The bug is at the source :D

image

I was not installing any of Ansible or azure.collection - it is managed by Microsoft :)

AD 1

Requesting a Cloud Shell.Succeeded. 
Connecting terminal...

Welcome to Azure Cloud Shell

Type "az" to use Azure CLI
Type "help" to learn about Cloud Shell

jakub@Azure:~$ pip3 list
Package                           Version
--------------------------------- -----------
adal                              1.2.7
ansible                           4.10.0
ansible-core                      2.11.12
applicationinsights               0.11.10
argcomplete                       1.12.3
asn1crypto                        0.24.0
azure-cli-core                    2.34.0
azure-cli-telemetry               1.0.6
azure-common                      1.1.11
azure-core                        1.24.2
azure-graphrbac                   0.61.1
azure-identity                    1.7.0
azure-keyvault                    1.0.0a1
azure-mgmt-apimanagement          0.2.0
azure-mgmt-authorization          0.51.1
azure-mgmt-automation             1.0.0
azure-mgmt-batch                  5.0.1
azure-mgmt-cdn                    3.0.0
azure-mgmt-compute                26.1.0
azure-mgmt-containerinstance      1.4.0
azure-mgmt-containerregistry      2.0.0
azure-mgmt-containerservice       9.1.0
azure-mgmt-core                   1.3.0
azure-mgmt-cosmosdb               0.15.0
azure-mgmt-datafactory            2.0.0
azure-mgmt-datalake-nspkg         2.0.0
azure-mgmt-datalake-store         0.5.0
azure-mgmt-devtestlabs            3.0.0
azure-mgmt-dns                    2.1.0
azure-mgmt-eventhub               2.0.0
azure-mgmt-hdinsight              0.1.0
azure-mgmt-iothub                 0.7.0
azure-mgmt-keyvault               1.1.0
azure-mgmt-loganalytics           1.0.0
azure-mgmt-managedservices        1.0.0
azure-mgmt-managementgroups       0.2.0
azure-mgmt-marketplaceordering    0.1.0
azure-mgmt-monitor                3.0.0
azure-mgmt-network                19.1.0
azure-mgmt-notificationhubs       2.0.0
azure-mgmt-nspkg                  2.0.0
azure-mgmt-privatedns             0.1.0
azure-mgmt-rdbms                  1.9.0
azure-mgmt-recoveryservices       0.4.0
azure-mgmt-recoveryservicesbackup 0.6.0
azure-mgmt-redis                  13.0.0
azure-mgmt-resource               10.2.0
azure-mgmt-search                 3.0.0
azure-mgmt-servicebus             0.5.3
azure-mgmt-sql                    3.0.1
azure-mgmt-storage                19.0.0
azure-mgmt-trafficmanager         0.50.0
azure-mgmt-web                    0.41.0
azure-nspkg                       2.0.0
azure-servicefabric               8.0.0.0
azure-storage                     0.35.1
azureml-core                      1.43.0
backports.tempfile                1.0
backports.weakref                 1.0.post1
bcrypt                            3.2.2
certifi                           2022.5.18.1
cffi                              1.15.1
charset-normalizer                2.0.12
colorama                          0.4.4
contextlib2                       21.6.0
cryptography                      37.0.4
distlib                           0.3.4
distro-info                       0.21
docker                            5.0.3
entrypoints                       0.3
enum34                            1.1.10
filelock                          3.7.0
future                            0.18.2
humanfriendly                     10.0
idna                              3.3
importlib-metadata                4.11.3
isodate                           0.6.1
Jinja2                            3.1.2
jmespath                          1.0.0
joblib                            1.1.0
jsonpickle                        2.2.0
keyring                           17.1.1
keyrings.alt                      3.1.1
knack                             0.9.0
MarkupSafe                        2.1.1
msal                              1.18.0
msal-extensions                   0.3.1
msrest                            0.6.21
msrestazure                       0.6.4
mssql-scripter                    1.0.0a23
ndg-httpsclient                   0.5.1
oauthlib                          3.2.0
packaging                         21.3
paramiko                          2.11.0
pathspec                          0.9.0
pip                               22.2.1
pkginfo                           1.8.3
platformdirs                      2.5.2
portalocker                       1.7.1
psutil                            5.9.1
pyasn1                            0.4.8
pycparser                         2.21
pycrypto                          2.6.1
pycurl                            7.43.0.2
Pygments                          2.12.0
PyGObject                         3.30.4
PyJWT                             2.4.0
PyNaCl                            1.5.0
pyOpenSSL                         22.0.0
pyparsing                         3.0.9
PySocks                           1.7.1
python-apt                        1.8.4.3
python-dateutil                   2.8.2
pytz                              2022.1
pyxdg                             0.25
PyYAML                            6.0
requests                          2.27.1
requests-oauthlib                 1.3.1
resolvelib                        0.5.4
SecretStorage                     2.3.1
setuptools                        40.8.0
sfctl                             11.1.0
sfmergeutility                    0.1.6
six                               1.12.0
tabulate                          0.8.9
tqdm                              4.64.0
typing_extensions                 4.2.0
unattended-upgrades               0.1
urllib3                           1.26.9
virtualenv                        20.14.1
websocket-client                  1.3.3
wheel                             0.32.3
xmltodict                         0.13.0
zipp                              3.8.0

AD 2

 ansible-galaxy collection list
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with 
Ansible 2.12. Current version: 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0]. This feature 
will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.

# /usr/share/ansible/collections/ansible_collections
Collection         Version
------------------ -------
azure.azcollection 1.12.0 

AD 3 I must reverse engineered :)

Below you can find a list of commands I was executing trying to figure out where is the module to be replaced, I did install some extra packages, it may influence the path :) though in general - I know the error, I knew I could fix it by replacing the file from https://github.com/ansible-collections/azure/pull/870, On Azure console /usr/share/ansible/collections/ansible_collections/azure/azcollection/plugins/module_utils/azure_rm_common.py is not writable :) so the next thing was somwhere in .local or `Using module file /home/jakub/.ansible/collections/ansible_collections/azure/azcollection/plugins/modules/azure_rm_virtualmachine.py

via

ansible-playbook playbook.yml -vvv


TASK [Create VM] ***********************************************************************************
task path: /home/jakub/playbook.yml:48
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: jakub
<127.0.0.1> EXEC /bin/sh -c 'echo ~jakub && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/jakub/.ansible/tmp `"&& mkdir "` echo /home/jakub/.ansible/tmp/ansible-tmp-1659695672.58352-1108-114068632047280 `" && echo ansible-tmp-1659695672.58352-1108-114068632047280="` echo /home/jakub/.ansible/tmp/ansible-tmp-1659695672.58352-1108-114068632047280 `" ) && sleep 0'
redirecting (type: modules) ansible.builtin.azure_rm_virtualmachine to azure.azcollection.azure_rm_virtualmachine
Using module file /home/jakub/.ansible/collections/ansible_collections/azure/azcollection/plugins/modules/azure_rm_virtualmachine.py
<127.0.0.1> PUT /home/jakub/.ansible/tmp/ansible-local-7994svffctw/tmp38evimw8 TO /home/jakub/.ansible/tmp/ansible-tmp-1659695672.58352-1108-114068632047280/AnsiballZ_azure_rm_virtualmachine.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/jakub/.ansible/tmp/ansible-tmp-1659695672.58352-1108-114068632047280/ /home/jakub/.ansible/tmp/ansible-tmp-1659695672.58352-1108-114068632047280/AnsiballZ_azure_rm_virtualmachine.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3 /home/jakub/.ansible/tmp/ansible-tmp-1659695672.58352-1108-114068632047280/AnsiballZ_azure_rm_virtualmachine.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/jakub/.ansible/tmp/ansible-tmp-1659695672.58352-1108-114068632047280/ > /dev/null 2>&1 && sleep 0'
ok: [localhost] => {
4  cat > playbook.yml
    5  vi playbook.yml 
    6  ansible-playbook playbook.yml -vvv
    7  pip install azureml-core --upgrade
    8  ansible-playbook playbook.yml -vvv
    9  wget https://raw.githubusercontent.com/Fred-sun/ansible_collections_azure/Add_msiauthentication_support_to_track2/plugins/module_utils/azure_rm_common.py
   10  cp azure_rm_common.py /tmp/ansible_azure_rm_virtualnetwork_payload_sswaz0cd/ansible_azure_rm_virtualnetwork_payload.zip/ansible_collections/azure/azcollection/plugins/module_utils/azure_rm_common.py
   11  cp azure_rm_common.py /usr/share/ansible/collections/ansible_collections/azure/azcollection/plugins/module_utils/azure_rm_common.py 
   12  sudo cp azure_rm_common.py /usr/share/ansible/collections/ansible_collections/azure/azcollection/plugins/module_utils/azure_rm_common.py 
   13  ls
   14  virtualenv --python=python3 venv
   15  cd venv/
   16  pip install ansible
   17  ansible-galaxy collection install azure.azcollection
   18  sudo pip3 install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements-azure.txt
   19  ls
   20  pip3 install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements-azure.txt
   21  pip3 install --upgrade pip
   22  pip3 install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements-azure.txt
   23  sudo cp azure_rm_common.py /home/jakub/.local/lib/python3.7/site-packages/ansible/module_utils/
   24  cp azure_rm_common.py /home/jakub/.local/lib/python3.7/site-packages/ansible/module_utils/
   25  cp ~/azure_rm_common.py /home/jakub/.local/lib/python3.7/site-packages/ansible/module_utils/
   26  cp ~/azure_rm_common.py /home/jakub/.local/lib/python3.7/site-packages/ansible_collections/azure/azcollection/plugins/module_utils/
   27  ansible-galaxy collection install azure.azcollection
   28  ansible-playbook playbook.yml -vvv

AD3 EDIT actually path comes from :) ansible python module location

ansible-playbook test.play  -vvv
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with 
Ansible 2.12. Current version: 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0]. This feature 
will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
ansible-playbook [core 2.11.12] 
  config file = None
  configured module search path = ['/home/jakub/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/jakub/.local/lib/python3.7/site-packages/ansible

AD 4 not sure if I get it... this?

ansible-playbook test.play  -vvv
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with 
Ansible 2.12. Current version: 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0]. This feature 
will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
ansible-playbook [core 2.11.12] 
  config file = None
  configured module search path = ['/home/jakub/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/jakub/.local/lib/python3.7/site-packages/ansible
  ansible collection location = /home/jakub/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/jakub/.local/bin/ansible-playbook
  python version = 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0]
  jinja version = 3.1.2
  libyaml = True
No config file found; using defaults
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
yaml declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
ini declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
toml declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
[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'
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: test.play ********************************************************************************
1 plays in test.play

PLAY [Create Azure VM] *****************************************************************************

TASK [Gathering Facts] *****************************************************************************
task path: /home/jakub/test.play:2
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: jakub
<127.0.0.1> EXEC /bin/sh -c 'echo ~jakub && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/jakub/.ansible/tmp `"&& mkdir "` echo /home/jakub/.ansible/tmp/ansible-tmp-1659695272.4882576-309-120212072175509 `" && echo ansible-tmp-1659695272.4882576-309-120212072175509="` echo /home/jakub/.ansible/tmp/ansible-tmp-1659695272.4882576-309-120212072175509 `" ) && sleep 0'
Using module file /home/jakub/.local/lib/python3.7/site-packages/ansible/modules/setup.py
<127.0.0.1> PUT /home/jakub/.ansible/tmp/ansible-local-303lvye2tzm/tmp89x0rmlh TO /home/jakub/.ansible/tmp/ansible-tmp-1659695272.4882576-309-120212072175509/AnsiballZ_setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/jakub/.ansible/tmp/ansible-tmp-1659695272.4882576-309-120212072175509/ /home/jakub/.ansible/tmp/ansible-tmp-1659695272.4882576-309-120212072175509/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3 /home/jakub/.ansible/tmp/ansible-tmp-1659695272.4882576-309-120212072175509/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/jakub/.ansible/tmp/ansible-tmp-1659695272.4882576-309-120212072175509/ > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers
META: ran handlers

sirkubax avatar Aug 05 '22 10:08 sirkubax

@sirkubax This function will be added later than V1.13.0. The Azure Cloud Shell is still old, so it cannot be used directly. You are advised to create a VM and install Dev branch and try again. Or copy the #870 changes to the specified directory and try again. Also, V1.14.0 will be released soon! Thanks!

Fred-sun avatar Aug 07 '22 15:08 Fred-sun