azure_service_principal_attribute.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # (c) 2018 Yunge Zhu, <yungez@microsoft.com>
  2. # (c) 2017 Ansible Project
  3. # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
  4. from __future__ import (absolute_import, division, print_function)
  5. __metaclass__ = type
  6. DOCUMENTATION = """
  7. lookup: azure_service_principal_attribute
  8. requirements:
  9. - azure-graphrbac
  10. author:
  11. - Yunge Zhu <yungez@microsoft.com>
  12. version_added: "2.7"
  13. short_description: Look up Azure service principal attributes.
  14. description:
  15. - Describes object id of your Azure service principal account.
  16. options:
  17. azure_client_id:
  18. description: azure service principal client id.
  19. azure_secret:
  20. description: azure service principal secret
  21. azure_tenant:
  22. description: azure tenant
  23. azure_cloud_environment:
  24. description: azure cloud environment
  25. """
  26. EXAMPLES = """
  27. set_fact:
  28. object_id: "{{ lookup('azure_service_principal_attribute',
  29. azure_client_id=azure_client_id,
  30. azure_secret=azure_secret,
  31. azure_tenant=azure_secret) }}"
  32. """
  33. RETURN = """
  34. _raw:
  35. description:
  36. Returns object id of service principal.
  37. """
  38. from ansible.errors import AnsibleError
  39. from ansible.plugins import AnsiblePlugin
  40. from ansible.plugins.lookup import LookupBase
  41. from ansible.module_utils._text import to_native
  42. try:
  43. from azure.common.credentials import ServicePrincipalCredentials
  44. from azure.graphrbac import GraphRbacManagementClient
  45. from msrestazure import azure_cloud
  46. from msrestazure.azure_exceptions import CloudError
  47. except ImportError:
  48. raise AnsibleError(
  49. "The lookup azure_service_principal_attribute requires azure.graphrbac, msrest")
  50. class LookupModule(LookupBase):
  51. def run(self, terms, variables, **kwargs):
  52. self.set_options(direct=kwargs)
  53. credentials = {}
  54. credentials['azure_client_id'] = self.get_option('azure_client_id', None)
  55. credentials['azure_secret'] = self.get_option('azure_secret', None)
  56. credentials['azure_tenant'] = self.get_option('azure_tenant', 'common')
  57. if credentials['azure_client_id'] is None or credentials['azure_secret'] is None:
  58. raise AnsibleError("Must specify azure_client_id and azure_secret")
  59. _cloud_environment = azure_cloud.AZURE_PUBLIC_CLOUD
  60. if self.get_option('azure_cloud_environment', None) is not None:
  61. cloud_environment = azure_cloud.get_cloud_from_metadata_endpoint(credentials['azure_cloud_environment'])
  62. try:
  63. azure_credentials = ServicePrincipalCredentials(client_id=credentials['azure_client_id'],
  64. secret=credentials['azure_secret'],
  65. tenant=credentials['azure_tenant'],
  66. resource=_cloud_environment.endpoints.active_directory_graph_resource_id)
  67. client = GraphRbacManagementClient(azure_credentials, credentials['azure_tenant'],
  68. base_url=_cloud_environment.endpoints.active_directory_graph_resource_id)
  69. response = list(client.service_principals.list(filter="appId eq '{0}'".format(credentials['azure_client_id'])))
  70. sp = response[0]
  71. return sp.object_id.split(',')
  72. except CloudError as ex:
  73. raise AnsibleError("Failed to get service principal object id: %s" % to_native(ex))
  74. return False