SlideShare a Scribd company logo
マイベストVariables
設定場所
(異論、反論Welcome!)
Ansible Night in Tokyo 2018.09 LT
オイシックス・ラ・大地(株) 林 如弥
最初に
お伝えしておきたいの
ですが
タイトルの
タイトルの これ
皆様には
皆様のベストが
きっとあるはず
今から話す内容が
何かの参考になれば
幸いです
22
22
この数字、何の数か
ぴんと来ます
でしょうか?
22
Ansibleにおいて、
variablesが設定可能な
場所の数です
https://docs.ansible.com/ansible/2.6/user_guide/playbooks_variables.html#variable-scopes
公式ドキュメントより:
Variable Precedence: Where Should I Put A Variable?
(変数の優位順:どこで変数を設定すると良いの?)
22
動的なcommand line
values、vars_promptを
除いても、20箇所で
変数定義が可能です
柔軟性には感謝しつつも
乱用すれば「スパゲッティ
コード」になること
請け合い
― Figure out where to define a variable,
and don’t make it complicated. ―
 ( 変数を定義する場所を特定し、
           複雑にするな)
公式ドキュメントより:
Variable Precedence: Where Should I Put A Variable?
(変数の優位順:どこで変数を設定すると良いの?)
https://docs.ansible.com/ansible/2.6/user_guide/playbooks_variables.html#variable-scopes
そこでプラクティス
group_vars/
group1.yml
group2.yml
host_vars/
hostname1.yml
hostname2.yml
roles/
common/
vars/
main.yml
defaults/
main.yml
公式ベストプラクティスでは
https://docs.ansible.com/ansible/2.6/user_guide/playbooks_best_practices.html#best-practices
group_vars/
group1.yml    ←グループ単位の変数
group2.yml
host_vars/
hostname1.yml    ←ホスト単位の変数
hostname2.yml
roles/
common/
vars/    ←ロール単位の変数
main.yml
defaults/    ←ロール単位のデフォルト変数
main.yml
公式ベストプラクティスでは
https://docs.ansible.com/ansible/2.6/user_guide/playbooks_best_practices.html#best-practices
group_vars/
group1.yml    ←グループ単位の変数
group2.yml (ミドルウェアのバージョンなど)
host_vars/
hostname1.yml    ←ホスト単位の変数
hostname2.yml    (特定ホストだけデバッグツール有効など)
roles/
common/
vars/    ←ロール単位の変数
main.yml  (ループ要素の切り出しなど)
defaults/    ←ロール単位のデフォルト変数
main.yml    (全変数の一覧化、再利用性高めるなど)
公式ベストプラクティスでは
https://docs.ansible.com/ansible/2.6/user_guide/playbooks_best_practices.html#best-practices
「...変更する必要のあるパラメータが十数個程度であれば、
Inventoryファイルに記述した方が見通しがよくなり...」
[all:vars]
ketchup_host=192.168.33.12
ketchup_nginx_host=192.168.33.13
ketchup_port=80
[vagrant:vars]
ansible_connection=local
instance_provider=vagrant
インフラCI実践ガイドでは
https://www.shoeisha.co.jp/book/detail/9784798155128
https://github.com/infra-ci-book/ketchup-vagrant-ansible/blob/master/hosts/ketchup/inventory
マイベスト
プラクティス
group_vars/
all.yml    ←共通設定(基本これを育てる)
host_vars/
hostname1.yml    ←ホスト単位の変数
hostname2.yml     (必要なら作る)
vars/
site1/ ←各サイト(複数のWebサービスがある)単位で
dev.yml ←開発、本番環境の差異を定義
prd.yml
site2/
roles/     ←ロール内では変数は定義しない
マイベストプラクティス
https://docs.ansible.com/ansible/2.6/user_guide/playbooks_best_practices.html#best-practices
Point.1
group_vars/all.yml
● とにかくall.ymlを育てる
○ 共通変数は全てall.yml
○ all.yml以外は置かない
○ all.ymlを見れば全体を知れる
Point.2
host_vars/
● 必要なら作る
○ 必要なくなれば消す
○ 極力作らない
Point.3
roles/
● 変数ファイルを持たない
○ 処理のみ記述する場所と定義
○ 調査時に潜る場所を減らす
Point.4
vars/site/xxx.yml
● サイト/環境の差異のみ記載
○ 基本はgroup_vars/all.ymlで定義され
ている
○ 違いが一目瞭然
なぜこうしたのか
● 手動構築の古株サーバが大半現役
● 基盤刷新を機に標準化を行いたい
● all.ymlで標準を定義
● 徐々にhost_varsを消していく
「標準化、統合への意志」をディレクトリ構成
へ反映 (ディレクトリ構成における”逆コンウェイ戦略”といってもいいかも?)
実は絶賛試行中...
● host_varsなかなか消せない(TT
● ついgroup_vars作りたくなる(^^;
● 今は突き進む予定(๑•̀ㅂ•́)‫و‬
● 仲間募集してます(><
(略称:オイラ大地)
まとめ
● ansibleのvariablesはとても柔軟
● 複雑にならないようにルールを決める
● ディレクトリ構造に戦略を持つ(のもありか
も)
ご静聴ありがとうございました m(_ _)m

More Related Content

PDF
I have a problem when operating AWS with multiple accounts
PDF
My misstake on Ansible’s lineinfile module
PDF
AWS SSO x On-Prem AD Easy IAM user management on Jtf2021
PDF
AWS Cognito makes old web apps available from anywhere
PDF
アドベントカレンダー から学ぶOCIの空気感
PDF
オンボーディングを楽しむ
PDF
事前アンケート集計 Terraform meetup tokyo#2
PDF
I want the power of onboarding!
I have a problem when operating AWS with multiple accounts
My misstake on Ansible’s lineinfile module
AWS SSO x On-Prem AD Easy IAM user management on Jtf2021
AWS Cognito makes old web apps available from anywhere
アドベントカレンダー から学ぶOCIの空気感
オンボーディングを楽しむ
事前アンケート集計 Terraform meetup tokyo#2
I want the power of onboarding!

More from Yukiya Hayashi (20)

PDF
How did you start learning Azure
PDF
My feelings of going to the first conference overseas
PDF
Let's split text by awk command
PDF
What i feel when began use AWS CodePipeline as GitLab Ci user
PDF
How to get rid of terraform plan diffs
PDF
Task and Time monitoring with Backlog and Toggl
PDF
Oiradaichi's Akamai Journey
PDF
What does the monitoring tool use at oisix ra daichi?
PDF
We love backlog ! in reCap event.
PDF
What we expect of neo4j
PDF
Backlog World 2019 LT - We love backlog !
PDF
20190116 neo4jug-lt
PDF
Introduce that Best practices for writing Dockerfiles
PDF
postgresql conference 2018 on-premise to rds
PDF
Zabbix Conference Japan 2018 LT Configure by Ansible
PDF
Jenkins User Conference 2018 Tokyo LT
PDF
ジョブ管理でcronは限界があったので”Rundeck”を使ってハッピーになりました
PDF
20171117 redminetokyo13
PDF
PostgreSQL DBのバックアップを一元化しよう
PDF
Attrasian JIRAをv6.3.15からv7.3.2へバージョンアップした話
How did you start learning Azure
My feelings of going to the first conference overseas
Let's split text by awk command
What i feel when began use AWS CodePipeline as GitLab Ci user
How to get rid of terraform plan diffs
Task and Time monitoring with Backlog and Toggl
Oiradaichi's Akamai Journey
What does the monitoring tool use at oisix ra daichi?
We love backlog ! in reCap event.
What we expect of neo4j
Backlog World 2019 LT - We love backlog !
20190116 neo4jug-lt
Introduce that Best practices for writing Dockerfiles
postgresql conference 2018 on-premise to rds
Zabbix Conference Japan 2018 LT Configure by Ansible
Jenkins User Conference 2018 Tokyo LT
ジョブ管理でcronは限界があったので”Rundeck”を使ってハッピーになりました
20171117 redminetokyo13
PostgreSQL DBのバックアップを一元化しよう
Attrasian JIRAをv6.3.15からv7.3.2へバージョンアップした話
Ad

ansiblejp-best-variables-place