【问题标题】:How to fetch Azure RM storage account keys in Ansible?如何在 Ansible 中获取 Azure RM 存储帐户密钥?
【发布时间】:2017-08-19 01:00:39
【问题描述】:

我正在使用 Ansible 创建一个 Azure RM 存储帐户,我想获取访问密钥的值以供以后在模板中使用。这些值在 Azure 端生成。例如,我可以使用 PowerShell Get-AzureStorageKey cmdlet 获取它们。

但是,azure_rm_storageaccount module 的返回值和使用 azure_rm_storageaccount_facts module 收集的事实都不包含这些键。

我想我可以使用 REST API 调用(每个 this answer)来获取它们,但我必须为此任务创建一个 OAuth2 令牌。对于 REST API,可能无法使用为 Ansible 定义的一组凭据(即环境变量 AZURE_CLIENT_IDAZURE_SECRETAZURE_SUBSCRIPTION_IDAZURE_TENANT)。

有没有办法获取这些密钥(使用已经提供给 Ansible 的凭据)?


事实上,Ansible 库似乎包含 the code for fetching these keys,但它们似乎也只在内部使用。


我的剧本:

---
- hosts: localhost
  connection: local

  vars:
    resource_group_name: fetchtest01
    resource_group_location: southcentralus
    storage_account: fdsahf343u2s
    storage_account_type: Standard_LRS

  tasks:
    - name: Ensure resource group "{{ resource_group_name }}" exists
      azure_rm_resourcegroup:
        name: "{{ resource_group_name }}"
        location: "{{ resource_group_location }}"

    - name: Ensure storage account "{{ storage_account }}" exists in "{{ resource_group_name }}" resource group
      azure_rm_storageaccount:
        resource_group: "{{ resource_group_name }}"
        name: "{{ storage_account }}"
        account_type: "{{ storage_account_type }}"

   - name: Fetch storage account keys
     # fetch storage_account_keys

   - name: Use the storage_account_keys.primary in a template 
     template:
       # ...

