NTNX>日記

個人的な趣味による Nutanix Community Edition 日記。Japanese のみですみません。

acli で VM 削除自動化くん。

この投稿は、[4枚目]Nutanix Advent Calendar 2019 の 8日目です。

Nutanix CE に VM をたくさん作成したので、まとめて削除してみます。

まず、現状の VM の様子です。gowatana-CitrixPVS~という名前の(実際には Citrix ととは関係ない)テスト VM がたくさん存在するので、これから削除します。

CVM$ acli vm.list
VM name               VM UUID
demo-vm-01            4e7e32d0-7464-4820-8109-c64d81865564
gowatana-CitrixPVS01  e985ae18-f19e-41ef-a75d-d5da15b8d955
gowatana-CitrixPVS02  b10f434d-38ae-4bc2-8990-c57e98459bec
gowatana-CitrixPVS03  b943ec8c-6c3b-451e-8224-b84344e87010
gowatana-CitrixPVS04  8f13754c-4bf5-4132-887d-c37df4007b30
gowatana-CitrixPVS05  2d85191d-5e22-4d78-8e01-ac4f7f739c02
gowatana-CitrixPVS06  65309f28-1cdb-4e0a-8a41-85dad7b88f4b
gowatana-CitrixPVS07  0c5beaf6-7171-4147-b92c-7543832f0d79
gowatana-CitrixPVS08  56003ec2-d3c6-44d3-a9fa-5f2e93f442ed
gowatana-CitrixPVS09  b17398cd-cb19-4cc1-b930-0947e577975b
gowatana-CitrixPVS10  d5fc8447-c5f7-4c85-9e71-3e5050d8f259
gowatana-CitrixPVS11  39a425b1-71de-4a12-8fb7-cc69f33b4652
gowatana-CitrixPVS12  eba2282d-bc87-4c2a-b306-509121729cb8
gowatana-CitrixPVS13  c242814d-c693-4e29-9c48-72340f5999bb
gowatana-CitrixPVS14  e950fe72-ee44-43a9-a003-12adcc80c227
gowatana-CitrixPVS15  90deca1d-cee7-4cec-9cbb-dbff91253020
gowatana-CitrixPVS16  6c9e606a-137f-4989-a7ed-f56e74012b43
gowatana-CitrixPVS17  c1f1af17-624d-4cc7-8074-b808e7f824a3
gowatana-CitrixPVS18  dfeb0235-306b-410b-9f73-997101f18299
gowatana-CitrixPVS19  dfe61da0-ff29-47d2-8e04-b7f9ccbfbaee
gowatana-CitrixPVS20  da78336f-5be0-4638-a9aa-daa004fb7b12
gowatana-CitrixPVS21  515c060b-c215-49f0-b8d5-5fe6542564f3
gowatana-CitrixPVS22  70817549-6ff9-4698-ba83-249acb990f2b
gowatana-CitrixPVS23  2f3e8ad0-cd51-4420-83ab-6a2a18f1b474
gowatana-CitrixPVS24  6f572876-5eb1-4933-8d39-d31a21b3166f
gowatana-CitrixPVS25  d3114bf5-d475-422e-999f-b624b76ef606
gowatana-CitrixPVS26  4d023124-4de8-4197-9359-9a28d80ecb7b
gowatana-CitrixPVS27  9dc553e0-514d-4579-9863-2585c6500229
gowatana-CitrixPVS28  923859dc-2d36-4466-a226-e47bdb804312
gowatana-CitrixPVS29  519361de-e4e9-4bfb-99f7-2a7aee5e2716
gowatana-CitrixPVS30  dd7bfcc9-bb83-4c01-9f36-677a078c2f0e
gowatana-CitrixPVS31  8462cf39-4bfe-4041-9712-4ac8659b8445
gowatana-CitrixPVS32  15f2397b-02c5-4c2d-a7c5-821b5ee89fe9
gowatana-CitrixPVS33  0fcb24d0-14db-4906-8cc4-14095fa6d554
gowatana-CitrixPVS34  ed41b01c-5c18-433a-8747-45f9607685bc
gowatana-CitrixPVS35  97c7f08c-e1e5-45bf-b6e9-23ac96666cf4
gowatana-CitrixPVS36  15d1724d-fa1f-41e8-a820-f3bedcd1b1b3
gowatana-CitrixPVS37  0467a69c-e589-4c1f-822c-3b5241fb2913
gowatana-CitrixPVS38  4429f648-88b8-44a7-a84d-b7eb5e7aaba6
gowatana-CitrixPVS39  d6a16c32-1572-40f0-9c95-c146e7d5471f
gowatana-CitrixPVS40  f7e08a47-0069-4ff0-b986-1123065ee796
gowatana-CitrixPVS41  b1ccd88f-cc2e-49bf-8c50-de83f0166097
gowatana-CitrixPVS42  3b919066-58e3-41da-9e3c-4287312b0d3e
gowatana-CitrixPVS43  6316c13a-901e-4c57-a903-275ff89da551
gowatana-CitrixPVS44  050fbffd-3da5-4703-9476-2407dfce6c91
gowatana-CitrixPVS45  1d2f9bbe-d00e-484d-9295-adc233e637e9
gowatana-CitrixPVS46  51543a54-3a3a-4c1c-a007-df41260815f9
gowatana-CitrixPVS47  82fd446d-2365-4f94-8e46-36ca71c14004
gowatana-CitrixPVS48  d98f3f11-e70e-4822-8656-539ea1aef07e
gowatana-CitrixPVS49  f366c9c1-9169-4936-822f-665d7a9cd84d
gowatana-CitrixPVS50  bb724f9b-70ef-48b5-bf50-8162a83cad93
lab-ntnx-pc-01        19192d75-ae55-4908-aae6-a9fd3a300d3c

