Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Dynamic Inventoryと参照変数
Search
bungoume
September 22, 2014
Technology
2
4.9k
Dynamic Inventoryと参照変数
Ansible Meetup in Tokyo 2014.09でのLT発表資料です。
http://ansible-users.connpass.com/event/7942/
bungoume
September 22, 2014
Tweet
Share
More Decks by bungoume
See All by bungoume
djangocongressjp2023_password_hash
bungoume
2
1.3k
日経電子版でのDjango活用事例紹介 / djangocongressjp2022-nikkei
bungoume
4
5.8k
CircleCIの活用事例とCI高速化/circleci-community-meetup3-speedup
bungoume
3
1.5k
Password Hashing djangocongress 20180519
bungoume
5
4k
OSSで始めるセキュリティログ収集/oss-securitylog-builderscon2017
bungoume
29
11k
日経電子版のアプリ開発を支えるログ活用術/nikkei-log-201609
bungoume
1
1.4k
Kibanaで秒間1万件のアクセスを可視化した話/nikkei-kibana-loganalyst2015
bungoume
20
17k
uwsgi-docker-pycon2015
bungoume
10
60k
Ansibleを結構使ってみた/ansible-nikkei-2015
bungoume
32
15k
Other Decks in Technology
See All in Technology
いつの間にか入れ替わってる!?新しいAWS Security Hubとは?
cmusudakeisuke
0
130
Should Our Project Join the CNCF? (Japanese Recap)
whywaita
PRO
0
340
Model Mondays S2E04: AI Developer Experiences
nitya
0
140
生成AI開発案件におけるClineの業務活用事例とTips
shinya337
0
260
生成AI活用の組織格差を解消する 〜ビジネス職のCursor導入が開発効率に与えた好循環〜 / Closing the Organizational Gap in AI Adoption
upamune
7
5.3k
Delegating the chores of authenticating users to Keycloak
ahus1
0
140
20250705 Headlamp: 專注可擴展性的 Kubernetes 用戶界面
pichuang
0
270
マネジメントって難しい、けどおもしろい / Management is tough, but fun! #em_findy
ar_tama
7
1.1k
Delta airlines Customer®️ USA Contact Numbers: Complete 2025 Support Guide
deltahelp
0
710
ゼロからはじめる採用広報
yutadayo
3
960
United Airlines Customer Service– Call 1-833-341-3142 Now!
airhelp
0
170
AWS Organizations 新機能!マルチパーティ承認の紹介
yhana
1
280
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
299
21k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.5k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.9k
Six Lessons from altMBA
skipperchong
28
3.9k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
Agile that works and the tools we love
rasmusluckow
329
21k
What's in a price? How to price your products and services
michaelherold
246
12k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
We Have a Design System, Now What?
morganepeng
53
7.7k
Faster Mobile Websites
deanohume
307
31k
Transcript
Dynamic Inventoryと参照変数 Ansible Meetup Tokyo 2014.09 @bungoume
内容 • 話すこと • Dynamic Inventory(EC2)について • 環境ごとに変数を使い分けるプラクティス • 話さないこと
• Ansible変数展開の順序
Dynamic Inventoryとは • Hostsをスクリプトで動的生成 • EC2やGCE, Zabbixなどの外部リソースからHostsを生成できる • プラグインとして用意 •
https://github.com/ansible/ansible/tree/devel/plugins/inventory • EC2.pyの場合 • タグやセキュリティグループ、リージョンなどを元にホストグループを管理 • ec2.iniで対象リージョンを制限したり、フィルタをかけたりできる • hostsとして利用できる • hosts/ ec2 ec2.ini • ansible-playbook –i hosts webservers.yml
EC2のAnsible管理用タグ付け例 • タグを用いて管理 • タグの例 • hostname: dbserver01a • env:
production, dev • role: webserver, dbserver • number: 01, 02, … • group: a, b, …. • ec2モジュールでインスタンス起動時にタグ付け or 手動でタグ付け • タグを指定してplaybook実行 • 例:ゾーンBにあるdev環境のwebserverにplaybookを実行 • ansible-playbook –i hosts webservers.yml --limit tag_env_dev:&ap-northeast-1b
こういうことがしたい • 環境とホストグループによって 変数の値を切り替えたい • 例: monitoring roleのlog_server変数(送り先) • production
の webserver は 10.0.1.100 • production の dbserver は 10.0.1.101 • dev の webserver は 10.0.2.100 • dev の dbserver は 10.0.2.101 • limitのand実行では難しい(playbook内で条件分岐が必要になる) 10.0.1.100 10.0.1.101 10.0.2.100 10.0.2.101
思いついた方法(2つ) • 方法1: inventory varsを使う • 方法2: vars_filesを使い、うまいこと読み込む
• hostsディレクトリに変数ファイルを置く hosts/ dev/ ec2 (ec2.py) ec2.ini (instance_filters = tag:env=dev)
group_vars/webserver (変数yaml) production/ …. group_vars/webserver (変数yaml) webservers.yml (playbook) • cat hosts/dev/group_vars/webserver --- log_server: 10.0.2.100 方法1: inventory varsを使う
- hosts: tag_role_webserver user: ec2-user tasks: - debug: var=log_server 方法1:
inventory varsを使う • Playbook ( webservers.yml ) • 実行 • ansible-playbook –i hosts/dev webservers.yml • group_vars/webserverを読み込み、 hosts/production/group_vars/webserverで上書きする ~~~ TASK: [debug var=log_server] ******************************************************** ok: [10.0.2.1] => { “log_server": "10.0.2.100" }
hosts/ ec2, ec2.ini vars/ nothing.yml tag_role_webserver.yml tag_env_dev/ tag_role_webserver.yml webservers.yml (playbook)
方法2: vars_filesを使う • 以下の様なディレクトリ構成 • エラー回避用にnothing.ymlという空ファイルを用意しておく
方法2: vars_filesを使う • Playbook(webservers.yml)に以下を記述 • 実行 • ansible-playbook –i hosts
webservers.yml --limit tag_env_dev • まずtag_role_webserver.ymlがあれば読込み、 tag_env_dev/tag_role_webserver.ymlがあれば上書きする - hosts: tag_role_webserver user: ec2-user vars_files: - [“vars/{{ ec2_tag_role }}.yml”, vars/nothing.yml] - [“vars/{{ ec2_tag_env }}/{{ ec2_tag_role }}.yml”, vars/nothing.yml] tasks: - debug: var=log_server
まとめ • 方法1( inventory vars ) • playbookの書き換えが不要 • limitを忘れて全体実行するリスクがない
• Inventoryファイルと組み合わせても使いやすい • 方法2( vars_files ) • 多段に利用可能 • {{ ec2_tag_env }}/{{ ec2_tag_role }}/{{ ec2_tag_group }}.yml • 読込優先度が自由に変更できる • まずは方法1、変数が増え、複雑な場合は方法2が良い感じ • もっと良い方法があったら教えて下さい