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

Fix ansible can use azure-cli credentials #31871

Closed
wants to merge 12 commits into from

Conversation

yuwzho
Copy link
Contributor

@yuwzho yuwzho commented Oct 18, 2017

SUMMARY

Ansible can use Azure-CLI's credential to manage azure resources.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

azure_rm_common

ANSIBLE VERSION
2.4.0.0
ADDITIONAL INFORMATION

User can login the AzureCLI and select the subscription via the following command. Then if the playbook hasn't specific the credentials, it will use AzureCLI's credential to manage the Azure resources.

az login
az account set -s <subscriptionId>

- For authentication with Azure you can pass parameters, set environment variables or use a profile stored
in ~/.azure/credentials. Authentication is possible using a service principal or Active Directory user.
- For authentication with Azure you can pass parameters, set environment variables, use a profile stored
in ~/.azure/credentials or login your AzureCLI.
Copy link
Contributor

Choose a reason for hiding this comment

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

"login your Azure CLI" -> "login with Azure CLI"

@@ -82,4 +83,5 @@ class ModuleDocFragment(object):
a [default] section and the following keys: subscription_id, client_id, secret and tenant or
subscription_id, ad_user and password. It is also possible to add additional profiles. Specify the profile
by passing profile or setting AZURE_PROFILE in the environment."
- Use 'az login' to login your AzureCLI.
Copy link
Contributor

Choose a reason for hiding this comment

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

AzureCLI -> Azure CLI

elif self.credentials.get('credentials') is not None:
self.azure_credentials = self.credentials.get('credentials')

if not self.azure_credentials:
self.fail("Failed to authenticate with provided credentials. Some attributes were missing. "
"Credentials must include client_id, secret and tenant or ad_user and password or "
"be logged using AzureCLI.")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think still here we should change to "be logged in using Azure CLI"

@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 bugfix_pull_request module_utils/azure_rm_common needs_triage Needs a first human triage before being processed. new_contributor This PR is the first contribution by a new community member. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Oct 18, 2017
@@ -82,4 +83,5 @@ class ModuleDocFragment(object):
a [default] section and the following keys: subscription_id, client_id, secret and tenant or
subscription_id, ad_user and password. It is also possible to add additional profiles. Specify the profile
by passing profile or setting AZURE_PROFILE in the environment."
- Use 'az login' to login your Azure CLI.
Copy link
Contributor

Choose a reason for hiding this comment

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

and here "your" -> "with"

- For authentication with Azure you can pass parameters, set environment variables or use a profile stored
in ~/.azure/credentials. Authentication is possible using a service principal or Active Directory user.
- For authentication with Azure you can pass parameters, set environment variables, use a profile stored
in ~/.azure/credentials or login with Azure CLI.
Copy link
Contributor

Choose a reason for hiding this comment

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

C(~/.azure/credentials) will give formatted text

http://docs.ansible.com/ansible/latest/dev_guide/developing_modules_documenting.html#formatting-options

Could you please update the other docs, for example C(AZURE_SUBSCRIPTION_ID), etc?

Thanks in advance

@gundalow gundalow added azure cloud and removed needs_triage Needs a first human triage before being processed. labels Oct 18, 2017
@ansibot
Copy link
Contributor

ansibot commented Oct 18, 2017

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Oct 18, 2017
@yuwzho
Copy link
Contributor Author

yuwzho commented Oct 19, 2017

@gundalow Thanks for your review. I have format the option value in the document.

@yuwzho
Copy link
Contributor Author

yuwzho commented Oct 20, 2017

@gundalow

@@ -67,19 +67,27 @@ class ModuleDocFragment(object):
C(AzureUSGovernment)), or a metadata discovery endpoint URL (required for Azure Stack). Can also be set via credential file profile or
the C(AZURE_CLOUD_ENVIRONMENT) environment variable.
default: AzureCloud
cli_default_profile:
description:
- Set to C(true), when login with Azure CLI
Copy link
Contributor

Choose a reason for hiding this comment

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

This description doesn't make sense to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about Whether login with Azure CLI?

Copy link
Contributor

Choose a reason for hiding this comment

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

Whether login with Azure CLI sounds better.
What happens if this is false?
Forgive my basic questions, I've not used Azure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

false is the same as NONE, this is a nullable option

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Has updated

cli_default_profile:
description:
- Set to C(true), when login with Azure CLI
required: false
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is a new option then please add
version_added: "2.5"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is a bugfix, the code has this option already, but the document haven't mention this option before.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, OK. Thanks for clarification