まず、削除したい VM 名のリストを簡単に指定できる場合(VM の命名規則がそろっている場合など)です。

CVM$ for VM in {01..04}; do echo gowatana-CitrixPVS$VM; done
gowatana-CitrixPVS01
gowatana-CitrixPVS02
gowatana-CitrixPVS03
gowatana-CitrixPVS04

下記のように削除することができます。

CVM$ for VM in {01..04}; do acli -y vm.delete gowatana-CitrixPVS$VM; done
gowatana-CitrixPVS01: pending
gowatana-CitrixPVS01: complete
gowatana-CitrixPVS02: pending
gowatana-CitrixPVS02: complete
gowatana-CitrixPVS03: pending
gowatana-CitrixPVS03: complete
gowatana-CitrixPVS04: pending
gowatana-CitrixPVS04: complete

ちなみに、acli の「-y」は、下記のような確認メッセージに自動応答するために付与しています。

CVM$ acli vm.delete gowatana-CitrixPVS01
Delete 1 VMs? (yes/no)

VM 名に規則がなかったとしても、VM 名のリストを指定してまとめて削除できます。

CVM$ for VM in gowatana-CitrixPVS05 gowatana-CitrixPVS06; do acli -y vm.delete $VM; done
gowatana-CitrixPVS05: pending
gowatana-CitrixPVS05: complete
gowatana-CitrixPVS06: pending
gowatana-CitrixPVS06: complete

逆に、特定の VM 以外を削除する場合について考えてみます。まず、下記のような、残したい VM のリストファイルを用意しておきます。

CVM$ cat /tmp/keep-vm-list.txt
demo-vm-01
lab-ntnx-pc-01

そして、リストにない VM のみ削除するつもりです。

CVM$ acli vm.list | grep -E -v "^VM name|^$" | awk '{print $1}' | while read VM; do grep $VM /tmp/keep-vm-list.txt || echo $VM '=>' delete; done
demo-vm-01
gowatana-CitrixPVS07 => delete
gowatana-CitrixPVS08 => delete
gowatana-CitrixPVS09 => delete
gowatana-CitrixPVS10 => delete
gowatana-CitrixPVS11 => delete
gowatana-CitrixPVS12 => delete
gowatana-CitrixPVS13 => delete
gowatana-CitrixPVS14 => delete
gowatana-CitrixPVS15 => delete
gowatana-CitrixPVS16 => delete
gowatana-CitrixPVS17 => delete
gowatana-CitrixPVS18 => delete
gowatana-CitrixPVS19 => delete
gowatana-CitrixPVS20 => delete
gowatana-CitrixPVS21 => delete
gowatana-CitrixPVS22 => delete
gowatana-CitrixPVS23 => delete
gowatana-CitrixPVS24 => delete
gowatana-CitrixPVS25 => delete
gowatana-CitrixPVS26 => delete
gowatana-CitrixPVS27 => delete
gowatana-CitrixPVS28 => delete
gowatana-CitrixPVS29 => delete
gowatana-CitrixPVS30 => delete
gowatana-CitrixPVS31 => delete
gowatana-CitrixPVS32 => delete
gowatana-CitrixPVS33 => delete
gowatana-CitrixPVS34 => delete
gowatana-CitrixPVS35 => delete
gowatana-CitrixPVS36 => delete
gowatana-CitrixPVS37 => delete
gowatana-CitrixPVS38 => delete
gowatana-CitrixPVS39 => delete
gowatana-CitrixPVS40 => delete
gowatana-CitrixPVS41 => delete
gowatana-CitrixPVS42 => delete
gowatana-CitrixPVS43 => delete
gowatana-CitrixPVS44 => delete
gowatana-CitrixPVS45 => delete
gowatana-CitrixPVS46 => delete
gowatana-CitrixPVS47 => delete
gowatana-CitrixPVS48 => delete
gowatana-CitrixPVS49 => delete
gowatana-CitrixPVS50 => delete
lab-ntnx-pc-01

