azure icon indicating copy to clipboard operation
azure copied to clipboard

Import error handling fails with inventory plugin sometimes

Open AlanCoding opened this issue 4 years ago • 6 comments

SUMMARY

correct error is not reported when "azure" module is missing and azure inventory is used

ISSUE TYPE
  • Bug Report
COMPONENT NAME

plugins/inventory/azure_rm.py

ANSIBLE VERSION
$ ansible --version
[WARNING]: You are running the development version of Ansible. You should only run Ansible from "devel" if you are modifying
the Ansible engine, or trying out features under development. This is a rapidly changing source of code and can become unstable
at any point.
ansible 2.11.0.dev0 (devel 6bc1e9f5dd) last updated 2020/12/08 13:56:25 (GMT -400)
  config file = None
  configured module search path = ['/home/alancoding/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/alancoding/repos/ansible/lib/ansible
  ansible collection location = /home/alancoding/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/alancoding/repos/awx/env/bin/ansible
  python version = 3.9.0 (default, Oct  6 2020, 00:00:00) [GCC 10.2.1 20200826 (Red Hat 10.2.1-3)]
  jinja version = 2.11.2
  libyaml = False
CONFIGURATION

defaults

OS / ENVIRONMENT

N/A, control machine

STEPS TO REPRODUCE

With credentials set up:

ansible-inventory -i testing/awx_75_ug7azj7k/azure_rm.yml --list --export -vvv

I don't think the specifics of the inventory file matters. I have done:

pip install msrest msrestazure
EXPECTED RESULTS

error that "azure" is not installed

ACTUAL RESULTS

False Traceback

[WARNING]:  * Failed to parse /home/alancoding/repos/awx/testing/awx_75_ug7azj7k/azure_rm.yml with auto plugin: name
'ServicePrincipalCredentials' is not defined
  File "/home/alancoding/repos/ansible/lib/ansible/inventory/manager.py", line 290, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/home/alancoding/repos/ansible/lib/ansible/plugins/inventory/auto.py", line 58, in parse
    plugin.parse(inventory, loader, path, cache=cache)
  File "/home/alancoding/.ansible/collections/ansible_collections/azure/azcollection/plugins/inventory/azure_rm.py", line 205, in parse
    self._credential_setup()
  File "/home/alancoding/.ansible/collections/ansible_collections/azure/azcollection/plugins/inventory/azure_rm.py", line 226, in _credential_setup
    self.azure_auth = AzureRMAuth(**auth_options)
  File "/home/alancoding/.ansible/collections/ansible_collections/azure/azcollection/plugins/module_utils/azure_rm_common.py", line 1330, in __init__
    self.azure_credentials = ServicePrincipalCredentials(client_id=self.credentials['client_id'],

It looks like the code allows this line:

https://github.com/ansible-collections/azure/blob/0f31447bbf69acd5d4224a17bbd18235be22e872/plugins/module_utils/azure_rm_common.py#L1366

While it should not, if the value HAS_AZURE is False.

AlanCoding avatar Dec 14 '20 16:12 AlanCoding

@AlanCoding Thank you for reporting this issue, we will review it as soon as possible, thank you!

Fred-sun avatar Dec 21 '20 02:12 Fred-sun

@AlanCoding could you list the installed packages in your environment to help us reproduce the issue. I can't reproduce it.

haiyuazhang avatar Dec 23 '20 08:12 haiyuazhang

I received this same error

with auto plugin: name 'ServicePrincipalCredentials' is not defined

Installing azure==5.0.0 results in an error

Starting with v5.0.0, the 'azure' meta-package is deprecated and cannot be installed anymore.
    Please install the service specific packages prefixed by `azure` needed for your application.

However the terse message about ServicePrincipalCredentials in no way helps you to understand which package might be missing.

I can do one step better than a list of packages, I can give you a dockerfile: https://gist.github.com/simonjduff/198ab902b368ce0b1fafb1380f6f1994

To repro, simply build this into a docker image, and then use as an azure devops agent to run ansible-inventory -i azure_rm.yml --graph

My azure_rm.yml is simply:

plugin: azure_rm
auth_source: auto
keyed_groups:
- prefix: tag
  key: tags
2021-02-07T16:58:17.1062901Z ##[section]Starting: Ansible Run
2021-02-07T16:58:17.1076613Z ==============================================================================
2021-02-07T16:58:17.1077450Z Task         : Command line
2021-02-07T16:58:17.1078306Z Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
2021-02-07T16:58:17.1079126Z Version      : 2.178.0
2021-02-07T16:58:17.1079710Z Author       : Microsoft Corporation
2021-02-07T16:58:17.1080579Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
2021-02-07T16:58:17.1081593Z ==============================================================================
2021-02-07T16:58:17.2577988Z Generating script.
2021-02-07T16:58:17.2586854Z Script contents:
2021-02-07T16:58:17.2587976Z ansible-inventory -i azure_rm.yml --graph
2021-02-07T16:58:17.2589016Z ========================== Starting Command Output ===========================
2021-02-07T16:58:17.2612862Z [command]/usr/bin/bash --noprofile --norc /azp/agent/_work/_temp/bf97c4ae-99ac-49e9-a60a-b78bec3bcfc2.sh
2021-02-07T16:58:17.8425112Z [WARNING]:  * Failed to parse /azp/agent/_work/r1/a/Configuration/azure_rm.yml
2021-02-07T16:58:17.8428753Z with auto plugin: name 'ServicePrincipalCredentials' is not defined
2021-02-07T16:58:17.8432387Z [WARNING]:  * Failed to parse /azp/agent/_work/r1/a/Configuration/azure_rm.yml
2021-02-07T16:58:17.8435029Z with yaml plugin: Plugin configuration YAML file, not YAML inventory
2021-02-07T16:58:17.8437251Z [WARNING]:  * Failed to parse /azp/agent/_work/r1/a/Configuration/azure_rm.yml
2021-02-07T16:58:17.8439963Z with ini plugin: Invalid host pattern 'plugin:' supplied, ending in ':' is not
2021-02-07T16:58:17.8441883Z allowed, this character is reserved to provide a port.
2021-02-07T16:58:17.8443556Z [WARNING]: Unable to parse /azp/agent/_work/r1/a/Configuration/azure_rm.yml as
2021-02-07T16:58:17.8445004Z an inventory source
2021-02-07T16:58:17.8446417Z [WARNING]: No inventory was parsed, only implicit localhost is available
2021-02-07T16:58:17.8447737Z @all:
2021-02-07T16:58:17.8449155Z   |--@ungrouped:
2021-02-07T16:58:17.8940938Z ##[section]Finishing: Ansible Run

simonjduff avatar Feb 07 '21 16:02 simonjduff

Same here.

$ ansible-inventory -v -i inventory --graph
[WARNING]:  * Failed to parse myinv.yml with ansible_collections.azure.azcollection.plugins.inventory.azure_rm plugin: name
'ServicePrincipalCredentials' is not defined

Versions (installed via Pip):

% ansible-inventory --version
ansible-inventory [core 2.11.2]

% ansible --version
ansible [core 2.11.2]

% ansible-galaxy collection list azure.azcollection

# /usr/local/lib/python3.9/site-packages/ansible_collections
Collection         Version
------------------ -------
azure.azcollection 1.7.0

Auth:

auth_source: credential_file
plugin: azure.azcollection.azure_rm

Stack trace:

with ansible_collections.azure.azcollection.plugins.inventory.azure_rm plugin: name
'ServicePrincipalCredentials' is not defined
  File "/usr/local/lib/python3.9/site-packages/ansible/inventory/manager.py", line 290, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/local/lib/python3.9/site-packages/ansible_collections/azure/azcollection/plugins/inventory/azure_rm.py", line 219, in parse
    self._credential_setup()
  File "/usr/local/lib/python3.9/site-packages/ansible_collections/azure/azcollection/plugins/inventory/azure_rm.py", line 240, in _credential_setup
    self.azure_auth = AzureRMAuth(**auth_options)
  File "/usr/local/lib/python3.9/site-packages/ansible_collections/azure/azcollection/plugins/module_utils/azure_rm_common.py", line 1459, in __init__
    self.azure_credentials = ServicePrincipalCredentials(client_id=self.credentials['client_id'],

kenorb avatar Jun 30 '21 14:06 kenorb

[WARNING]: * Failed to parse myinv.yml with ansible_collections.azure.azcollection.plugins.inventory.azure_rm plugin: name 'ServicePrincipalCredentials' is not defined

Fixed by:

pip3 install azure

The module most likely disappeared after reinstalling Ansible.

kenorb avatar Jun 30 '21 14:06 kenorb

Since the meta package azure will be removed this can be installed with the following also:

pip3 install azure-mgmt-resource

d-rk avatar Aug 27 '21 09:08 d-rk

@AlanCoding Can you update to the latest version and try again? The previous authentication method for track1 has been deprecated, and the new package is azure-identity. This problem should have been avoided. Thank you!

Fred-sun avatar Jan 25 '24 08:01 Fred-sun