118
Practical Ansible: A Top-down Introduction Architect @ Gogolook

Ansible 實戰:top down 觀點

Embed Size (px)

Citation preview

Page 1: Ansible 實戰:top down 觀點

Practical Ansible: A Top-down Introduction

Architect @ Gogolook

Page 2: Ansible 實戰:top down 觀點

3

--no-provision

Page 3: Ansible 實戰:top down 觀點

4

Page 4: Ansible 實戰:top down 觀點

5

Page 5: Ansible 實戰:top down 觀點

6

Modern Web 2015Bottom-up Ansible

IT

Top-down

“”

Page 6: Ansible 實戰:top down 觀點

7

Modern Web 2015Bottom-up Ansible

IT

Top-down

“”

Page 7: Ansible 實戰:top down 觀點

8

Page 8: Ansible 實戰:top down 觀點

9

☛ https://github.com/ansible/ansible

Page 9: Ansible 實戰:top down 觀點

9

☛ https://github.com/ansible/ansible

Page 10: Ansible 實戰:top down 觀點

9

☛ https://github.com/ansible/ansible

Page 11: Ansible 實戰:top down 觀點

10

VPC

CloudFront ELB API servers MongoDB

Page 12: Ansible 實戰:top down 觀點

11

Page 13: Ansible 實戰:top down 觀點

11

Page 14: Ansible 實戰:top down 觀點

12

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 15: Ansible 實戰:top down 觀點

13

ad-hoc commandsinventory

playbook - pushplaybook - pull

Page 16: Ansible 實戰:top down 觀點

13

ad-hoc commandsinventory

playbook - pushplaybook - pull

rolesselective execution

Page 17: Ansible 實戰:top down 觀點

13

ad-hoc commandsinventory

playbook - pushplaybook - pull

rolesselective execution

Page 18: Ansible 實戰:top down 觀點

13

ad-hoc commandsinventory

playbook - pushplaybook - pull

rolesselective execution

Capistrano-stylezero-downtimeblue-greenrolling upgrade

Page 19: Ansible 實戰:top down 觀點

13

ad-hoc commandsinventory

playbook - pushplaybook - pull

rolesselective execution

Capistrano-stylezero-downtimeblue-greenrolling upgrade

Page 20: Ansible 實戰:top down 觀點

rolesselective execution

Capistrano-stylezero-downtimeblue-greenrolling upgrade

14

ad-hoc commandsinventory

playbook - pushplaybook - pull

Page 21: Ansible 實戰:top down 觀點

15

Page 22: Ansible 實戰:top down 觀點

16

control machine managed node

Page 23: Ansible 實戰:top down 觀點

16

control machine managed node

Python ≥ 2.5

Page 24: Ansible 實戰:top down 觀點

16

control machine managed node

Python ≥ 2.5

SSH

Page 25: Ansible 實戰:top down 觀點

16

control machine managed node

Python ≥ 2.5

SSH

Ansible: pip install ansible yum install ansible apt-get install ansible brew install ansible

Page 26: Ansible 實戰:top down 觀點

16

control machine managed node

Python ≥ 2.5Python ≥ 2.6/2.7

SSH

Ansible: pip install ansible yum install ansible apt-get install ansible brew install ansible

Page 27: Ansible 實戰:top down 觀點

17

control machine managed node

SSH

SSH

SSH

host1

host2

host3

Page 28: Ansible 實戰:top down 觀點

17

control machine managed node

SSH

SSH

SSH

host1

host2

host3

inventory file

host1 host2 ansible_ssh_host=10.0.0.10 host3 ansible_ssh_port=2222

Page 29: Ansible 實戰:top down 觀點

18

inventory file

lb ansible_ssh_host=10.0.0.10 app1 ansible_ssh_host=10.0.0.20 app2 ansible_ssh_host=10.0.0.21 app3 ansible_ssh_host=10.0.0.22 db ansible_ssh_host=10.0.0.30

Page 30: Ansible 實戰:top down 觀點

19

ansible \ --inventory-file=hosts-vagrant \ --user=vagrant --ask-pass \ all \ -a hostname

inventory file

Page 31: Ansible 實戰:top down 觀點

19

ansible \ --inventory-file=hosts-vagrant \ --user=vagrant --ask-pass \ all \ -a hostname

inventory file

user account

Page 32: Ansible 實戰:top down 觀點

19

ansible \ --inventory-file=hosts-vagrant \ --user=vagrant --ask-pass \ all \ -a hostname