実際の削除には、下記のようなコマンドラインを実行します。

CVM$ acli vm.list | grep -E -v "^VM name|^$" | awk '{print $1}' | while read VM; do grep $VM /tmp/keep-vm-list.txt || acli -y vm.delete $VM; done
demo-vm-01
gowatana-CitrixPVS07: pending
gowatana-CitrixPVS07: complete
gowatana-CitrixPVS08: pending
gowatana-CitrixPVS08: complete
gowatana-CitrixPVS09: pending
gowatana-CitrixPVS09: complete
gowatana-CitrixPVS10: pending
gowatana-CitrixPVS10: complete
gowatana-CitrixPVS11: pending
gowatana-CitrixPVS11: complete
gowatana-CitrixPVS12: pending
gowatana-CitrixPVS12: complete
gowatana-CitrixPVS13: pending
gowatana-CitrixPVS13: complete
gowatana-CitrixPVS14: pending
gowatana-CitrixPVS14: complete
gowatana-CitrixPVS15: pending
gowatana-CitrixPVS15: complete
gowatana-CitrixPVS16: pending
gowatana-CitrixPVS16: complete
gowatana-CitrixPVS17: pending
gowatana-CitrixPVS17: complete
gowatana-CitrixPVS18: pending
gowatana-CitrixPVS18: complete
gowatana-CitrixPVS19: pending
gowatana-CitrixPVS19: complete
gowatana-CitrixPVS20: pending
gowatana-CitrixPVS20: complete
gowatana-CitrixPVS21: pending
gowatana-CitrixPVS21: complete
gowatana-CitrixPVS22: pending
gowatana-CitrixPVS22: complete
gowatana-CitrixPVS23: pending
gowatana-CitrixPVS23: complete
gowatana-CitrixPVS24: pending
gowatana-CitrixPVS24: complete
gowatana-CitrixPVS25: pending
gowatana-CitrixPVS25: complete
gowatana-CitrixPVS26: pending
gowatana-CitrixPVS26: complete
gowatana-CitrixPVS27: pending
gowatana-CitrixPVS27: complete
gowatana-CitrixPVS28: pending
gowatana-CitrixPVS28: complete
gowatana-CitrixPVS29: pending
gowatana-CitrixPVS29: complete
gowatana-CitrixPVS30: pending
gowatana-CitrixPVS30: complete
gowatana-CitrixPVS31: pending
gowatana-CitrixPVS31: complete
gowatana-CitrixPVS32: pending
gowatana-CitrixPVS32: complete
gowatana-CitrixPVS33: pending
gowatana-CitrixPVS33: complete
gowatana-CitrixPVS34: pending
gowatana-CitrixPVS34: complete
gowatana-CitrixPVS35: pending
gowatana-CitrixPVS35: complete
gowatana-CitrixPVS36: pending
gowatana-CitrixPVS36: complete
gowatana-CitrixPVS37: pending
gowatana-CitrixPVS37: complete
gowatana-CitrixPVS38: pending
gowatana-CitrixPVS38: complete
gowatana-CitrixPVS39: pending
gowatana-CitrixPVS39: complete
gowatana-CitrixPVS40: pending
gowatana-CitrixPVS40: complete
gowatana-CitrixPVS41: pending
gowatana-CitrixPVS41: complete
gowatana-CitrixPVS42: pending
gowatana-CitrixPVS42: complete
gowatana-CitrixPVS43: pending
gowatana-CitrixPVS43: complete
gowatana-CitrixPVS44: pending
gowatana-CitrixPVS44: complete
gowatana-CitrixPVS45: pending
gowatana-CitrixPVS45: complete
gowatana-CitrixPVS46: pending
gowatana-CitrixPVS46: complete
gowatana-CitrixPVS47: pending
gowatana-CitrixPVS47: complete
gowatana-CitrixPVS48: pending
gowatana-CitrixPVS48: complete
gowatana-CitrixPVS49: pending
gowatana-CitrixPVS49: complete
gowatana-CitrixPVS50: pending
gowatana-CitrixPVS50: complete
lab-ntnx-pc-01

VM が削除されました。

CVM$ acli vm.list
VM name         VM UUID
demo-vm-01      4e7e32d0-7464-4820-8109-c64d81865564
lab-ntnx-pc-01  19192d75-ae55-4908-aae6-a9fd3a300d3c

定期的に環境を初期化するデモ環境など、決められた VM 以外をまとめて削除する場合などに、自動化の一部として利用できるかなと思います。

ちなみに、 VM スナップショットもまとめて削除するには「acli vm.delete ~ delete_snapshots=true」といったコマンドラインにします。

以上。

 

©2023 gowatana
クリエイティブ・コモンズ・ライセンスこの 作品 は クリエイティブ・コモンズ 表示 4.0 国際 ライセンスの下に提供されています。