-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
azure_vars_export
37 lines (30 loc) · 1.53 KB
/
azure_vars_export
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
# This file must be used with "source azure_vars_export" from bash,
# you should not run it directly
# You can login with the following command:
# az login --use-device-code --tenant <tenant_id>
ARM_SUBSCRIPTION_ID="${ARM_SUBSCRIPTION_ID:-$(az account show --query id -o tsv)}"
LOCATION="${ARM_LOCATION:-northeurope}"
RESOURCE_GROUP_NAME="${ARM_RESOURCE_GROUP_NAME:-$(jq -r '."resource_group"' ubuntu-azure-vars.json)}"
PRINCIPAL_NAME="${ARM_PRINCIPAL_NAME:-$(jq -r '."principal_name"' ubuntu-azure-vars.json)}"
CHECK_PRINCIPAL_NAME=$(az ad sp list --display-name "${PRINCIPAL_NAME}" | jq '.[]."appDisplayName"')
CHECK_RESOURCE_GROUP=$(az group list --query "[?name=='${RESOURCE_GROUP_NAME}']" | jq '.[0].name')
if [ "${CHECK_RESOURCE_GROUP}" == "null" ]; then
az group create --location "${LOCATION}" --name "${RESOURCE_GROUP_NAME}"
fi
if [ "${CHECK_PRINCIPAL_NAME}" != "\"${PRINCIPAL_NAME}\"" ]; then
az_array=$(az ad sp create-for-rbac \
--role contributor \
--scopes "/subscriptions/${ARM_SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP_NAME}" \
--name "${PRINCIPAL_NAME}" 2>/dev/null)
else
PRINCIPAL_ID=$(az ad sp list --display-name "${PRINCIPAL_NAME}" |\
jq -r '.[]."appId"' 2>/dev/null)
az_array=$(az ad sp credential reset --id "${PRINCIPAL_ID}" 2>/dev/null)
fi
ARM_CLIENT_ID="$(echo "${az_array}" | jq -r '.appId')"
ARM_CLIENT_SECRET="$(echo "${az_array}" | jq -r '.password')"
ARM_TENANT_ID="$(echo "${az_array}" | jq -r '.tenant')"
export ARM_CLIENT_ID
export ARM_CLIENT_SECRET
export ARM_SUBSCRIPTION_ID
export ARM_TENANT_ID