@ansibot
Copy link
Contributor

ansibot commented Oct 24, 2017

@yuwzho this PR contains the following merge commits:

Please rebase your branch to remove these commits.

click here for bot help

@ansibot ansibot added merge_commit This PR contains at least one merge commit. Please resolve! needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html labels Oct 24, 2017
@ansibot
Copy link
Contributor

ansibot commented Oct 30, 2017

The test ansible-test sanity --test rstcheck [?] failed with the following errors:

docs/docsite/rst/guide_azure.rst:76:0: (bash) `$ az account set -s <your-subscription-id>'
docs/docsite/rst/guide_azure.rst:76:0: (bash) syntax error near unexpected token `newline'

click here for bot help

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Oct 30, 2017
@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Oct 30, 2017
@yuwzho
Copy link
Contributor Author

yuwzho commented Oct 31, 2017

Hi @haroldwongms @nitzmahone @tstringer , please help take a look at this PR. Thanks very much!

@haroldwongms
Copy link
Contributor

This is not working for me. I hit the error check in lines 245 - 248 when getting credentials.

@nitzmahone
Copy link
Member

nitzmahone commented Oct 31, 2017

I missed zapping the UI element for this from 2.4 because it wasn't fully implemented. We definitely want this functionality, but we're talking about a slightly different way to expose it that will be consistent across Ansible cloud modules (probably a new credential_source arg that will default to auto, which would support the current behavior (and we'd probably just add cli as the lowest value in the chain), but also support explicit values like env,module,profile, etc.

@haroldwongms
Copy link
Contributor

@nitzmahone You bring up a good point in terms of order of preference. I'm thinking cli should be the highest priority in the chain.

@nitzmahone
Copy link
Member

That's not typically the way Ansible does it though- usually we go from closest/most-explicit definition out (some are arguably at the same level, so we choose arbitrarily in those cases and try to be consistent across providers). So module args > env > credential file on disk > CLI would probably be how I'd do it at first blush. For a typical intro cloud-shell user that's using ambient CLI auth, it'll still "just work" because that'd be all they have specified, but that way if someone needs to take more control at a different level (without explicitly specifying the source in the task), they still can.

@yuwzho
Copy link
Contributor Author

yuwzho commented Nov 1, 2017

@haroldwongms I think you are not set the cli_default_profile and meet such error. As currently behavior, the priority is CLI > profile > env var > auth file, so that using CLI should explicitly tell ansible use the CLI auth.

@nitzmahone
This PR is only for fixing login with Azure CLI's functional work. Since current error message tells user can login with CLI, but it is not work. That is why I treat this PR as a bug fix.

As for the ansible cloud modules' auth priority, it is another thing we need to think twice and make all the cloud modules consistent.

@ansibot
Copy link
Contributor

ansibot commented Nov 1, 2017

@trstringer
Copy link
Contributor

If I'm understanding @nitzmahone correctly, I'm in agreement with him. Environment variables typically by convention take precedence in Ansible or otherwise. If the end user sets them it should be assumed those are the credentials that you want to use. It's quite intentional. If we were to have CLI profile credentials take precedence then we would run into really bad behaviour if the user explicitly sets env vars for connection and credentials. Best case, that's annoying. Worst case, that's disastrous as they could mutate Azure resources in an unintended subscription.

@haroldwongms
Copy link
Contributor

@tstringer Totally understand.

@yuwzho I did set both options and neither option worked for me.

@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. and removed new_contributor This PR is the first contribution by a new community member. labels Nov 9, 2017
@ansibot
Copy link
Contributor

ansibot commented Jan 20, 2018

@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jan 20, 2018
@nitzmahone
Copy link
Member

Superseded by #35213 (the new auth_source arg)- since cli_default_profile was never fully hooked up or documented, we just removed it completely sans deprecation.

@nitzmahone nitzmahone closed this Jan 23, 2018
@yuwzho yuwzho deleted the yuwzho-cred branch January 23, 2018 08:08
@yuwzho yuwzho restored the yuwzho-cred branch January 23, 2018 08:08
@yuwzho yuwzho deleted the yuwzho-cred branch February 1, 2018 12:06
@ansibot ansibot added docs This issue/PR relates to or includes documentation. bug This issue/PR relates to a bug. and removed docs_pull_request labels Mar 4, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 azure bug This issue/PR relates to a bug. cloud docs This issue/PR relates to or includes documentation. module_utils/azure_rm_common needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants