🍣

Ansibleのインベントリ(hosts)を作る

2022/01/31に公開

いつもなんとなく作ってきてたけど、これはいいなというのが見えたのでメモ。

Ansibleの公式にはこんな感じで書かれている。

mail.example.com

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

https://docs.ansible.com/ansible/2.9_ja/user_guide/intro_inventory.html#inventoryformat

実はこれあんまり使い勝手よくないので、下記のようにするとよい。

[app01:children]
webservers
dbservers

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

[hosts]
one.example.com ansible_host=10.10.53.*** # コメント
two.example.com ansible_host=10.10.53.*** # コメント
three.example.com ansible_host=10.10.53.*** # コメント
mail.example.com
  1. ホスト単体の定義は末尾でまとめてやったほうが視認性がいい
  2. :children 無しでホストをグループ化できる
  3. グループ化したホストを:childrenでさらにグループ化する

インベントリエイリアスを便利に使った方法かもしれない。
https://docs.ansible.com/ansible/2.9_ja/user_guide/intro_inventory.html#inventory-aliases

Discussion