【问题讨论】:

    标签: azure ansible


    【解决方案1】:

    由于我没有与 Ansible 合作过,因此不能直接回答您的问题,因此我将解释为什么您无法使用 azure_rm_storageaccount_facts 模块获取存储帐户密钥。

    基本上在 Azure 资源管理器中,您需要某些类型的权限才能执行操作。由于您可能会更新存储帐户中的数据,因此团队已将获取存储帐户属性和密钥的操作拆分为两个单独的操作。要获取属性,您将执行不返回键的Get Properties 操作。要获取密钥,您需要执行List Keys

    我相信azure_rm_storageaccount_facts 只执行第一个操作(即获取属性),这就是您没有获得密钥的原因。我查看了所有与 Azure 相关的操作here,但找不到返回密钥的操作。


    如果使用 PowerShell 是一个选项,那么您要使用的 Cmdlet 是 Get-AzureRmStorageAccountKey 而不是 Get-AzureStorageKey,因为这是针对 Classic 存储帐户的。

    【讨论】:

    • :)。 Do you happen to know, if the keys are at all accessible using any method utilising the AZURE_CLIENT_ID, AZURE_SECRET, AZURE_SUBSCRIPTION_ID, AZURE_TENANT credentials? - 是的。这本质上是一个Service Principal。您只需确保此服务主体有权执行 List Keys 操作。您可以将此服务主体置于Storage Account Contributor 角色中。但是它也会使用 OAuth2。
    • 是的,起初我以为有一些更深的分离,但这只是两个不同的调用。实际上有fetching these keys in Ansible 的代码。追踪如何使用它...
    【解决方案2】:

    这是一个 bash sn-p,它会给你一个 KEY。该命令返回 2 个键,这将为您提供其中一个。假设您已安装 jq 和 azure cli。

    KEY=$(az storage account keys list --resource-group ${RG_NAME} --account-name ${STORAGE_NAME} | jq -r '.[].value' | head -1)

    【讨论】:

      【解决方案3】:

      这周我在同一个问题上苦苦挣扎。我想要一种检索这些密钥的方法,最后我最终使用了 Azure 服务原则和 Azure REST api。如文档https://docs.ansible.com/ansible/2.6/scenario_guides/guide_azure.html#storing-in-a-file

      中所述,我将凭据存储在我的笔记本电脑上 ~/.azure/credentials

      我还使用了具有多个服务主体的 azure 凭据配置文件,并在命令行中指定了 AZURE_PROFILE 环境变量,如下所示:

      ansible-playbook -e AZURE_PROFILE="profile-dev" -i inventories/local playbooks/azure_sas_token.yml -vvv
      

      azure_sas_token.yml 剧本

      ---
      - name: get sas token and storage account keys
        hosts: 127.0.0.1
        become: no
      
        vars:
          az_subscription_id: "{{ lookup('ini',  'subscription_id section={{ AZURE_PROFILE }}  file={{ ansible_env.HOME }}/.azure/credentials') }}"
          az_client_id: "{{ lookup('ini',  'client_id section={{ AZURE_PROFILE }}  file={{ ansible_env.HOME }}/.azure/credentials') }}"
          az_tenant_id: "{{ lookup('ini',  'tenant section={{ AZURE_PROFILE }}  file={{ ansible_env.HOME }}/.azure/credentials') }}"
          az_secret: "{{ lookup('ini',  'secret section={{ AZURE_PROFILE }}  file={{ ansible_env.HOME }}/.azure/credentials') }}"
      
        tasks:
          - name: get sas token through oauth2
            uri:
              url: "https://login.windows.net/{{ az_tenant_id }}/oauth2/token"
              method: POST
              body: "resource=https://management.core.windows.net&client_id={{ az_client_id }}&grant_type=client_credentials&client_secret={{ az_secret }}"
              return_content: yes
            register: sas_token_info
            no_log: true
      
          - name: get the storage account keys
            uri:
              url: "https://management.azure.com/subscriptions/{{ az_subscription_id }}/resourceGroups/{{ resource_group }}/providers/Microsoft.Storage/storageAccounts/{{ storage_account }}/listKeys?api-version=2016-12-01"
              method: POST
              headers:
                Authorization: "Bearer {{ sas_token_info.json.access_token }}"
              return_content: yes
            register: storage_account_keys
      
          - debug:
              msg: "{{ storage_account_keys.json['keys'].0.value }}"
      
          - debug:
              msg: "{{ storage_account_keys.json['keys'].1.value }}"
      

      使用这 2 个任务,我能够获取存储帐户的密钥,然后我能够自动创建文件共享并装载我的 cifs 驱动器。

      希望它可以帮助某人。

      【讨论】:

        【解决方案4】:

        将 Azure Cli 包装在一个任务中,

          tasks:
            - name: Retrieve storage access key
              shell: az storage account keys list --account-name {{ storage_account.name }} --resource-group {{ azure.resource_group }} --query "[0].value" --output tsv
              register: storage_access_key
        

        现在, storage_access_key 将包含所需的结果。

        【讨论】:

        • 缺少重要的部分。您将需要使用 storage_access_key.stdout 来获取该密钥(至少在 ansible 2.8 中)。
        【解决方案5】:

        现在 azure_rm_storageaccount_facts 模块已支持显示帐户的访问密钥。实际上,它可以通过在前面设置show_connection_string: true 来显示连接字符串。为了方便使用,现在通过设置show_connection_string: true,它还将包含一个名为key 的字段,其中包含访问密钥。

        例子可以像下面这样

        - name: Get facts for one account
          azure_rm_storageaccount_facts:
            resource_group: myResourceGroup
            name: clh0002
            show_connection_string: true
        

        返回的字典将包含密钥

        primary_endpoints:
        {
        blob:
        table:
        queue:
        key:
        }
        

        【讨论】:

          猜你喜欢
          • 2017-05-01
          • 2020-11-05
          • 2019-12-16
          • 2019-09-17
          • 2019-01-14
          • 1970-01-01
          • 2018-10-09
          • 2018-02-27
          • 1970-01-01
          相关资源
          最近更新 更多