Ansible入門...?

43
Ansible 入門 #pyfes 2013.11 in Tokyo by @r_rudi(しろう) …?

description

Ansible 紹介 at #pyfes 2013.11

Transcript of Ansible入門...?

Page 1: Ansible入門...?

Ansible 入門

#pyfes 2013.11 in Tokyo

by @r_rudi(しろう)

…?

Page 2: Ansible入門...?

Ansible

Page 3: Ansible入門...?

Ansible間違い

正しい 正解

Page 4: Ansible入門...?

Chef

Puppet

Salt

cfengine

juju

…..

Page 5: Ansible入門...?

Provisiong tool

構成管理ツール

Page 6: Ansible入門...?

構成

管理

Page 7: Ansible入門...?
Page 8: Ansible入門...?

by Lee Thompson at Velocity 2010

Page 9: Ansible入門...?

一部誇張表現があります

Page 10: Ansible入門...?

Assumptions

本日の前提知識

Page 11: Ansible入門...?

Task

- name: install python

homeblew: name=python

installs_options={{ option }}

state=present

Page 12: Ansible入門...?

Task

- name: install python

homeblew: name=python

installs_options={{ option }}

state=present

モジュール名

引数

変数

タスク名(省略可)

状態: absent を指定すると消せる(moduleによる)

Page 13: Ansible入門...?

Playbook == Taskのセット

- hostname:

name=AnsibleDemo

- apt_repository:

repo=’deb http://….’

- apt_key:

url=http://…..

実行順

Page 14: Ansible入門...?

実行方法

% ansible-playbook hoge.yml

-i inventory file (接続先ホストのリスト)

-u username

-k ssh pass

-C check mode

-D diff表示

Page 15: Ansible入門...?

OK

Let' Go !

Page 16: Ansible入門...?

unarchive module

- unarchive: src=blah.tar.gz dest=/tmp/

Local

Remote

Remote

copy unzip

untar

Page 17: Ansible入門...?

- shell: foo.sh

shell module

Local

copyRemote

Remote

Run

Run

Page 18: Ansible入門...?

ec2

- local_action: ec2

args:

instance_type: c1.medium

image: emi-329394

count: 3

Page 19: Ansible入門...?

Launch Instances module

- Google Compute Engine

- Digital Ocean

- Linode

- Rackspace

- Docker

:

Page 20: Ansible入門...?

インスタンスはいくつ?- local_action: ec2

args:

count: 3

- local_action: ec2

args:

count: 3

- local_action: ec2

args:

count: 3

Page 21: Ansible入門...?

インスタンスはいくつ?- local_action: ec2

args:

count: 3

- local_action: ec2

args:

count: 3

- local_action: ec2

args:

count: 3

Page 22: Ansible入門...?

インスタンスはいくつ?- local_action: ec2

args:

count: 3

- local_action: ec2

args:

count: 3

- local_action: ec2

args:

count: 3

9冪等性...?

Page 23: Ansible入門...?

ec2 elb

- local_action: ec2_elb

args:

instance_id:

“{{ ansible_ec2_instance_id }}”

state: present

Page 24: Ansible入門...?

deploy !!

- local_action: ec2_elb

args: state=absent

- nagios: action=disable_alert

- git: repo=.... dest=/www version=release-11

- service: name=foo state=restarted

- wait_for: port=8080 state=started

:

Page 25: Ansible入門...?

rolling update

- serial: 1

-一台ずつ実行できる

-数台まとめてもできる

Remote

Remote

Remote

Page 26: Ansible入門...?

#pyfes ですから…

Python API

Page 27: Ansible入門...?

from ansible.inventory import Inventory

from ansible.playbook import PlayBook

from ansible import callbacks

from flask import Flask, render_template

import json

app = Flask(__name__)

@app.route("/play")

def play():

inventory = Inventory('localhost.conf')

stats = callbacks.AggregateStats()

playbook_cb =

callbacks.PlaybookCallbacks()

ansible + flask

runner_cb =

callbacks.PlaybookRunnerCallbacks(stats)

results = PlayBook(playbook='pyfes-

demo.yml',

forks=1,

remote_user='shirou',

sudo=False,

module_path='module',

callbacks=playbook_cb,

runner_callbacks=runner_cb,

stats=stats,

inventory=inventory).run()

return json.dumps(results)

Page 28: Ansible入門...?

AnsibleWorks AWX

Page 29: Ansible入門...?

Demo

or Die

Page 30: Ansible入門...?

Web UI demo

Page 31: Ansible入門...?

- twilio APIを使うshell script

本当のデモ: twilio module

#!/usr/bin/env sh

AccountSid=AAAAAAAAA

AuthToken=07999999999999

curl -X POST 'https://api.twilio.com/2010-04-

01/Accounts/ACe0361e5b6236a8948191d08635bcd449/Calls.json' ¥

-d 'From=%2B815031596145' -d 'To=%2B81999999993' ¥

-d 'Url=http%3A%2F%2Fexample.com%2Fansible.html' -u

${AccountSid}:${AuthToken}

echo "changed=True"

exit 0

Page 32: Ansible入門...?

module 作成

-超簡単

-スクリプト言語ならなんでも書ける

-残念ながらgoは無理

- YAMLで書きにくいなら気軽にmoduleを作成

-サーバの./libraryに置いておけば使ってくれる

Page 33: Ansible入門...?

handler

tasks:

- template: src=/srv/hoge.j2 dest=/etc/hoge

notify:

- restart apache

handlers:

- name: restart apache

service: name=httpd state=restarted

Page 34: Ansible入門...?

何台ぐらい扱えるの?

Page 35: Ansible入門...?

We have users using Ansible in push

mode against 5000 machines at a time

Page 36: Ansible入門...?

Accelarated mode

- hosts: all

accelerate: true

tasks: ...

- SSHでdaemonを起動

-あとはdaemonと直接通信

-このdaemonは通信が

終わると自動で終わる

- 2-8x faster than SSH

Page 37: Ansible入門...?

モジュール紹介

Page 38: Ansible入門...?

Arista networks

- 10G/40G/100Gのスイッチ

- sshで入れる

- pythonが入っている

- Ansibleの初期からのスポンサー

Page 39: Ansible入門...?

Arista modules

- name: enable interface Ethernet 1

arista_interface:

interface_id=Ethernet1 admin=up

speed=10g duplex=full logging=true

Page 40: Ansible入門...?

DB

- mongodb_user

- mysql_db

- mysql_replication

- postgres_user

- postgres_db

- riak

- redis

Page 41: Ansible入門...?

notification

- irc

- hipchat

- jabber

- mail

- osx_say

Page 42: Ansible入門...?

まとめ

- Ansibleは自動実行ツールとしても使えるよ

- moduleたくさんうれしいな

- notification moduleは便利

-欲しいものがなければ簡単に作れるよ

- python必要ないよ

-実行速度は十分速いよ

Page 43: Ansible入門...?

Ansible 本

-最近出ました

-平易な英語

-広い範囲をカバー

-今日飛ばした部分