-
Notifications
You must be signed in to change notification settings - Fork 1
/
az_kv_set_vm_policy.yml
46 lines (39 loc) · 1.57 KB
/
az_kv_set_vm_policy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ansible-playbook
# Example of adding access to a KV access to a VM using CLI
# and not module.
- hosts: localhost
gather_facts: false
tasks:
- block:
- name: "Obtain Service Principal information."
command: >
az resource
list
--resource-group "{{ az_resource_group }}"
--name "{{ azure_vm }}"
--query "[].identity.principalId"
--output tsv
register: __az_vm_principalId
changed_when: false
- name: "Obtain the current keyvault access for the Service Principal."
command: >
az keyvault
show
--name "{{ az_vault_name }}"
--resource-group "{{ az_resource_group }}"
--query 'properties.accessPolicies[?objectId == `{{ __az_vm_principalId.stdout }}`].permissions.secrets[]'
--output tsv
register: __az_vm_keyvault_access
changed_when: false
- name: "Modify the Key Vault access for VM."
command: >
az keyvault set-policy
--name "{{ az_vault_name }}"
--resource-group "{{ az_resource_group }}"
--object-id "{{ __az_vm_principalId.stdout }}"
--secret-permissions "{{ az_vm_policy_secret_permissions | sort | join }}"
register: __result
when: >
( __az_vm_keyvault_access.stdout.split() | sort | join )
!= ( az_vm_policy_secret_permissions | sort | join )