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 1 year has passed since last update.

Azure CLIでVMを作成する

Last updated at Posted at 2022-09-11

はじめに

わたしがAzure上でVMをつくっては壊してを繰り返しているときによく使用する手順です。

踏み台サーバ兼Ansible-Playbook実行環境を作成する想定で記載しています。
より良い方法があると思いますのでご存知の方がみてくださったらご教示いただけるとありがたいです。

前提

Azure Cloud shell上で実行します
変数の値は適宜読み替えてください。
VM作成時に指定するイメージやサイズについては、最後に調べる方法を記載しています。

VM作成手順

リソースグループの作成(初回のみ)

  • リソースグループの作成
RG="managementResourceGroup"
REGION="eastus2"
az group create --name $RG --location $REGION
  • 実行結果
{
  "id": "/subscriptions/c74afe34-c421-4540-bf9c-fad145c5204e/resourceGroups/managementResourceGroup",
  "location": "eastus2",
  "managedBy": null,
  "name": "managementResourceGroup",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}

VMの新規作成(都度実施)

  • スクリプトを実行して、VMを作成する
    ※IPアドレスのセグメントは、10.0.0.0/24のなかで指定する
. ./create_vm.sh 10.0.0.10 ansible-server
  • スクリプトの内容(create_vm.sh)
#!/bin/bash
IMAGE="Canonical:0001-com-ubuntu-confidential-vm-experimental:20_04:20.04.20210309"
SIZE="Standard_B2s"
RG="managementResourceGroup"
USERNAME="aki-nasu"
PRIVATEIP=$1
VMNAME=$2

az vm create \
    --resource-group $RG \
    --name "$VMNAME" \
    --image "$IMAGE" \
    --size $SIZE \
    --admin-username $USERNAME \
    --private-ip-address "$PRIVATEIP" \
    --public-ip-address "" \
    --public-ip-sku Standard \
    --generate-ssh-keys

PublicIPとの関連付け

  • PublicIPを関連付けるときは、VMNicのIP構成を選択する(上記スクリプトではPublicIPを作成しないようにしている)
    • 関連付けするIPアドレスを選択すると、関連付けの設定画面がでるので設定する

おまけ

参考

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?