LoginSignup
3
5

More than 5 years have passed since last update.

AnsibleのInventoryファイル形式をiniからymlに変更した話

Last updated at Posted at 2017-09-22

追記(2017/09/27)

  • ansible 2.3.xでは問題がないが、2.4.0では次のようなエラーが発生する。
ERROR! Unexpected Exception, this is probably a bug: string indices must be integers
  • ini形式で戻したら、エラーが無くなった。2.4.0のbugだと思うが、原因が分かり次第、追記する。

背景

  • 久々にAnsible Inventoryのドキュメントを見たらymlでも作成できるようになっていた。
  • まだ慣れてないini形式よりplaybookと同様のymlで直した方がいいと判断した。

ターゲットノードの一覧

vagrantでlocalで運用中のサーバーの一覧

Current machine states:

jumpbox                   running (virtualbox)
dev                       running (virtualbox)
test                      running (virtualbox)
live                      running (virtualbox)
gembox                    running (virtualbox)
proxy                     running (virtualbox)
db1                       running (virtualbox)
db2                       running (virtualbox)
redmine                   running (virtualbox)
chat                      running (virtualbox)
gitlab                    running (virtualbox)
runner                    running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

/etc/hostsファイルに一覧に表示されているサーバーの名前でマッピングされている。

Inventoryファイル

local(macOS Sierra)とターゲットノード(vagrant上のサーバー)をansibleで管理している。
そのときのファイルをini形式からyml形式で直して見た。

local:
  hosts: localhost
  vars:
    ansible_connection: local

all:
  hosts:
    jumpbox: jumpbox
    dev: dev
    test: test
    live: live
    gembox: gembox
    proxy: proxy
    redmine: redmine
    chat: chat
    gitlab: gitlab
    runner: runner
  children:
    db:
      hosts: db[1:2]
  vars:
    ansible_connection: ssh
    ansible_user: vagrant
    ansible_password: vagrant

ansible.cfg

Inventoryファイルはリポジトリ内で管理したほうがいろいろ便利である。
僕はansible.cfgファイルを作成してInventoryファイルなどのデフォルトの設定を書いて使っている。

% cat ansible.cfg
[defaults]
inventory = inventories/hosts.yml
forks = 5
log_path = ~/Library/Logs/ansible.log
host_key_checking = false
gathering = smart
executable = /bin/bash -l
retry_files_save_path = ~/.ansible/retry-files

検証

  • AnsibleでInventoryファイルをちゃんと解析しているのかを確認する。
% ansible all --list-hosts
  hosts (13):
    jumpbox
    runner
    gitlab
    gembox
    live
    redmine
    dev
    chat
    test
    proxy
    db1
    db2
    localhost
  • ansibleコマンドで各サーバーについてpingテストを行う。
% ansible all -m ping
jumpbox | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
gitlab | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
runner | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
live | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
gembox | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
redmine | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
dev | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
test | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
chat | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
proxy | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
db1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
db2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

これで問題なし!

参考

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