Zim's Notes

Just work related notes.

Diving Into Azure_rm_deployment

It’s time to refresh azure_rm_deployment.

Currently we have following problems with azure_rm_deployment

  • it’s a kind of VM oriented only
  • no proper facts module, no proper information about created resources, so it’s difficult to build on the top of resources created by ARM deployment
  • deleting deployment actually deletes entire resource group, so no convenient way to have any additional resources in the same resource group
  • default deployment name (user can optionally specify) is confusing and inconsistent with other resources

What we can fix, and it’s quite easy: - facts module that lists all the resource ids in dictionary by resource type - properly deleting resources created by deployment, enabling users to have more than one deployment in a resource group

Let’s start with a simple playbook. I have just taken it from integration tests:

How it currently works?

Just a simple playbook that creates a virtual machine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- name: Create Azure Deploy
    azure_rm_deployment:
    resource_group: ""
    location: eastus2
    template_link: 'https://raw.githubusercontent.com/Azure/azure-quickstart-templates/d01a5c06f4f1bc03a049ca17bbbd6e06d62657b3/101-vm-simple-linux/azuredeploy.json'
    deployment_name: ""
    parameters:
        adminUsername:
        value: chouseknecht
        adminPassword:
        value: password123!
        dnsLabelPrefix:
        value: ""
        ubuntuOSVersion:
        value: "16.04.0-LTS"
    register: output

- debug:
    var: output

and the output looks like this:

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
{
    "output": {
        "changed": true,
        "deployment": {
            "group_name": "zimsdeployment",
            "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/zimsdeployment/providers/Microsoft.Resources/deployments/zimsdeployment",
            "instances": [
                {
                    "ips": [
                        {
                            "dns_settings": {
                                "domain_name_label": "zimsdeployment",
                                "fqdn": "zimsdeployment.eastus2.cloudapp.azure.com"
                            },
                            "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/zimsdeployment/providers/Microsoft.Network/publicIPAddresses/myPublicIP",
                            "name": "myPublicIP",
                            "public_ip": "23.100.65.195",
                            "public_ip_allocation_method": "Dynamic"
                        }
                    ],
                    "vm_name": "MyUbuntuVM"
                }
            ],
            "name": "zimsdeployment",
            "outputs": {
                "hostname": {
                    "type": "String",
                    "value": "zimsdeployment.eastus2.cloudapp.azure.com"
                },
                "sshCommand": {
                    "type": "String",
                    "value": "ssh chouseknecht@zimsdeployment.eastus2.cloudapp.azure.com"
                }
            }
        },
        "failed": false,
        "msg": "deployment succeeded"
    }
}

What is actually returned by Azure REST API?

I have created following task to find it out:

1
2
3
4
5
6
7
8
9
10
- name: Get deployment
    azure_rm_resource_facts:
    api_version: '2019-03-01'
    resource_group: ""
    provider: resources
    resource_type: deployments
    resource_name:  ""
    register: output
- debug:
    var: output

As you see below it actually returns quite a lot: - list of all created resource IDs - list of all dependencies between resources

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{
    "output": {
        "changed": false,
        "failed": false,
        "response": [
            {
                "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Resources/deployments/zimsdplxxxx",
                "name": "zimsdplxxxx",
                "properties": {
                    "correlationId": "0c26c003-f147-4f18-95c6-a28e43630dd1",
                    "dependencies": [
                        {
                            "dependsOn": [
                                {
                                    "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Network/publicIPAddresses/myPublicIP",
                                    "resourceName": "myPublicIP",
                                    "resourceType": "Microsoft.Network/publicIPAddresses"
                                },
                                {
                                    "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Network/virtualNetworks/MyVNET",
                                    "resourceName": "MyVNET",
                                    "resourceType": "Microsoft.Network/virtualNetworks"
                                }
                            ],
                            "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Network/networkInterfaces/myVMNic",
                            "resourceName": "myVMNic",
                            "resourceType": "Microsoft.Network/networkInterfaces"
                        },
                        {
                            "dependsOn": [
                                {
                                    "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Storage/storageAccounts/iovbjgl443l64sawinvm",
                                    "resourceName": "iovbjgl443l64sawinvm",
                                    "resourceType": "Microsoft.Storage/storageAccounts"
                                },
                                {
                                    "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Network/networkInterfaces/myVMNic",
                                    "resourceName": "myVMNic",
                                    "resourceType": "Microsoft.Network/networkInterfaces"
                                }
                            ],
                            "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Compute/virtualMachines/SimpleWinVM",
                            "resourceName": "SimpleWinVM",
                            "resourceType": "Microsoft.Compute/virtualMachines"
                        }
                    ],
                    "duration": "PT46.927468S",
                    "mode": "Incremental",
                    "outputResources": [
                        {
                            "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Compute/virtualMachines/SimpleWinVM"
                        },
                        {
                            "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Network/networkInterfaces/myVMNic"
                        },
                        {
                            "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Network/publicIPAddresses/myPublicIP"
                        },
                        {
                            "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Network/virtualNetworks/MyVNET"
                        },
                        {
                            "id": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.Storage/storageAccounts/iovbjgl443l64sawinvm"
                        }
                    ],
                    "outputs": {
                        "hostname": {
                            "type": "String",
                            "value": "zimsdplxxxx.eastus2.cloudapp.azure.com"
                        }
                    },
                    "parameters": {
                        "adminPassword": {
                            "type": "SecureString"
                        },
                        "adminUsername": {
                            "type": "String",
                            "value": "chouseknecht"
                        },
                        "dnsLabelPrefix": {
                            "type": "String",
                            "value": "zimsdplxxxx"
                        },
                        "location": {
                            "type": "String",
                            "value": "eastus2"
                        },
                        "windowsOSVersion": {
                            "type": "String",
                            "value": "2016-Datacenter"
                        }
                    },
                    "providers": [
                        {
                            "namespace": "Microsoft.Storage",
                            "resourceTypes": [
                                {
                                    "locations": [
                                        "eastus2"
                                    ],
                                    "resourceType": "storageAccounts"
                                }
                            ]
                        },
                        {
                            "namespace": "Microsoft.Network",
                            "resourceTypes": [
                                {
                                    "locations": [
                                        "eastus2"
                                    ],
                                    "resourceType": "publicIPAddresses"
                                },
                                {
                                    "locations": [
                                        "eastus2"
                                    ],
                                    "resourceType": "virtualNetworks"
                                },
                                {
                                    "locations": [
                                        "eastus2"
                                    ],
                                    "resourceType": "networkInterfaces"
                                }
                            ]
                        },
                        {
                            "namespace": "Microsoft.Compute",
                            "resourceTypes": [
                                {
                                    "locations": [
                                        "eastus2"
                                    ],
                                    "resourceType": "virtualMachines"
                                }
                            ]
                        }
                    ],
                    "provisioningState": "Succeeded",
                    "templateHash": "17224794003184781395",
                    "templateLink": {
                        "contentVersion": "1.0.0.0",
                        "uri": "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-windows/azuredeploy.json"
                    },
                    "timestamp": "2019-03-07T05:49:05.3868647Z"
                },
                "type": "Microsoft.Resources/deployments"
            }
        ],
        "url": "/subscriptions/1c5b82ee-9294-4568-b0c0-b9c523bc0d86/resourceGroups/zimsanotherdeployment/providers/Microsoft.resources/deployments/zimsdplxxxx",
        "warnings": [
            "Azure API profile latest does not define an entry for GenericRestClient"
        ]
    }
}