inventory file

apply to “all” hosts in the inventory file

user account

Page 33: Ansible 實戰:top down 觀點

19

ansible \ --inventory-file=hosts-vagrant \ --user=vagrant --ask-pass \ all \ -a hostname

inventory file

ad-hoc command

apply to “all” hosts in the inventory file

user account

Page 34: Ansible 實戰:top down 觀點

20

inventory filedefault: • /etc/ansible/hosts • /usr/local/etc/ansible/hosts

Page 35: Ansible 實戰:top down 觀點

20

inventory file

cp hosts-vagrant /usr/local/etc/ansible/hosts

ansible \ --user=vagrant --ask-pass \ all \ -a hostname

default: • /etc/ansible/hosts • /usr/local/etc/ansible/hosts

Page 36: Ansible 實戰:top down 觀點

21

ansible \ --user=vagrant --ask-pass \ all \ -m setup

host information

Page 37: Ansible 實戰:top down 觀點

22

Page 38: Ansible 實戰:top down 觀點

22

Page 39: Ansible 實戰:top down 觀點

23

Page 40: Ansible 實戰:top down 觀點

24

ansible \ --user=vagrant --ask-pass \ lb \ -m yum \ -a "name=openssh"

apply to the “lb” host in the inventory file

lb 10.0.0.10CentOS 7.1

Page 41: Ansible 實戰:top down 觀點

24

ansible \ --user=vagrant --ask-pass \ lb \ -m yum \ -a "name=openssh"

apply to the “lb” host in the inventory file

invoke Ansible module “yum”

lb 10.0.0.10CentOS 7.1

Page 42: Ansible 實戰:top down 觀點

24

ansible \ --user=vagrant --ask-pass \ lb \ -m yum \ -a "name=openssh"

apply to the “lb” host in the inventory file

invoke Ansible module “yum”

inspect package status

lb 10.0.0.10CentOS 7.1

Page 43: Ansible 實戰:top down 觀點

25

ansible \ --user=vagrant --ask-pass \ --become \ lb \ -m yum \ -a "name=openssh state=latest"

install or update latest package

lb 10.0.0.10CentOS 7.1

Page 44: Ansible 實戰:top down 觀點

25

ansible \ --user=vagrant --ask-pass \ --become \ lb \ -m yum \ -a "name=openssh state=latest"

install or update latest package

become “sudo” privilegelb 10.0.0.10CentOS 7.1

Page 45: Ansible 實戰:top down 觀點

26

ansible \ --user=vagrant --ask-pass \ --become \ lb:db \ -m yum \ -a "name=openssh state=latest"

apply to the “lb” and “db” hosts in the inventory file

lb 10.0.0.10 db 10.0.0.30CentOS 7.1 CentOS 7.1

Page 46: Ansible 實戰:top down 觀點

27

ansible \ --user=vagrant --ask-pass \ --become \ 'app*' \ -m apt \ -a "name=openssh-server state=latest"

apply to the “app*” hosts in the inventory file

app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

Ubuntu 14.04

Page 47: Ansible 實戰:top down 觀點

27

ansible \ --user=vagrant --ask-pass \ --become \ 'app*' \ -m apt \ -a "name=openssh-server state=latest"

apply to the “app*” hosts in the inventory file

app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

Ubuntu 14.04

invoke Ansible module “apt”

Page 48: Ansible 實戰:top down 觀點

28

Page 49: Ansible 實戰:top down 觀點

[lbservers] lb ansible_ssh_host=10.0.0.10

[appservers] app1 ansible_ssh_host=10.0.0.20 app2 ansible_ssh_host=10.0.0.21 app3 ansible_ssh_host=10.0.0.22

[dbservers] db ansible_ssh_host=10.0.0.30

29

inventory file

Page 50: Ansible 實戰:top down 觀點

30

“push” mode

Page 51: Ansible 實戰:top down 觀點

31

control machine managed node

SSH

SSH

SSH

host1

host2

host3

inventory file

Page 52: Ansible 實戰:top down 觀點

31

control machine managed node

SSH

SSH

SSH

host1

host2

host3

playbook

inventory file

Page 53: Ansible 實戰:top down 觀點

31

control machine managed node

SSH

SSH

SSH

host1

host2

host3

playbook

inventory file

Page 54: Ansible 實戰:top down 觀點

32

playbook

- hosts: lbservers:dbservers tasks: - name: update openssh yum: name=openssh state=latest

- hosts: appservers tasks: - name: update openssh apt: name=openssh-server state=latest

