はじめに
Ansible から Azure の VM の情報を取得して動的にインベントリを生成できる azure.azcollection.azure_rm
インベントリプラグインという便利なものがあります。
こちらの issueを見てなるほどと思ったのですが、デフォルトだと実行中(running
)でない VM や、provisioning_state
が succeeded
でない VM は除外されます。何らかの事情で、状態に関わらずインベントリとして取得したい場合は、default_host_filters
オプションで空リストを指定すればいいということなので、試してみました。
- 環境
- ansible-core 2.12.1
- azure.azcollection 1.10.0
(比較用)デフォルトの default_host_filters
の場合
比較のためまず、default_host_filters
の指定なしで試します。サービスプリンシパルは ~/.azure/credentials
に定義済みの状態です。
--- plugin: azure.azcollection.azure_rm include_vm_resource_groups: - "*" plain_host_names: true # インベントリ名を仮想マシン名にする(本件とは直接関係ない)
確認には、単純にインベントリの情報だけ確認したいので、ansible-inventory
コマンドを利用します。
$ ansible-inventory -i inventory_azure_rm.yml --graph @all: |--@ungrouped: | |--ansible01
ポータルと見比べると起動中の VM だけが対象になりました。
停止中含め 全 VM を対象にする
次は、状態に関わらず全 VM を対象にする場合です。
--- plugin: azure.azcollection.azure_rm include_vm_resource_groups: - "*" plain_host_names: true # インベントリ名を仮想マシン名にする(本件とは直接関係ない) default_host_filters: [] # ポイント
また ansible-inventory
を実行して確認します。
$ ansible-inventory -i inventory_azure_rm.yml --graph @all: |--@ungrouped: | |--ansible01 | |--iwana01 | |--yamame01 | |--yokoyoko
無事実行中以外の VM も表示されました。
参考
default_host_filters
オプションSet this to a different value or empty list if you need to include hosts in these states.