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
社内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
960
Other Decks in Technology
See All in Technology
Git 研修【MIXI 26新卒技術研修】
mixi_engineers
PRO
2
660
モバイル研修【MIXI 26新卒技術研修】
mixi_engineers
PRO
1
260
データベース研修【MIXI 26新卒技術研修】
mixi_engineers
PRO
1
500
PHPで作って学ぶリアルタイム音声対話AIとWebSocket入門 by ムナカタ
munakata
0
170
StepFunctionsとGraphRAGを活用した暗黙知活用のためのRAG基盤
yakumo
0
180
「待ち時間」の消滅と「自我消耗」の加速:生成AI時代のエンジニアを救うメンタル・リソース管理
poropinai1966
0
250
AIで楽になるはずが、なぜ疲れる?
kinopeee
0
120
システム監視を 「システムを監視するだけ」で 終わらせないために
seiud
0
130
数値で見る Microsoft MVP 〜Spec Kit と GitHub Copilot Agent で作るデータ可視化ダッシュボード〜
yutakaosada
0
150
人手不足への挑戦:車両保全を支えるIoTとクラウド内製化の道【SORACOM Discovery 2026】
soracom
PRO
0
110
歴史から理解するクラウドインフラのしくみ
kizawa2020
0
180
ここは地獄!つらい朝会を体験することで、チームとしてのより良い振る舞いに気づくワークショップ / The stand-up meeting from hell in the game industry
scrummasudar
0
360
Featured
See All Featured
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
390
Believing is Seeing
oripsolob
1
170
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.7k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
450
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
200
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
430
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
How to train your dragon (web standard)
notwaldorf
97
6.7k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
How to Ace a Technical Interview
jacobian
281
24k
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!