LoginSignup
13
16

More than 5 years have passed since last update.

Ansibleのインベントリファイル

Last updated at Posted at 2016-07-03

Ansibleでホスト(ホストグループ)と、各種変数を設定する。

インベントリファイルを指定する方法

  1. ansible-playbookの-iオプションで指定する
  2. ansible.cfgで指定する

ansible-playbookの-iオプションで指定する

shell-session: -iオプションによるインベントリファイルを指定
ansible-playbook -i インベントリファイルパス プレイブックファイル名

ansible.cfgで指定する

ansible.cfgによるインベントリファイルの指定
hostfile=ホストファイルパス

インベントリファイルの記法

基本

ホストグループ(webservers)とそれに紐づく変数(webservers:vars)を記述した場合

インベントリファイルの記法サンプル
[webservers]
web1.example.com
web2.example.com
web3.example.com

[webservers:vars]
key1=value1
key2=value2

[]でホストグループを指定する。
個別のホストは[]の配下に記述。

特定のホストグループに対して適用したい変数は
[ホストグループ名:vars]の形で特定のホストグループに
所属する変数群が存在することを記述した後に
"キー=値"の形で変数とその値を指定する。

連番の取扱い

インベントリファイル内では連番やアルファベット順の場合は
正規表現的な書き方で短縮することができる

数字の場合

改修前(数字)
[webservers]
web1.example.com
web2.example.com
web3.example.com
改修後(数字)
[webservers]
web[1:3].example.com

アルファベットの場合

改修前(アルファベット)
[webservers]
webA.example.com
webB.example.com
webC.example.com
改修後(アルファベット)
[webservers]
web[A:C].example.com

ホスト変数を指定

特定のホストに対して変数を指定した場合

インベントリファイルの記法サンプル
[webservers]
web1.example.com key11=value11 key12=value12
web2.example.com key21=value21 key22=value22
web3.example.com key31=value31 key32=value32

ホストに対して変数を指定する場合は
スペース区切りで"キー=値"形式で記述する。

ホストグループの親子関係を記述した場合

下図のようなホストグループの親子関係が存在する場合

image

ホストグループの親子関係
[parent]
parent1.example.com

[childA]
childA1.example.com

[childB]
childB1.example.com

[childC]
childC1.example.com

[parent:children]
childA
childB
childC

13
16
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
13
16