LoginSignup
4
5

More than 1 year has passed since last update.

Ansible | インベントリファイルの基本

Last updated at Posted at 2017-09-14

ホストの指定

インベントリファイルの作成

適当な名前でインベントリファイルを作る。

inventory.txt
example.com

実行

「インベントリファイル」と「実行対象」を指定して、コマンドを実行する。
( --list-hosts はホストの一覧を表示する )

ansible -i [インベントリファイル] [実行対象] --list-hosts

ここではインベントリファイルに書いたホスト ( example.com ) を直接、実行対象として指定する。

$ ansible -i inventory.txt example.com --list-hosts

ファイルにもホストを書いて、コマンドでもホストを指定する。
なんだか二度手間なような気もするが、とりあえず気にしないでおく。

結果

$ ansible -i inventory.txt example.com --list-hosts
    example.com

ちなみに

インベントリに書かれていない対象(ホスト)に対しては、コマンドを実行できない。

$ ansible -i inventory.txt none.com --list-hosts
No hosts matched

グループの指定

最初のケースだとほとんど意味がないが、グループを指定することでインベントリファイルが重宝する。
グループAとグループBにホストをまとめてみる。

inventory.txt
[groupA]
first.example.com
second.example.com
third.example.com

[groupB]
forth.example.com
fifth.example.com
sixth.example.com

実行

それぞれグルーピングされているのが分かる。

$ ansible -i inventory.txt groupA --list-hosts
    first.example.com
    second.example.com
    third.example.com
$ ansible -i inventory.txt groupB --list-hosts
    forth.example.com
    fifth.example.com
    sixth.example.com

グループのグループを作る

グループをグループ化することが出来る。
ここでは「グループA」と「グループB」をまとめた「グループC」を作ってみる。

inventory.txt
[groupA]
first.example.com
second.example.com
third.example.com

[groupB]
forth.example.com
fifth.example.com
sixth.example.com

[groupC:children]
groupA
groupB

実行

$ ansible -i inventory.txt groupC --list-hosts
    first.example.com
    second.example.com
    third.example.com
    forth.example.com
    fifth.example.com
    sixth.example.com

環境

  • ansible 1.9.6

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

4
5
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
4
5