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
社内LT_Ansible
Search
okochang
October 08, 2014
Technology
240
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
社内LT_Ansible
社内LTでAnsibleについて話した資料
okochang
October 08, 2014
More Decks by okochang
See All by okochang
LT対戦_Season4
okochang
0
120
AWSのAPIを使うときに気をつけてること
okochang
1
2k
もしお金を支払わなかった時、あなたに何がおきますか?
okochang
0
200
ITインフラ業務自動化現状確認会
okochang
1
970
Other Decks in Technology
See All in Technology
USB PDを使ってスマホを労わりたい
raspython3
0
180
LambdaWebFunctionsを試してみた / exploring-lambda-web-functions
koukihosaka
1
340
しろおびから始める!ログ活用の第一歩
shumei_ito
0
950
QA4AI 2026-Oct 生成AI時代の品質保証
tokuty
1
860
DevelopersIO 2026 知識の地図を描く ─ オントロジーとは何か?
ishikawa_satoru
1
1.7k
実践!Cloud Run ハンズオン!! / Cloud Run handson
y0hgi
2
280
会社説明資料
persolserverworks
0
9.8k
Temporal 最高!でも改めて「日付」について調べたら人類の負債すぎる件
riyaamemiya
0
150
Rustuna: Rustによる高速なOptuna実装
pfn
PRO
0
190
SAENM ~Jupyterで残すPoCの試行錯誤、未来の自分と仲間を助ける記録!~
terapyon
0
110
初海外で注意すべきこと12選!!!安全快適なre:invent生活にするために
yuuuuuuu168
1
160
Post AI時代のリファクタリング実践記 / Refactoring in the post AI era
kaminashi
1
230
Featured
See All Featured
The SEO Collaboration Effect
kristinabergwall1
1
590
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
430
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
550
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
410
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
630
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
730
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
750
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.3k
Statistics for Hackers
jakevdp
799
230k
Transcript
社内LT Ansible サービス開発チーム 柳瀬 任章
自己紹介 •やなせ ひであき •@oko_chang •サービス開発グループ •HR4⇒HR6 •持ち場 •風呂場 •便所場 •土日の調理場
検討したネタ •Middleman •Mackerel •同じアドレス帯のVPCとVPNする話 •Ansible
今日話さないこと プログラミング言語は人間が楽になるため に作られてきました。しかし何が楽かは、 言語によってまちまちです。 言語は道具です。ある言語が自分の目的に 適しているのかどうかは、その言語を使う ことで自分がどれだけの生産性を発揮でき るかでわかります。 ! コーディングを支える技術
基本編
Ansibleとは •Configuration Management Tool •Chef,Puppet •Playbooks •Recipe,Manifest •ロゴがダサい
構成
インストール① # Via Pip $ sudo easy_install pip $ sudo
pip install ansible
インストール② # Via Yum $ sudo rpm -ivh http://ftp.riken.jp/ Linux/fedora/epel/6/i386/epel-
release-6-8.noarch.rpm $ sudo yum install ansible
インストール③ # Via Apt $ sudo apt-add-repository ppa:rquillo/ansible $ sudo
apt-get update $ sudo apt-get install ansible
インストール④ # Via Homebrew $ brew update $ brew install
ansible
対象バージョン $ ansible --version ansible 1.6.2
構文 # 書式 $ ansible 対象 -m モジュール $ ansible
対象 -a 'コマンド'
実行例 $ ansible all -m ping ansible01.okochang.com | success >>
{ "changed": false, "ping": "pong" } $ ansible all -a "uname -r" ansible01.okochang.com | success | rc=0 >> 3.10.35-43.137.amzn1.x86_64
対象ホストの指定
対象ホストの指定 •ファイルにホスト名を記入する •/etc/ansible/hosts •export ANSIBLE_HOSTS="config path"
ホストの指定方法① # グルーピングが出来ます [developments] dev.okochang.com ! [webservers] www01.okochang.com www02.okochang.com
ホストの指定方法② # ポートをデフォルトから変えたい [dbservers] db01.okochang.com:22222 db02.okochang.com:22222
ホストの指定方法③ # ローカルIPアドレスをaliasで指定 [local] local.okochang.com ansible_ssh_port=22 ansible_ssh_host=192.168.0.10
ホストの指定方法④ # 復数ホストを一度に指定したい。 [appservers] app-[01:10].okochang.com app-[a:d].okochang.com
実行時のホスト指定 $ ansible web* -m ping $ ansible webservers:dbservers -m
ping $ ansible webservers:\!webserver01 - m ping
オプション編
実行時オプション① # SSHのパスワード認証をしたい $ ansible all -m ping --ask-pass
実行時オプション② # SSH秘密鍵のパスを指定したい $ ansible all -m ping \ --private-key=”~/.ssh/yourkey.pem”
実行時オプション③ # SSHユーザーを指定したい $ ansible all -m ping -u ec2-user
実行時オプション④ # sudoをしたい $ ansible all -m ping --sudo
実行時オプション⑤ # ユーザーを指定してsudoをしたい $ ansible all -m ping --sudo \
--sudo-user okochang
実行時オプション⑥ # パスワードを指定してsudoをしたい $ ansible all -m ping --sudo \
--ask-sudo-pass
実行時オプション⑦ # バッチ処理などをしたい $ ansible all -B 180 -a "sleep
120" $ ansible all -m async_status \ -a "jid=51511436813.4715"
設定編
Ansibleの設定 •以下のファイルに記入する •- /etc/ansible/ansible.cfg •- ~/.ansible.cfg
設定項目を紹介 [defaults] host_key_checking=True fork=5 ask_sudo_pass=True hostfile=/etc/ansible/hosts log_path=/var/log/ansible.log nocolor=0 remote_port=22
AWS編
AWS環境への対応 $ sudo pip install boto $ cd .ansible $
curl -LO https://raw.github.com/ansible/ansible/devel/plugins/ inventory/ec2.py $ curl -LO https://raw.github.com/ansible/ansible/devel/plugins/ inventory/ec2.ini $ vi .bash_profile export AWS_ACCESS_KEY_ID='xxxxxxxxxxxxxxxxxxxx' export AWS_SECRET_ACCESS_KEY='yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' export EC2_INI_PATH=~/.ansible/ec2.ini $ chmod +x .ansible/ec2.py $ .ansible/ec2.py --list
AWS環境へ実行① # 全てのEC2インスタンスを対象 $ ansible ec2 -i .ansible/ec2.py -u ec2-user
-m ping
AWS環境へ実行② # リージョン限定 $ ansible ap-northeast-1 - i .ansible/ec2.py -u
ec2-user -m ping
AWS環境へ実行③ # AZ限定 $ ansible ap-northeast-1a - i .ansible/ec2.py -u
ec2-user -m ping
AWS環境へ実行④ # セキュリティグループ限定 $ ansible security_group_develop - i .ansible/ec2.py -u
ec2-user -m ping
AWS環境へ実行⑤ # タグ限定 $ ansible tag_Name_webserver01 - i .ansible/ec2.py -u
ec2-user -m ping
モジュール編
Module? •Ansibleにおける処理の最小単位 •指定したモジュールが対象サーバに実 行される •ChefユーザーにとってはResourcesに近 い感じ
モジュールを使う① # shellモジュール $ ansible webservers -m shell -a 'ping
-c 3 google.co.jp'
モジュールを使う② # copyモジュール $ ansible webservers -m copy -a "src=~/test.txt
dest=/tmp/test.txt"
モジュールを使う③ # fileモジュール $ ansible webservers -m file -a "dest=/tmp/test.txt
mode=600 owner=foo group=var"
モジュールを使う④ # yumモジュール $ ansible webservers -m yum -a "name=httpd
state=installed" -u ec2- user --sudo $ ansible webservers -m yum -a "name=postfix state=removed" -u ec2- user --sudo
モジュールを使う⑤ # serviceモジュール $ ansible webservers -m service -a "name=httpd
state=started" -u ec2-user -- sudo $ ansible webservers -m service -a "name=httpd state=restarted" -u ec2-user -- sudo $ ansible webservers -m service -a "name=httpd state=stopped" -u ec2-user -- sudo
Playbooks編
Playbooks? •Ansibleにおいて対象サーバーに処理す る内容を記載しておくもの •Configuration •Deployment •Orchestration •ymlフォーマットで記入
Playbooksを使うその① # test_con.yml - hosts: all remote_user: ec2-user tasks: -
name: test connection ping: ================================= $ ansible-playbook .ansible/playbooks/ test_con.yml
Playbooksを使うその② # shell_result.yml - hosts: ec2 remote_user: ec2-user tasks: -
name: run command and ignore the result shell: /bin/echo foo ================================= $ ansible-playbook -i .ansible/ec2.py -u ec2- user .ansible/playbooks/shell_result.yml
Playbooksを使うその③ # foo_var.yml - name: echo foo command: /bin/echo foo
- name: echo var command: /bin/echo var ================================= # hoge.yml - hosts: ec2 remote_user: ec2-user tasks: - include: foo_var.yml
Playbooksを使う④ # webserver01.yml - hosts: tag_Name_target01 sudo: yes tasks: -
name: ensure apache is installed yum: pkg=httpd state=installed - name: apacche running service: name=httpd state=startet enabled=yes
Playbooksを使う⑤ ! # webserver02.yml - hosts: tag_Name_target01 vars: http_port: 80
sudo: yes tasks: - name: create virtual host file for {{ vhost }} template: src=~/vhost.conf.j2 dest=/etc/httpd/conf.d/{{ vhost }}.conf notify: - restart apache handlers: - name: restart apache service: name=httpd state=restarted
Playbook Roles編
Playbook Roles? •playbookをincludeして再利用する •決められたディレクトリやファイル構 成に従うことで再利用がしやすくなる
ディレクトリ構成 . ├── roles │ ├── common │ │ ├──
files │ │ ├── handlers │ │ │ └── main.yml │ │ ├── meta │ │ ├── tasks │ │ │ └── main.yml │ │ ├── templates │ │ │ └── clock.j2 │ │ └── vars │ └── webservers └── site.yml
site.yml - name: common configuration to all node hosts: ec2
user: ec2-user roles: - common - name: webserver configuration to Role web hosts: tag_Role_web user: ec2-user roles: - webservers
実行 $ ansible-playbook \ -i ~/.ansible/ec2.py ./site.yml
まとめ •復数ホストにコマンドが実行出来るの 便利だし、すぐに役立ちそう •きちんと運用する場合はPlaybooksや Rolesを使って管理をする •ansible-galaxyやansible-towerなど他 にも面白そうなのがあります
参考 •Ansibleの公式ドキュメント http://docs.ansible.com/index.html •Playbookのサンプル https://github.com/ansible/ansible- examples •Ansibleの概要を知るのに良さそう http://apatheia.info/blog/2013/04/ 06/about-ansible/
Enjoy Ansible!