ゆるい感じのプログラムを書きたい。

プログラムの敷居を下げて、多くの人が開発出来るように色々書いていきます!

【インベントリー概要】構築方法 1章:基本

インベントリーの構築方法

  • Ansibleは、インベントリーと呼ばれるリスト、またはリストのグループを使用して インフラストラクチャーにある複数管理ノードまたは「ホスト」に対して同時に機能します。
  • インベントリーのデフォルトの場所は以下のファイルです。
/etc/ansible/hosts
-i <path>
  • 動的イベントリーでの使用で説明されているように、複数のイベントリーファイルを同時に使用したり動的またはクラウドのソースまたは異なる形式(YAML,iniなど)からづくすうのイベントリーを取得(プル)することも出来る。

  • Ansibleには、inventoryプラグインがあり、柔軟にカスタマイズ可能 バージョン2.4で導入


1.イベントリーの基本:形式、ホスト、およびグループ

  • イベントリーファイルは、所有するイベントリープラグインに応じて、以下の多くの形式のいずれかになります。 最も一般的な形式はINIおよびYAML形式です

基本的なINIである/etc/ansible/hostsは以下のようになります。

mail.example.com

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

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

カッコ内の見出しはグループ内で、ホストを分類し、どの時点で、どのような目的で、どのホストを制御するのかを決定するのに使用されます。


基本的なYAMLである/etc/ansible/hostsは以下のようになります。

all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:


1−1.デフォルトのグループ

デフォルトのグループはallおよび、ungroupedです。

グループ名 説明
allグループ 全てのホストが含まれます
ungroupedグループ ユーザーによって明示的にグループへの紐付けがされていない全てのホストが所属するグループ
allグループの例
all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:

この設定の場合 以下のホスト名が含まれる

mail.example.com
foo.example.com
bar.example.com
one.example.com
two.example.com
three.example.com        


ungroupedグループの例
all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:

この設定の場合 以下の children配下ではない、ホスト名が含まれる

mail.example.com


補足:childern

  • 子グループを定義する時の宣言

管理対象のノードを共通な内容でグルーピングします。 上記の設定の場合以下のような子グループに分かれます。

# ホスト名 用途
1 mail.example.com -
2 foo.example.com webservers
3 bar.example.com webservers
4 one.example.com dbservers
5 two.example.com dbservers
6 three.example.com dbservers


1−2.複数グループ内のホスト

  • 各ホストを複数のグループに配置

例:Atlantaのデータセンター内の実稼働Webサーバは [prod],[atlanta]および[webservers]と呼ばれるグループに含まれる場合があります。 以下を追跡するグループ作成できます。

項目 説明
なにを アプリケーション、スタック、またはマイクロサービス(データベースサーバ,Webサーバなど)
どこで (ローカルDNS,ストレージなどと通信する)データセンターまたはリージョン(例:東、西)
いつ 実稼働リソースでのテストを回避する開発ステージ(例:実稼働、テスト)

「いつ、どこで、なにを」が含まれるようにします。

all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:
    east:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    west:
      hosts:
        bar.example.com:
        three.example.com:
    prod:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    test:
      hosts:
        bar.example.com:
        three.example.com:

one.exampple.comsbservers,east,prodグループに存在することを確認出来る。

このイベントりーのprod,testを簡略化するために、ネストされたグループを使用することも出来ます。

all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:
    east:
      hosts:
        foo.example.com:
        one.example.com:
        two.example.com:
    west:
      hosts:
        bar.example.com:
        three.example.com:
    prod:
      children:
        east:
    test:
      children:
        west:

イベントリーを整理し、ホストをグループ化する方法は、イベントリーの設定例を参照してください。


1−3.ホスト範囲の追加

同様のパターンを持つホストが複数ある場合は、各ホスト名を個別に表示するのではなく ホスト範囲として追加できます。

  • INIの場合は以下のようになります。
[webservers]
www[01:50].example.com
  • YAMLの場合は以下のようになります。
...
  webservers:
    hosts:
      www[01:50].example.com:

数値のパターンでは、必要に応じて、先頭にゼロを含めるか、または削除出来ます。

また、アルファベットの範囲を定義することも出来ます。

[databases]
db-[a:f].example.com