【问题标题】:Ansible Tower - Using Azure Key Vault Secrets Within PlaybooksAnsible Tower - 在 Playbook 中使用 Azure Key Vault Secrets
【发布时间】:2020-06-22 18:42:50
【问题描述】:
我目前正在尝试在剧本中使用 Microsoft Azure Key Vault 类型的凭据。通过一些谷歌搜索,我找到了一些关于指定 Credential Types 和指定自定义注入器的资源,但如果可能的话,我更愿意使用内置凭据来实现这一点。
目前,我已从 UI 正确配置和测试了我的密钥保管库凭据,并且能够使用我提供的服务主体详细信息找到机密。
我希望使用此凭据通过{{ my_kv_store }}:secret_name 之类的某种命令在我的剧本中动态访问和查找机密。
有可能实现这种功能吗?
提前致谢。
【问题讨论】:
标签:
azure
ansible
azure-keyvault
ansible-tower
【解决方案1】:
决定放弃尝试使用 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 凭据
- 选择凭据类型
- 选择之前定义的凭据类型(Azure 服务主体)
- 提供必要的输入字段
在您的工作中使用凭据
- 在作业模板内
- 选择 Azure SP 凭据
- 在您的 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 }}"