Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Dynamic Inventoryと参照変数
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
bungoume
September 22, 2014
Technology
5.1k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Dynamic Inventoryと参照変数
Ansible Meetup in Tokyo 2014.09でのLT発表資料です。
http://ansible-users.connpass.com/event/7942/
bungoume
September 22, 2014
More Decks by bungoume
See All by bungoume
djangocongressjp2023_password_hash
bungoume
2
1.7k
日経電子版でのDjango活用事例紹介 / djangocongressjp2022-nikkei
bungoume
4
7.4k
CircleCIの活用事例とCI高速化/circleci-community-meetup3-speedup
bungoume
3
1.6k
Password Hashing djangocongress 20180519
bungoume
5
4.3k
OSSで始めるセキュリティログ収集/oss-securitylog-builderscon2017
bungoume
29
11k
日経電子版のアプリ開発を支えるログ活用術/nikkei-log-201609
bungoume
1
1.5k
Kibanaで秒間1万件のアクセスを可視化した話/nikkei-kibana-loganalyst2015
bungoume
20
17k
uwsgi-docker-pycon2015
bungoume
10
61k
Ansibleを結構使ってみた/ansible-nikkei-2015
bungoume
32
15k
Other Decks in Technology
See All in Technology
Maestro MCPとClaude Code Skillsによる自律的なE2Eテスト支援
asmz
0
310
演劇と私とスクラム
chinmo
0
170
[re:Invent2026事前勉強会(有志開催)]続きは、ラスベガスの廊下で / continue-in-the-vegas-hallway
sh_fk2
0
370
cdk deployとcdkd deploy、実際にデプロイして5倍速近くの差を実測した
yama3133
1
110
スクラム祭り2026福井トラック_閉じたループから価値のスパイラルへ
kc_nakanishi
0
190
"計画的無茶振り"を通してAIネイティブ世代の新人を育てる
subroh0508
1
120
2026-10-24 JAWS JAWS-UG 茨城 #18 IAM Policy Autopilotってなにさ。terraform planからIAM Policyつくれるの? / What's IAM Policy Autopilot
masasuzu
0
180
How we choose session proposals
kawaguti
PRO
1
180
OneLakeをデータソースにした AI Search(Foundry IQ)の ライフサイクル管理
ryuseiiida
0
140
Devin プロモコードの受け取りと適用(2026.10.27)
kinopeee
1
670
Jev の中身は50行で再現できる。でも本物との差はどこにあるのか
hisuzuya
0
140
クラウドエージェントの先駆け Devin。今も最先端を走る、他との違い
kinopeee
1
500
Featured
See All Featured
Marketing to machines
jonoalderson
1
5.9k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.8k
Being A Developer After 40
akosma
PRO
92
590k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
440
Reflections from 52 weeks, 52 projects
jeffersonlam
356
22k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
エンジニアに許された特別な時間の終わり
watany
109
250k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
4
4.4k
How STYLIGHT went responsive
nonsquared
100
6.3k
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が良い感じ • もっと良い方法があったら教えて下さい