iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 5
2
Microsoft Azure

深入 Azure 雲端服務系列 第 5

Azure CLI + Bash 批量操作

Azure CLI + Bash 批量操作

演示影片 :

Yes


近年來隨著微軟擁抱開源社群,大喊 Microsoft ❤ Linux ,不管在雲端的 Azure Cloud Shell 跟地端的 Mac、Linux 我們都可以使用 Bash Shell 來幫助我們完成維運腳本的編寫。

舉例,在 Azuer Cloud Shell 簡單使用 bash 的 forloop 指令測試

#!/bin/bash
for (( i=1; i<=10; i++ ))
do  
   echo "Hello Azure IT Help $i times"
done

image-20200921221835818

舉例,Bash 搭配 Azure CLI 做批量建立、刪除的動作,一次性建立、刪除10個群組

# 創建批量建立群組shell
weihan@Azure:~$ vim demo.sh
#!/bin/bash
for (( i=1; i<=10; i++ ))
do  
   az group create --name "demo$i" --location eastasia
   echo "Create $i Azure Group"
done

# 執行創建
weihan@Azure:~$ bash demo.sh
Create 1 Azure Group
..
Create 10 Azure Group

# 查詢結果
weihan@Azure:~$ az group list --query "[]" -o table
Name    Location
------  ----------
demo1   eastasia
demo2   eastasia
...
demo10  eastasia

# 批量刪除
weihan@Azure:~$ touch demo1.sh
weihan@Azure:~$ code demo1.sh

# 創建批量刪除群組shell
#!/bin/bash
for (( i=1; i<=10; i++ ))
do  
   az group delete --name "demo$i" --no-wait -y 
   echo "Delete demo$i Azure Group"
done

weihan@Azure:~$ ls
demo1.sh  demo.json  demo.sh

weihan@Azure:~$ bash demo1.sh
Delete demo1 Azure Group
..
Delete demo10 Azure Group

小知識 :

  • Azure Cloud Shell 支援 Ubuntu Python,方便維運人員延續使用之前的python腳本工具 :
    image-20200921224618683

上一篇
Azure CLI 快速導覽
下一篇
Azure CLI + PowerShell
系列文
深入 Azure 雲端服務15
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言