0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

サービスプリンシパルが不要な Azure Automation マネージド ID を Azure CLI で作って実行してみた

Posted at

背景と目的

以前は Azure Automation を使って Azure リソースの自動化を実現するにはサービスプリンシパルが必須でした。2021 年 11 月に Azure Automation のマネージド ID がサポートされるようになり、サービスプリンシパルもしくはマネージド ID をそれぞれの適用範囲やユースケースに応じて使い分けられるようになりました。

そこで今回は、システム割り当てマネージド ID を使用した Azure Automation を Azure CLI で作成してみます。

前提条件

コマンドの実施環境は、Mac + Azure CLI です。

bash
$ sw_vers
ProductName:    macOS
ProductVersion: 12.3.1
BuildVersion:   21E258

$ az version
{
  "azure-cli": "2.36.0",
  "azure-cli-core": "2.36.0",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}

Azure Automation を作成

bash
# 環境変数をセットします
region=japaneast
prefix=mnrdevauto
sid=$(az account show --query id --output tsv)

# リソースグループを作成します
az group create \
  --name ${prefix}-rg \
  --location $region

# Automation アカウントを作成します
az automation account create \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --sku Free

# マネージド ID を有効にします
az resource update \
  --set identity.type=systemassigned \
  --ids $(az automation account show \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --query id \
  --output tsv)

# マネージド ID を共同作成者ロールでリソースグループに割り当てます
az role assignment create \
  --assignee $(az automation account show \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --query identity.principalId \
  --output tsv) \
  --scope $(az group show \
  --name ${prefix}-rg \
  --query id \
  --output tsv) \
  --role "Contributor"

Runbook を作成して実行

bash
# Runbook を作成します
az automation runbook create \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --name ${prefix} \
  --type PowerShell

# Runbook のコンテンツを作成します
cat <<"EOF" > ${prefix}.ps1
Connect-AzAccount -Identity
Get-AzSubscription
EOF

# Runbook のコンテンツを置き換えます
az automation runbook replace-content \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --name ${prefix} \
  --content @${prefix}.ps1

# Runbook の下書きを公開します
az automation runbook publish \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --name ${prefix}

# Runbook を開始します
jobid=$(az automation runbook start \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --name ${prefix} \
  --query jobId \
  --output tsv)

# ジョブの情報を取得します
az automation job show \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --name $jobid \
  --output table

# ジョブの出力を取得します
az rest \
  --method GET \
  --url "https://management.azure.com/subscriptions/$sid/resourceGroups/${prefix}-rg/providers/Microsoft.Automation/automationAccounts/${prefix}/jobs/$jobid/output?api-version=2019-06-01"

参考

# リソースグループを削除します
az group delete \
  --name ${prefix}-rg \
  --yes

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?