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
データエンジニアこそ組織のオントロジーに向き合うべき — 問いに答えるAIから、事業を動かすAIへ
gappy50
4
950
AI研修(Day2)【MIXI 26新卒技術研修】
mixi_engineers
PRO
1
1k
インシデント事例と パッケージの全量解析に学ぶ ソフトウェアサプライチェーンの守り方 / supply-chain-attack-defense
flatt_security
0
1.1k
iOS/Androidの二刀流エンジニアがFlutter & TypeScriptへ越境後の現在地 - Flutterがメインになって見えた景色と現在の醍醐味 / Dual-Platform Mobile Engineer Shifts to Flutter & TypeScript - The View and Real Thrill of Going Flutter-First
bitkey
PRO
0
110
なぜMIXIはゼロトラスト基盤として クラウドフレアを選んだのか - Cloudflare Peer Point SASE User Voices
mixi_engineers
PRO
2
110
現場で使える AWS DevOps Agent 活用ノウハウ - Release Management 機能の検証結果を添えて / AWS DevOps Agent Release Management and Know-How
kinunori
3
260
現場との対話から始める “作る前に問い直す”業務改善
mochico50
2
330
VPCセキュリティ対応の最新事情
nagisa53
1
340
Jitera Company Deck
jitera
0
620
ソフトウェアアーキテクチャ研修【MIXI 26新卒技術研修】
mixi_engineers
PRO
2
740
AIとハーネスで育てるトランスコンパイラ / 20260722 Yasushi Katayama
shift_evolve
PRO
4
1k
13年運用タイトルのサーバーサイドが辿り着いた現在地 ― モンスターストライクにおける技術・組織・AI活用から得た知見
mixi_engineers
PRO
1
210
Featured
See All Featured
Crafting Experiences
bethany
1
230
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
230
Automating Front-end Workflow
addyosmani
1370
210k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
64
56k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
600
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
320
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.5k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.2k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.3k
Agile that works and the tools we love
rasmusluckow
331
22k
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!