0
0

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の備忘録(環境分離編)

Last updated at Posted at 2018-10-20

Ansibleを環境毎に使い分ける

やりたいこと

  • 環境毎にansibleを使い分ける
    • dev /stg
  • verifiロールで実行環境のenvを表示させて確認する

検証時のAnsibleディレクトリ構造とplaybook

├── bastion.yml
├── group_vars
│   ├── all
│   ├── env_dev
│   │   └── main.yml
│   ├── env_stg
│   │   └── main.yml
│   └── role_bastion
├── inventory
│   ├── dev
│   └── stg
└── roles
    └── verifi
        └── tasks
            └── main.yml
bastion.yml
- hosts: role_bastion
  user: ubuntu
  become_method: sudo
  gather_facts: yes

  roles:
     - verifi

環境分離にinventoryを使う

今回はdevとstgをそれぞれ用意した。

  • inventory/dev
[role_bastion]
localhost

[env_dev:children]
role_bastion
  • inventory/stg
[role_bastion]
localhost

[env_stg:children]
role_bastion

それぞれの環境名を変数をセットする

/group_vars/env_dev/main.yml
---
env: dev
group_vars/env_stg/main.yml
---
env: stg

環境毎の変数を参照しているか確認

inventoryでdev/stgを指定して出力を確認する

  • devを指定して実行する
    • $ ansible-playbook -i inventory/dev bastion.yml -D
TASK [verifi : echo environment] ***********************************************************************
ok: [localhost] => (item=dev) => {
    "dev": "VARIABLE IS NOT DEFINED!",
    "item": "dev"
}
  • stgを指定して実行する
    • $ansible-playbook -i inventory/stg bastion.yml -D
TASK [verifi : echo environment] ***********************************************************************
ok: [localhost] => (item=stg) => {
    "item": "stg",
    "stg": "VARIABLE IS NOT DEFINED!"
}

まとめ

inventoryを使って、環境を指定してAnsibleを実行することができた。
これで、複数の環境をひとつのリポジトリでコード管理してプロビジョニングをすることができる。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?