5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansible の inventory 指定はディレクトリが良い

Last updated at Posted at 2017-01-24

Ansible の inventory をディレクトリにすると

そのディレクトリ以下のファイルをすべて inventory として読み込んでくれる。
※ -i オプションは1回しか指定できない

$ ansible-playbook -i inventories/vagrant/ playbooks/playbook.yml

確認のための準備

たとえば、以下の構成があったとする。

├── inventories
│   ├── IaaS
│   │   ├── all_vars
│   │   ├── consul
│   │   └── jenkins
│   └── vagrant
│       ├── all_vars
│       ├── consul
│       └── jenkins
└── playbooks
    └── playbook.yml
inventories/IaaS/all_vars.
[all:vars]
datacenter='IaaS'
inventories/IaaS/consul.
[consul_server]
2.2.2.2
inventories/IaaS/jenkins.
[jenkins]
1.1.1.1
inventories/vagrant/all_vars.
[all:vars]
datacenter = vagrant
inventories/vagrant/consul.
[consul_server]
10.10.10.12
inventories/vagrant/jenkins.
[jenkins]
10.10.10.11
playbooks/playbook.yml
- hosts: localhost
  tasks:
    - debug:
        var: datacenter
    - debug:
        var: groups

確認してみる

インベントリディレクトリに inventories/vagrant/ を指定してみる

$ ansible-playbook -i inventories/vagrant/ playbooks/playbook.yml 

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "datacenter": "vagrant"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "groups": {
        "all": [
            "10.10.10.12", 
            "10.10.10.11"
        ], 
        "consul_server": [
            "10.10.10.12"
        ], 
        "jenkins": [
            "10.10.10.11"
        ], 
        "ungrouped": [
            "localhost"
        ]
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

インベントリディレクトリに inventories/IaaS/ を指定してみる

$ ansible-playbook -i inventories/IaaS/ playbooks/playbook.yml 

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "datacenter": "IaaS"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "groups": {
        "all": [
            "2.2.2.2", 
            "1.1.1.1"
        ], 
        "consul_server": [
            "2.2.2.2"
        ], 
        "jenkins": [
            "1.1.1.1"
        ], 
        "ungrouped": [
            "localhost"
        ]
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0   

宣言した変数すべてが期待通りである。

便利です

このようなディレクトリ&ファイル構成にしておけば、
各環境でインベントリファイルがそろっているかを比較しやすいし、
-i で指定するディレクトリ名を変えるだけで、テスト/本番の切り替えが楽にできますね。

それでは、良いインフラライフを。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?