Page 55: Ansible 實戰:top down 觀點

33

ansible-playbook \ --user=vagrant --ask-pass \ --become \ openssh.yml

apply Ansible playbook “openssh.yml” to all hosts in the inventory

Page 56: Ansible 實戰:top down 觀點

34

Page 57: Ansible 實戰:top down 觀點

How about the “pull” mode?

35

Page 58: Ansible 實戰:top down 觀點

36

managed node

host1

host2

playbook

Page 59: Ansible 實戰:top down 觀點

36

managed node

host1

host2

playbook

• git pull …• sftp …• rsync …• wget …• …

Page 60: Ansible 實戰:top down 觀點

36

managed node

host1

host2

playbook

• git pull …• sftp …• rsync …• wget …• …

❶ansible-playbook \ --connection=local \ playbook.yml

apply locally

Page 61: Ansible 實戰:top down 觀點

37

managed node

host1

host2

playbook

❶ansible-pull --url=xxxx

Page 62: Ansible 實戰:top down 觀點

37

managed node

host1

host2

playbook

❶ansible-pull --url=xxxx

Page 63: Ansible 實戰:top down 觀點

37

managed node

host1

host2

playbook

❶ansible-pull --url=xxxx

❷apply locally❸

Page 64: Ansible 實戰:top down 觀點

Capistrano-stylezero-downtimeblue-greenrolling upgrade

ad-hoc commandsinventory

playbook - pushplaybook - pull

38

rolesselective execution

Page 65: Ansible 實戰:top down 觀點

39

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 66: Ansible 實戰:top down 觀點

40

Page 67: Ansible 實戰:top down 觀點

41

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

timezone ntp

All nodes will need these…

Page 68: Ansible 實戰:top down 觀點

42

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

haproxy

Page 69: Ansible 實戰:top down 觀點

43

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

repo-epel redis

Page 70: Ansible 實戰:top down 觀點

44

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

nodejs git

project_deploy

Page 71: Ansible 實戰:top down 觀點

45

playbook

- hosts: all tasks: ...

- hosts: lbservers tasks: ...

- hosts: appservers tasks: ...

- hosts: dbservers tasks: ...

timezone, ntp

haproxy

repo-epel, redis

nodejs, git, project_deploy

Page 72: Ansible 實戰:top down 觀點

46

ansible galaxy [pic]is your friend…

Page 73: Ansible 實戰:top down 觀點

46

ansible galaxy [pic]is your friend…

Ansible Galaxy is your friend…

Page 74: Ansible 實戰:top down 觀點

47

playbook

- hosts: all tasks: ...

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 75: Ansible 實戰:top down 觀點

47

playbook

- hosts: all tasks: ...

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 76: Ansible 實戰:top down 觀點

47

playbook

- hosts: all tasks: ...

roles: - yatesr.timezone - geerlingguy.ntp

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 77: Ansible 實戰:top down 觀點

47

playbook

- hosts: all tasks: ...

roles: - yatesr.timezone - geerlingguy.ntp

vars: timezone: Asia/Taipei ntp_timezone: Asia/Taipei

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 78: Ansible 實戰:top down 觀點

48

playbook

- hosts: appservers roles: - williamyeh.nodejs - geerlingguy.git - project_deploy

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 79: Ansible 實戰:top down 觀點

48

playbook

- hosts: appservers roles: - williamyeh.nodejs - geerlingguy.git - project_deploy

vars: project_git_repo: "https://github.com/..." project_version: "master" project_has_npm: true

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 80: Ansible 實戰:top down 觀點

49

Page 81: Ansible 實戰:top down 觀點

50

ansible-playbook \ --user=vagrant --ask-pass \ --become \ --limit=appservers \ playbook.yml

apply to all “appservers” hosts in the inventory

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 82: Ansible 實戰:top down 觀點

51

playbook

- hosts: appservers roles: - williamyeh.nodejs - geerlingguy.git - project_deploy

vars: project_git_repo: "https://github.com/..." project_version: "master" project_has_npm: true

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 83: Ansible 實戰:top down 觀點

51

playbook

- hosts: appservers roles: - williamyeh.nodejs - geerlingguy.git - project_deploy

vars: project_git_repo: "https://github.com/..." project_version: "master" project_has_npm: true

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 84: Ansible 實戰:top down 觀點

51

playbook

- hosts: appservers roles: - williamyeh.nodejs - geerlingguy.git - project_deploy

