Ansible Tower-在剧本中使用Azure密钥保管库秘密

时间:2020-06-22 18:42:50

标签: azure ansible azure-keyvault ansible-tower

我目前正在尝试在剧本中使用Microsoft Azure Key Vault类型的凭据。通过一些谷歌搜索,我发现了一些有关指定Credential Types和指定自定义注入器的资源,但是如果可能的话,我更希望使用内置的证书来实现这一点。

当前,我已经从UI中正确配置和测试了密钥库凭据,并且能够使用我提供的服务主体详细信息来查找机密。 enter image description here

我希望使用该凭据通过诸如{{ my_kv_store }}:secret_name之类的命令来动态访问和查找我的剧本中的秘密。 实现这种功能是否可能?

谢谢。

1 个答案:

答案 0 :(得分:0)

决定不尝试使用Azure Key Vault,而是使用凭据类型路由,而不是剧本中的Azure cli。

我遵循的步骤:

新凭据类型: Azure服务主体

# Input Configuration
fields:
  - id: vault_url
    type: string
    label: Vault URL (DNS Name)
  - id: client_id
    type: string
    label: Client ID
  - id: client_secret
    type: string
    label: Client Secret
    secret: true
  - id: tenant_id
    type: string
    label: Tenant ID
required:
  - vault_url
  - client_id
  - client_secret
  - tenant_id

# Injector Configuration
env:
  AZ_CLIENT_ID: '{{ client_id }}'
  AZ_CLIENT_SECRET: '{{ client_secret }}'
  AZ_TENANT_ID: '{{ tenant_id }}'
  AZ_VAULT_URL: '{{ vault_url }}'

凭据配置示例

新凭据: Azure SP凭据

  1. 选择凭据类型
  2. 选择先前定义的凭据类型(Azure服务主体) image
  3. 提供必要的输入字段

在作业中使用凭据

  1. 工作模板内
    1. 选择 Azure SP Creds
  2. 在您的playbook.yml中
---
- name: grab test-secret from azure kv
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    kv_secret_name: "test-secret"
  tasks:
    - name: set facts
      set_fact:
        az_vault_name: "kv-237-gr-vnet-devops"
        az_tenant_id:   '{{ lookup("env", "AZ_TENANT_ID") }}'
        az_client_id:   '{{ lookup("env", "AZ_CLIENT_ID") }}'
        az_client_secret:  '{{ lookup("env", "AZ_CLIENT_SECRET") }}'
        az_vault_url:      '{{ lookup("env", "AZ_VAULT_URL") }}'
    - name: connect AZ CLI to Azure
      shell: |
        az login --service-principal -u "{{ az_client_id }}" -p "{{ az_client_secret }}" --tenant "{{ az_tenant_id }}"
      args:
        executable: /usr/bin/bash
    - name: Retrieve secret and register as var
      shell: az keyvault secret show --name "{{ kv_secret_name }}" --vault-name "{{ az_vault_name }}" --query value -o tsv
      args:
        executable: /usr/bin/bash
      register: azure_secret
    - debug:
        msg: "{{ azure_secret.stdout }}"
    
    # - name: Use creds to get KV secret
    #   azure_rm_keyvaultsecret_info
    #   debug: msg="the value of this secret is {{lookup('azure_kv',secretname,vault_url=url, client_id=client_id, secret=secret, tenant_id=tenant)}}"

- hosts: all
  vars:
    kv_secret: "{{ hostvars['localhost']['azure_secret']['stdout'] }}"
  tasks:
    - name: "Show the secret retrieved from localhost"
      shell: echo "{{ kv_secret }}"