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
Ansibleで始めるサーバ構成の自動化
Search
Sorarinu
February 13, 2020
Technology
320
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ansibleで始めるサーバ構成の自動化
Sorarinu
February 13, 2020
Transcript
22 slides
ハンズオンあり! Ansible で始める サーバ構成の自動化 WILLGATE Ansible Day 2020 インフラユニット 高畑
達也
Infrastructure as Code ( IaC ) ?
インフラの構成管理をコード化し、Git 管理やコードレビュー、CI などソフトウェア開発 のプラクティスを適用するもの。 IaC の代表的なものとして Puppet、Chef、Ansible、Terraform などがある。 DevOps ロードマップ(https://roadmap.sh/devops)にも組み込まれている。
Infrastructure as Code ( IaC ) とは
IaC を取り入れることの メリット・デメリット
IaC を取り入れることのメリデメ メリット • 手作業による作業ミスのリスクがなくなる • コードレビューを行うことで品質の向上を図 れる • バージョン管理することにより過去に遡れる
• コード化することで別環境でも使いまわせる • 本番環境に SSH しなくて良い デメリット • それなりに学習コストがかかる • サーバ構成の変更に若干の時間がかかる • ガチガチに管理し始めると心が疲弊してくる • きちんとコード規約を定めて定期的に更新し ておかないとコードが腐る
Ansible
Ansible とは • RedHat 社が提供している OSS ◦ https://github.com/ansible/ansible • サーバのミドルウェアをコードで構築・管理ができる
• エージェントが不要で SSH 接続ができればプロビジョニング可能 • 冪等性があり、何度実行しても同じ結果が得られる(はず) • 構成ファイルは基本的に YAML で書く必要がある
基本的なディレクトリ構造 . ├── group_vars ・・・・・・・・・・・ ホスト、環境ごとの変数を定義 │ └── testing.yml ├── all.yml ・・・・・・・・・・・
全てのホストで実行する playbook ├── webservers.yml ・・・・・・・・・・・ ホストグループごとの playbook ├── site.yml ・・・・・・・・・・・ どの playbook を読み込むか(いわゆる main 関数的な) ├── hosts ・・・・・・・・・・・ 各サーバの SSH 情報などを記述する └── roles ・・・・・・・・・・・ ミドルウェアをインストールするなどの role をここで用意する └── hello_world └── tasks │ └── main.yml ・・・・・・・・・・・ hello_world role の task を書く ├── templates │ └── hello.txt.j2 ・・・・・・・・・・・ task で設置したいファイルなど └── files Ansible ディレクトリ構成ベストプラクティス
Playbook と冪等性 Ansible の Playbook は、リモートホストのあるべき状態を定義したファイルで、これ に書かれた定義をもとにリモートホストへ反映される。 原則として、Playbook を変更しない限り何度実行してもリモートホストの状態に変 更が無いように
Playbook を作成することが冪等性を担保するためには重要であ る。 https://dev.classmethod.jp/server-side/ansible/ansible_playbook_with_idempotence/
Playbook と冪等性 Ansible の Playbook は、リモートホストのあるべき状態を定義したファイルで、これ に書かれた定義をもとにリモートホストへ反映される。 原則として、Playbook を変更しない限り何度実行してもリモートホストの状態に変 更が無いように
Playbook を作成することが冪等性を担保するためには重要であ る。 https://dev.classmethod.jp/server-side/ansible/ansible_playbook_with_idempotence/ これがめっっちゃたいへん
Playbook と Role Playbook に直接 task を書いていく方法もあるが、これはメソッドを分けないプログ ラミングと同様。 基本的には再利用可能な単位( nginx
をセットアップするなど)で分割した role を用 意して読み込むのがベスト。
Try it !
Ansible をインストールしよう ▪ Amazon Linux / RHEL / CentOS >
sudo yum -y install epel-release > sudo yum --enablerepo=epel install ansible > ansible --version ▪ Mac > brew install ansible > ansible --version
GitHub からリポジトリを clone してくる > git clone https://github.com/Sorarinu/ansible-practice.git > cd
ansible-practice
インベントリファイルを編集する > make init > vim hosts [servers] hogehoge ansible_ssh_host=xxx.xxx.xxx.xxx
ansible_ssh_port=22
インベントリファイルを編集する > make init > vim hosts [servers] hogehoge ansible_ssh_host=xxx.xxx.xxx.xxx
ansible_ssh_port=22 ホスト名などホストが 識別できるもの ローカルから実行する場合は 対象サーバのパブリック IP アドレス 対象サーバ上から実行する場合は 127.0.0.1 などのループバックアドレス
Dry-Run を実行してみよう > make ansible/dry-run PRIVATE_KEY_PATH=SSH鍵のパス この段階では実際に反映 はされてなくて、差分だけ 確認できます
Dry-Run で問題がなければ適用しよう > make ansible/apply PRIVATE_KEY_PATH=SSH鍵のパス
Ansible を書いてみよう 以下を実現する Ansible の Playbook を書いてみましょう! • 英語の格言、名言を出力してくれる fortune-mod
を yum でインストール • fortune の実行結果を Ansible の実行結果に出力する ansible yum module ansible shell register debug ヒント
Ansible を書いてみよう https://github.com/Sorarinu/ansible-practice/pull/1/files
まとめ
まとめ • DevOps を推進していく上で IaC の知識は必須になってきている • 人は弱い生き物なので本番環境に SSH したくなってしまうけどなるべくやめよう
(やめたい)
More Decks by Sorarinu
See All by Sorarinu
WILLGATE Infra Day 2019
sorarinu
0
8.9k
vimは尊い
sorarinu
0
2.6k
Other Decks in Technology
See All in Technology
Microsoft Entra Kerberos で実現するクラウド デバイス管理
tamaiyutaro
2
670
Context != Context: One window, many lifecycles (AI Native DevCon, Nov 2026)
birgitta410
0
120
GCASアップデート(202607-202610)
techniczna
1
320
DevOps Agentにあらゆるセキュリティインシデントのトリアージを任せた件
jirtosterone
1
130
Gifu WordPress Meetup #200 に向けて、私たちには何ができるだろうか?/ 2026-11-07 Gifu WordPress Meetup
torounit
0
280
うちなーぐちが認識できるなら、秋田弁もいけるはず!? Amazon Transcribe方言チューニング検証
ryouto715
1
170
大江戸Ruby会議でデータベース クライアントの話をするのは 間違っているだろうか
yahonda
1
120
2026-11-11 Tamachi.sre#7 PaCにインフラリソースの命を救われました。 / PaC saves the infra resource life
masasuzu
0
150
AWS Quick 入門 | 目指せ Quick マスター 01
inariku
PRO
2
340
【Oracle AI SPOTLIGHT ウェビナー】【特別開催:日本語で解説!】Oracle AI World 2026 最新情報フィードバック
oracle4engineer
PRO
1
200
AIエージェントの監視、思考を見るか行動を見るか / Monitoring AI Agents: Thoughts or Actions
yusukeshimizu
2
190
AI時代における医療情報システムのセキュリティ / Security of Healthcare IT Systems in the AI Era
henryofficial
0
160
Featured
See All Featured
The Invisible Side of Design
smashingmag
301
52k
From π to Pie charts
rasagy
1
440
Skip the Path - Find Your Career Trail
mkilby
1
290
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
350
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
SEO for Brand Visibility & Recognition
aleyda
0
4.9k
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
For a Future-Friendly Web
brad_frost
184
11k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
460
Optimizing for Happiness
mojombo
378
71k