vars: project_git_repo: "https://github.com/..." project_version: "master" project_has_npm: true

- { role: project_deploy, tags: ['deploy'] }

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 85: Ansible 實戰:top down 觀點

52

ansible-playbook \ --user=vagrant --ask-pass \ --become \ --tags=deploy \ playbook.yml

apply only the roles/tasks with a “deploy” tag to all hosts in the inventory

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 86: Ansible 實戰:top down 觀點

ad-hoc commandsinventory

playbook - pushplaybook - pull

rolesselective execution

53

Capistrano-stylezero-downtimeblue-greenrolling upgrade

Page 87: Ansible 實戰:top down 觀點

54

Page 88: Ansible 實戰:top down 觀點

54

Page 89: Ansible 實戰:top down 觀點

55

Page 90: Ansible 實戰:top down 觀點

56

https://galaxy.ansible.com/list#/roles/732

Page 91: Ansible 實戰:top down 觀點

57

Page 92: Ansible 實戰:top down 觀點

57

Deploy software projects (Capistrano-like)

Page 93: Ansible 實戰:top down 觀點

58

git source

Page 94: Ansible 實戰:top down 觀點

58

git source

older build

Page 95: Ansible 實戰:top down 觀點

58

git source

older build

newer build

Page 96: Ansible 實戰:top down 觀點

58

git source

older build

newer build

current active build

Page 97: Ansible 實戰:top down 觀點

59

Page 98: Ansible 實戰:top down 觀點

60

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 99: Ansible 實戰:top down 觀點

60

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

shutdown this on purpose!

Page 100: Ansible 實戰:top down 觀點

60

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

shutdown this on purpose!

visible downtime?

Page 101: Ansible 實戰:top down 觀點

61

Page 102: Ansible 實戰:top down 觀點

62

playbook

- hosts: appservers roles: - williamyeh.nodejs - geerlingguy.git

vars: project_git_repo: "https://github.com/..." project_version: "master"

project_has_npm: true

- { role: project_deploy, tags: ['deploy'] }

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 103: Ansible 實戰:top down 觀點

63

ansible-playbook \ --user=vagrant --ask-pass \ --become \ --extra-vars='project_version=green' \ --limit=app1 \ --tags=deploy \ playbook.yml

apply only the roles/tasks with a “deploy” tag

lb 10.0.0.10 app1 10.0.0.20 app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

apply to the “app1” host

Page 104: Ansible 實戰:top down 觀點

63

ansible-playbook \ --user=vagrant --ask-pass \ --become \ --extra-vars='project_version=green' \ --limit=app1 \ --tags=deploy \ playbook.yml

apply only the roles/tasks with a “deploy” tag

lb 10.0.0.10 app1 10.0.0.20 app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

apply to the “app1” host

checkout “green” branch

Page 105: Ansible 實戰:top down 觀點

64

Page 106: Ansible 實戰:top down 觀點

65

playbook

- hosts: appservers serial: 1

roles: - williamyeh.nodejs - geerlingguy.git - { role: project_deploy, tags: ['deploy'] } vars: project_git_repo: "https://github.com/..." project_version: "master" project_has_npm: true

lb 10.0.0.10 app1 10.0.0.20app2 10.0.0.21app3 10.0.0.22

db 10.0.0.30

CentOS 7.1 CentOS 7.1Ubuntu 14.04

Page 107: Ansible 實戰:top down 觀點

ad-hoc commandsinventory

playbook - pushplaybook - pull

rolesselective execution

Capistrano-stylezero-downtimeblue-greenrolling upgrade

66

Page 108: Ansible 實戰:top down 觀點

67

Page 109: Ansible 實戰:top down 觀點

67

Page 110: Ansible 實戰:top down 觀點

67

Page 111: Ansible 實戰:top down 觀點

67

Page 112: Ansible 實戰:top down 觀點

68

Page 113: Ansible 實戰:top down 觀點

69

Page 114: Ansible 實戰:top down 觀點

70

☛ https://github.com/ansible/ansible

Page 115: Ansible 實戰:top down 觀點

70

☛ https://github.com/ansible/ansible

Page 116: Ansible 實戰:top down 觀點

70

☛ https://github.com/ansible/ansible

Page 117: Ansible 實戰:top down 觀點

71

ad-hoc commandsinventory

playbook - pushplaybook - pull

rolesselective execution

Capistrano-stylezero-downtimeblue-greenrolling upgrade

Page 118: Ansible 實戰:top down 觀點

72