2019-03-06 20:09:54 +01:00
|
|
|
- name: Fix resource prefix
|
|
|
|
set_fact:
|
|
|
|
role_name: "{{ (resource_group | replace('-','x'))[-8:] }}{{ 1000 | random }}testrole"
|
|
|
|
subscription_id: "{{ lookup('env','AZURE_SUBSCRIPTION_ID') }}"
|
2019-03-21 05:48:55 +01:00
|
|
|
principal_id: "{{ lookup('env','AZURE_CLIENT_ID') }}"
|
2019-03-06 20:09:54 +01:00
|
|
|
run_once: yes
|
|
|
|
|
|
|
|
- name: Create a role definition (Check Mode)
|
|
|
|
azure_rm_roledefinition:
|
|
|
|
name: "{{ role_name }}"
|
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
permissions:
|
|
|
|
- actions:
|
|
|
|
- "Microsoft.Compute/virtualMachines/read"
|
|
|
|
not_actions:
|
|
|
|
- "Microsoft.Compute/virtualMachines/write"
|
|
|
|
data_actions:
|
|
|
|
- "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read"
|
|
|
|
not_data_actions:
|
|
|
|
- "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write"
|
|
|
|
assignable_scopes:
|
|
|
|
- "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
check_mode: yes
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- name: Assert creating role definition check mode
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- output.changed
|
|
|
|
|
|
|
|
- name: Create a role definition
|
|
|
|
azure_rm_roledefinition:
|
|
|
|
name: "{{ role_name }}"
|
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
permissions:
|
|
|
|
- actions:
|
|
|
|
- "Microsoft.Compute/virtualMachines/read"
|
|
|
|
not_actions:
|
|
|
|
- "Microsoft.Compute/virtualMachines/write"
|
|
|
|
data_actions:
|
|
|
|
- "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read"
|
|
|
|
not_data_actions:
|
|
|
|
- "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write"
|
|
|
|
assignable_scopes:
|
|
|
|
- "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- name: Assert creating role definition
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- output.changed
|
|
|
|
|
2019-04-09 10:05:03 +02:00
|
|
|
- name: Get facts by type
|
2019-04-03 17:03:47 +02:00
|
|
|
azure_rm_roledefinition_facts:
|
2019-03-06 20:09:54 +01:00
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
type: custom
|
|
|
|
register: facts
|
|
|
|
|
|
|
|
- name: Assert facts
|
|
|
|
assert:
|
2019-04-03 08:02:19 +02:00
|
|
|
that:
|
|
|
|
- facts['roledefinitions'] | length > 1
|
2019-03-06 20:09:54 +01:00
|
|
|
|
2019-04-09 10:05:03 +02:00
|
|
|
- name: Get facts by name
|
2019-04-03 17:03:47 +02:00
|
|
|
azure_rm_roledefinition_facts:
|
2019-03-06 20:09:54 +01:00
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
role_name: "{{ role_name }}"
|
|
|
|
register: facts
|
2019-05-17 07:41:30 +02:00
|
|
|
until: facts.roledefinitions | length > 0
|
2019-04-09 10:05:03 +02:00
|
|
|
retries: 50
|
|
|
|
delay: 60
|
2019-03-06 20:09:54 +01:00
|
|
|
|
|
|
|
- name: Assert facts
|
|
|
|
assert:
|
2019-04-03 08:02:19 +02:00
|
|
|
that:
|
|
|
|
- facts['roledefinitions'] | length == 1
|
2019-04-09 10:05:03 +02:00
|
|
|
- facts['roledefinitions'][0]['permissions'] | length == 1
|
|
|
|
- facts['roledefinitions'][0]['permissions'][0]['not_data_actions'] | length == 1
|
|
|
|
- facts['roledefinitions'][0]['permissions'][0]['data_actions'] | length == 1
|
2019-03-06 20:09:54 +01:00
|
|
|
|
|
|
|
- name: Update the role definition (idempotent)
|
|
|
|
azure_rm_roledefinition:
|
|
|
|
name: "{{ role_name }}"
|
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
permissions:
|
|
|
|
- actions:
|
|
|
|
- "Microsoft.Compute/virtualMachines/read"
|
|
|
|
not_actions:
|
|
|
|
- "Microsoft.Compute/virtualMachines/write"
|
|
|
|
data_actions:
|
|
|
|
- "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read"
|
|
|
|
not_data_actions:
|
|
|
|
- "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write"
|
|
|
|
assignable_scopes:
|
|
|
|
- "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- name: assert output not changed
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- not output.changed
|
|
|
|
|
|
|
|
- name: Update the role definition
|
|
|
|
azure_rm_roledefinition:
|
|
|
|
name: "{{ role_name }}"
|
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
permissions:
|
|
|
|
- actions:
|
|
|
|
- "Microsoft.Compute/virtualMachines/read"
|
|
|
|
- "Microsoft.Compute/virtualMachines/start/action"
|
|
|
|
not_actions:
|
|
|
|
- "Microsoft.Compute/virtualMachines/write"
|
|
|
|
data_actions:
|
|
|
|
- "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read"
|
|
|
|
not_data_actions:
|
|
|
|
- "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write"
|
|
|
|
assignable_scopes:
|
|
|
|
- "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- name: assert output changed
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- output.changed
|
|
|
|
|
2019-03-21 05:48:55 +01:00
|
|
|
- name: Get role definition facts
|
2019-04-03 17:03:47 +02:00
|
|
|
azure_rm_roledefinition_facts:
|
2019-03-21 05:48:55 +01:00
|
|
|
role_name: "{{ role_name }}"
|
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
type: custom
|
|
|
|
register: roledef
|
2019-04-09 10:05:03 +02:00
|
|
|
until: "{{ roledef.roledefinitions | length > 0 }}"
|
|
|
|
retries: 50
|
|
|
|
delay: 60
|
2019-03-21 05:48:55 +01:00
|
|
|
|
|
|
|
- name: Assert role definition facts
|
|
|
|
assert:
|
|
|
|
that:
|
2019-04-09 10:05:03 +02:00
|
|
|
- roledef['roledefinitions'] | length == 1
|
2019-03-21 05:48:55 +01:00
|
|
|
- roledef['roledefinitions'][0]['id']
|
|
|
|
|
|
|
|
- name: Create a role assignment (Check Mode)
|
|
|
|
azure_rm_roleassignment:
|
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
assignee_object_id: "{{ principal_id }}"
|
|
|
|
role_definition_id: "{{ roledef['roledefinitions'][0]['id'] }}"
|
2019-04-09 10:05:03 +02:00
|
|
|
check_mode: yes
|
2019-03-21 05:48:55 +01:00
|
|
|
register: output
|
|
|
|
|
|
|
|
- name: Assert creating role definition check mode
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- output.changed
|
|
|
|
|
|
|
|
- name: Create a role assignment
|
|
|
|
azure_rm_roleassignment:
|
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
assignee_object_id: "{{ principal_id }}"
|
|
|
|
role_definition_id: "{{ roledef['roledefinitions'][0]['id'] }}"
|
|
|
|
register: output
|
|
|
|
|
2019-04-09 10:05:03 +02:00
|
|
|
- name: Assert creating role assignment
|
2019-03-21 05:48:55 +01:00
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- output.changed
|
|
|
|
|
|
|
|
- name: Get facts
|
2019-04-03 17:03:47 +02:00
|
|
|
azure_rm_roleassignment_facts:
|
2019-03-21 05:48:55 +01:00
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
|
|
|
assignee: "{{ principal_id }}"
|
2019-04-09 10:05:03 +02:00
|
|
|
role_definition_id: "{{ roledef['roledefinitions'][0]['id'] }}"
|
2019-03-21 05:48:55 +01:00
|
|
|
register: facts
|
|
|
|
|
|
|
|
- name: assert role assignment facts
|
|
|
|
assert:
|
|
|
|
that:
|
2019-04-09 10:05:03 +02:00
|
|
|
- facts['roleassignments'] | length > 0
|
2019-03-21 05:48:55 +01:00
|
|
|
- facts['roleassignments'][0]['id']
|
|
|
|
|
|
|
|
- name: delete role assignment
|
|
|
|
azure_rm_roleassignment:
|
2019-04-09 10:05:03 +02:00
|
|
|
name: "{{ facts['roleassignments'][0]['id'].split('/')[-1] }}"
|
|
|
|
scope: "/subscriptions/{{ subscription_id }}"
|
2019-03-21 05:48:55 +01:00
|
|
|
state: absent
|
|
|
|
|
2019-03-06 20:09:54 +01:00
|
|
|
- name: Delete the role definition (Check Mode)
|
|
|
|
azure_rm_roledefinition:
|
|
|
|
name: "{{ role_name }}"
|
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
2019-04-09 10:05:03 +02:00
|
|
|
state: absent
|
2019-03-06 20:09:54 +01:00
|
|
|
check_mode: yes
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- name: assert deleting role definition check mode
|
|
|
|
assert:
|
|
|
|
that: output.changed
|
|
|
|
|
2019-04-09 10:05:03 +02:00
|
|
|
- name: Delete the role definition
|
2019-03-06 20:09:54 +01:00
|
|
|
azure_rm_roledefinition:
|
|
|
|
name: "{{ role_name }}"
|
|
|
|
scope: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ resource_group }}"
|
2019-04-09 10:05:03 +02:00
|
|
|
state: absent
|
2019-03-06 20:09:54 +01:00
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
2019-05-17 07:41:30 +02:00
|
|
|
- output.changed
|