Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect AZURE_CLIENT_ID, ANSIBLE_AZURE_AUTH_SOURCE on inventory plugin #713

Merged
merged 11 commits into from
Mar 22, 2024
3 changes: 2 additions & 1 deletion plugins/doc_fragments/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class ModuleDocFragment(object):
type: str
client_id:
description:
- Azure client ID. Use when authenticating with a Service Principal.
- Azure client ID. Use when authenticating with a Service Principal or Managed Identity (msi).
kingsleyadam marked this conversation as resolved.
Show resolved Hide resolved
Can also be set via the C(AZURE_CLIENT_ID) environment variable.
kingsleyadam marked this conversation as resolved.
Show resolved Hide resolved
type: str
secret:
description:
Expand Down
4 changes: 3 additions & 1 deletion plugins/inventory/azure_rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
except ImportError:
from Queue import Queue, Empty

from os import environ
from collections import namedtuple
from ansible import release
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
Expand Down Expand Up @@ -222,8 +223,9 @@ def parse(self, inventory, loader, path, cache=True):
raise

def _credential_setup(self):
auth_source = environ.get('ANSIBLE_AZURE_AUTH_SOURCE', None) or self.get_option('auth_source')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kingsleyadam I think this order should be adjusted, because the auth_source of the configuration is fetched first, and if the fetch is auto, the environment variable is fetched, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kingsleyadam I think this order should be adjusted, because the auth_source of the configuration is fetched first, and if the fetch is auto, the environment variable is fetched, thanks!

That's the issue I was running into. If I run self.get_option('auth_source') first, it has a default value of auto. Which won't fetch the environment variable ANSIBLE_AZURE_AUTH_SOURCE value. To avoid this my logic here is to first check the environment variable for the auth source, if it's not set then fetch it from the config.

auth_options = dict(
auth_source=self.get_option('auth_source'),
auth_source=auth_source,
profile=self.get_option('profile'),
subscription_id=self.get_option('subscription_id'),
client_id=self.get_option('client_id'),
Expand Down
1 change: 1 addition & 0 deletions plugins/module_utils/azure_rm_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,7 @@ def _get_profile(self, profile="default"):
return None

def _get_msi_credentials(self, subscription_id=None, client_id=None, **kwargs):
client_id = client_id or self._get_env('client_id')
credentials = MSIAuthentication(client_id=client_id)
subscription_id = subscription_id or self._get_env('subscription_id')
if not subscription_id:
Expand Down