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
Ansible把握した 1日目
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Masaru OKI
February 20, 2021
Technology
0
390
Ansible把握した 1日目
構成管理ツールAnsibleをざっと触ってみて、現時点での理解をまとめてみました。
Masaru OKI
February 20, 2021
Tweet
Share
More Decks by Masaru OKI
See All by Masaru OKI
SONiCを自前でビルドする話
imasaruoki
2
1.7k
NPLによるデータプレーンプログラミング
imasaruoki
8
2.7k
SONiC近況報告 2019/Fall
imasaruoki
1
1.3k
ホワイトボックススイッチをAnsibleで操る話
imasaruoki
2
2.6k
ホワイトボックススイッチとNOSを取り巻く状況について
imasaruoki
3
2.6k
SONICイントロダクション
imasaruoki
1
550
SONiCをはじめてみよう
imasaruoki
6
2.3k
SONiCで設定するFRRouting
imasaruoki
0
1.9k
SONiCトラブルシューティング
imasaruoki
4
640
Other Decks in Technology
See All in Technology
AWSの資格って役に立つの?
tk3fftk
2
340
Abuse report だけじゃない。AWS から緊急連絡が来る状況とは?昨今の攻撃や被害の事例の紹介と備えておきたい考え方について
kazzpapa3
1
740
AI時代のSaaSとETL
shoe116
1
150
楽しく学ぼう!ネットワーク入門
shotashiratori
1
390
Google系サービスで文字起こしから勝手にカレンダーを埋めるエージェントを作った話
risatube
0
190
Everything Claude Code を眺める
oikon48
8
4.6k
NewSQL_ ストレージ分離と分散合意を用いたスケーラブルアーキテクチャ
hacomono
PRO
4
350
クラウド × シリコンの Mashup - AWS チップ開発で広がる AI 基盤の選択肢
htokoyo
2
250
身体を持ったパーソナルAIエージェントの 可能性を探る開発
yokomachi
1
120
社内レビューは機能しているのか
matsuba
0
130
オレ達はAWS管理をやりたいんじゃない!開発の生産性を爆アゲしたいんだ!!
wkm2
4
530
AI実装による「レビューボトルネック」を解消する仕様駆動開発(SDD)/ ai-sdd-review-bottleneck
rakus_dev
0
140
Featured
See All Featured
Discover your Explorer Soul
emna__ayadi
2
1.1k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.8k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
400
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
270
HDC tutorial
michielstock
1
540
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
2.4k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.9k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
180
Designing for humans not robots
tammielis
254
26k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
120
Transcript
Ansible把握した 1日目 Feb 20, 2021 Masaru OKI @masaru0714
Ansible? 構成管理ツール。ChefとかPuppetとかも構成管理ツール。 サーバやネットワーク機器にいろいろな設定を入れるためのツール。 対象によるがソフトウェアのインストールなどもできる。 sshで入れてPythonが動く機器ならどうにかできる。 インストールは公式サイトの指示に従って。
ansibleとansible-playbook ワンライナー的な一発実行はansibleコマンド(全ホストにpingを打つとか) ansible all -m ping 複数のタスクをまとめて実行する場合はansible-playbookコマンド ansible-playbook deploy.yml ホストの一覧(とsshすべきIPアドレス)はinventoryと呼ばれるファイルに書く。
(ファイル名がinventoryというわけではない)
実行準備 ターゲットホストにsshでログインできるようにしておく必要がある。 ターゲットホストに~/.ssh/authorized_keysを置くのが一般的のようだ。 rootである必要はないが、その場合sudoできること(doasなども選べる)。
ansible-playbook おおざっぱな概略図 h1 h2 h3 h4 [db] h1 ansible_ssh_host=192.168.0.1 h2
ansible_ssh_host=192.168.0.2 [web] h3 ansible_ssh_host=192.168.0.3 h4 ansible_ssh_host=192.168.0.4 hosts (inventory) ホストとグループの定義 変数も定義できる - hosts: db roles: - dev-sec.mysql - hosts: web roles: - nginxinc.nginx Playbook ホストやグループごとに何 を構成するかの定義 taskを直接書くか、あるい はroleを指定する nodes: h1: mysql_databases: - name: customer h2: mysql_databases: - name: statistics h3: nginx_start: false h4: nginx_debug_output: true role dev-sec.mysql role nginxinc.nginx customer statistics nginx install nginx install nginx start debug output 外部変数ファイル (これもinventory) 構成の設定値や挙動の指定 この例ではnodesグループのホ ストh1,h2,h3,h4の変数を定義 している role=定義済task task (mysql_db呼出) task (apt呼出)
用語解説 moduleは構成対象のホストで実行されるプログラム。 inventoryはパラメータの集合。ホストの識別名とIPアドレスが必要。 taskはinventoryから必要な情報を引き出してmoduleを使って実行。 roleは繰り返し使われるtaskのパラメータを変数にして呼出可能にしたもの。 Playbookはホストあるいはグループとtaskやroleとの組み合わせを記述する。
taskとmodule Playbook tasks: - name: やることを人間にわかるように書く ここがmodule名: moduleに与えるパラメータ: 値 をわらわら並べる
- name: Webサーバを起動する service: name: httpd state: started
taskとinventory inventory h1: webserver: started Playbook - hosts: h1 tasks:
- name: Webサーバを用意する(起動はinventory次第) service: name: httpd state: {{ webserver }}
roleとtask Playbookに記載するtaskを別ファイルに切り出して名前をつけたのがrole Playbook roles: - role名 で呼び出す。 • 呼び出したroleがどんな変数を参照しているかはドキュメントかソースを読む。 •
自作roleを用意することもできる。 roles/role名/task/main.ymlに - name: から記述する。 • 既存のroleがたくさんあり、ansible-galaxyコマンドでインストールできる。 • ansible-galaxy search キーワードで検索できる。 • ありすぎてどれを使えばいいのかわからない。 公式があればなるべくそれを。
Playbookとtask,role Playbook - hosts: 識別子(ホストでもグループでもいい) tasks: - name: 識別子のホストで実行するタスクを並べる service:
name: httpd roles: - gobgp - hosts: 識別子 以下略
inventory(変数)はどこに書く? 書ける箇所がたくさんありすぎる。 • グローバルなのは/etc/ansible/hosts • ansible-playbookコマンドのパラメータで直接指定 • ansible-playbookコマンドの-iでファイルを指定できる • Playbookの中でvars:指定で書くこともできる
• host_vars/ホスト識別子.yml に書くこともできる • group_vars/グループ識別子.yml に書くこともできる • group_vars/all.yml に全部まとめて書くこともできる どこに書かれた値を使うかの優先順位がある。 複数箇所に書くと混乱するので注意。
inventory(hosts)のフォーマット hosts (INI形式) [Guests] guest1 ansible_ssh_host=192.168.122.116 guest2 ansible_ssh_host=192.168.122.117 hosts.yml (YAML
形式) all: children: Guests: hosts: guest1: ansible_ssh_host: 192.168.122.116 guest2: ansible_ssh_host: 192.168.122.117
hosts(あるいはhosts.yml)の場所 ansible.cfg(実はこれの場所もあちこちに用意できる)に [defaults] inventory = 任意のpath と書いておくと、/etc/ansible/hostsではなくそこが参照される。 あるいは、明示的に ansible-playbook -i
任意のpath と指定する。
やってみた(VMの2本めの足にIPアドレスを振る) • VMが起動していることが前提。 • IPアドレスは192.168.122.116/24でホストからアクセスできる。 • 試したVMはUbuntu 20.04LTS • アカウントはubuntu
• ~/.ssh/authorized_keys配置済 • パスワードレスでsudoできることも確認済。 • ネットワークインタフェースはens3, ens4。ens3はAnsibleとの通信用。
やってみた(VMの2本めの足にIPアドレスを振る) deploy.yml - hosts: Guests user: ubuntu become: yes become_method:
sudo roles: - mrlesmithjr.config-interfaces hosts [Guests] guest ansible_ssh_host=192.168.122.116 group_vars/all nodes: guest: network_interfaces: - name: ens4 method: static address: 192.168.0.1 netmask: 255.255.255.0 ansible.cfg [defaults] inventory=hosts 実行 ansible-galaxy install mrlesmithjr.config-interfaces ansible-playbook -i hosts deploy.yml
まとめ • Ansible便利! • 下準備がちょっと面倒だけど! • role山盛りあってどれがちゃんと使えるか探すの大変だけど!