現代 it 人一定要知道的 ansible 自動化組態技巧

56
[ chusiang @kalug ~ ] $ cat .profile # Author: 㲺Ռᗼ / chusiang.lai (at) gmail.com # Blog: http://note.drx.tw # Modified: 2016-05-31 16: 30

Upload: chu-siang-lai

Post on 16-Apr-2017

8.180 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

[ chusiang@kalug ~ ] $ cat .profile # Author: / chusiang.lai (at) gmail.com # Blog: http://note.drx.tw # Modified: 2016-05-31 16:30

Page 3: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

3

Page 4: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

OutlineI. IT

4

Page 5: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

OutlineI. IT

II.

5

Page 6: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

OutlineI. IT

II.

III. Ansible

6

Page 7: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

OutlineI. IT

II.

III. Ansible

IV. Ansible

7

Page 8: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

OutlineI. IT

II.

III. Ansible

IV. Ansible

V. Ansible

8

Page 9: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

OutlineI. IT

II.

III. Ansible

IV. Ansible

V. Ansible

VI. Q & A

9

Page 10: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ⅰ. IT

10

DevOps

Page 11: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

IT

11

IT IT

(hr) 30 (min)

code  code 

( )

Page 12: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ⅱ.

12

※ = Configuration management (CM)

Page 13: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ansible

- Ansible as Automation Glue13

Page 14: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

" "

14

Page 15: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ⅲ. Ansible

15

Page 17: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ansible DevOps

2013 3

DevOps

iThome - http://goo.gl/yJbWtz17

Page 18: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ansible

• Puppet, Salt, Chef (Infrastructure as Code)

DevOps

• Push Python SSH Angent

• Python

18

Page 19: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ⅳ. Ansible

19

Page 20: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ansible inventory Managed node SSH Python

20

Page 21: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ansible• Control Machine Ansible Managed node

Python 2.5+ SSH

21

# Debian & Ubuntu (apt).$ sudo apt-get install ansible

# Mac OS X (homebrew). $ sudo brew install ansible

# Python (pip).$ sudo pip install ansible

Page 22: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ansible• ansible.cfg inventory (host file)

Managed node ( ) SSH …

22

$ vim ansible.cfg[defaults] # inventory hostfile = hosts

# remote_user = vagrant#private_key_file = ~/.ssh/id_rsa

# host_key_checking: ssh host_key_checking = False

Page 23: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

inventory • Managed node ( )

ssh

23

$ vim hosts # ansible_ssh_host: SSH # ansible_ssh_port: SSH (Port)# ansible_ssh_user: SSH # ansible_ssh_private_key_file: SSH # ansible_ssh_pass: SSH ( )[dev]ansible-demo.local ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222

[test]ansible-test.local ansible_ssh_host=10.10.1.1 ansible_ssh_user=adeliae

[prod]ansible-prod.local ansible_ssh_host=demo.drx.tw ansible_ssh_port=22

Page 24: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ⅴ. Ansible

24

Ad-Hoc command, Playbook* (Module, Galaxy), Ansible Tower

Page 25: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ad-Hoc command

and

25

Playbook

Page 26: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ad-Hoc command • ( ) command line

26

# command line

$ ping ansible-demo.localPING localhost (127.0.0.1): 56 data bytes64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.037 ms

--- localhost ping statistics ---1 packets transmitted, 1 packets received, 0.0% packet lossround-trip min/avg/max/stddev = 0.037/0.037/0.037/0.000 ms

$ echo Hello WorldHello World

Page 27: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Ad-Hoc command • Ansible -m

Module Index

27

# ansible < > -m < >

$ ansible all -m ping ansible-demo.local | SUCCESS => { "changed": false, "ping": "pong" }

$ ansible all -m command -a "echo Hello World"ansible-demo.local | SUCCESS | rc=0 >>Hello World

Page 28: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Playbooks

• Shell Script

• YAML code

• Jinja2 (template )

...

Baby Playbook Onesie - http://goo.gl/GKJvXn28

Page 29: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Playbooks • Playbook Play Tasks

• Play*1, Task*3 Module*3 (command, apt, lineinfile)

29

$ vim example.yml ---

- name: This is a Super-basic playbook. hosts: all tasks: - name: Hello World command: echo "Hello World"

- name: Install Vim & Emacs become: yes apt: name={{ item }} state=present with_items: - vim - emacs

# emacs - name: use vi-mode in readline become: yes lineinfile: dest=/etc/inputrc line="set editing-mode vi"

Page 30: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Playbooks • Playbook Play Tasks

• Play*1, Task*3 Module*3 (command, apt, lineinfile)

30

$ vim example.yml ---

- name: This is a Super-basic playbook. hosts: all tasks: - name: Hello World command: echo "Hello World"

- name: Install Vim & Emacs become: yes apt: name={{ item }} state=present with_items: - vim - emacs

# emacs - name: use vi-mode in readline become: yes lineinfile: dest=/etc/inputrc line="set editing-mode vi"

Play

Page 31: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Playbooks • Playbook Play Tasks

• Play*1, Task*3 Module*3 (command, apt, lineinfile)

31

$ vim example.yml ---

- name: This is a Super-basic playbook. hosts: all tasks: - name: Hello World command: echo "Hello World"

- name: Install Vim & Emacs become: yes apt: name={{ item }} state=present with_items: - vim - emacs

# emacs - name: use vi-mode in readline become: yes lineinfile: dest=/etc/inputrc line="set editing-mode vi"

Task 1

Task 2

Task 3

Page 32: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Playbooks • Playbook Play Tasks

• Play*1, Task*3 Module*3 (command, apt, lineinfile)

32

$ vim example.yml ---

- name: This is a Super-basic playbook. hosts: all tasks: - name: Hello World command: echo "Hello World"

- name: Install Vim & Emacs become: yes apt: name={{ item }} state=present with_items: - vim - emacs

# emacs - name: use vi-mode in readline become: yes lineinfile: dest=/etc/inputrc line="set editing-mode vi"

Module

Page 33: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Playbooks • example.yml playbook

33

$ ansible-playbook example.yml

PLAY [This is a Super-basic playbook.] *****************************************

TASK [setup] *******************************************************************ok: [ansible-demo.local]

TASK [Hello World] *************************************************************changed: [ansible-demo.local]

TASK [Install Vim & Emacs] *****************************************************changed: [ansible-demo.local] => (item=[u'vim', u'emacs'])

TASK [use vi-mode in readline] *************************************************changed: [ansible-demo.local]

PLAY RECAP *********************************************************************ansible-demo.local : ok=4 changed=3 unreachable=0 failed=0

Page 34: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Playbooks • example.yml playbook

34

$ ansible-playbook example.yml

PLAY [This is a Super-basic playbook.] *****************************************

TASK [setup] *******************************************************************ok: [ansible-demo.local]

TASK [Hello World] *************************************************************changed: [ansible-demo.local]

TASK [Install Vim & Emacs] *****************************************************changed: [ansible-demo.local] => (item=[u'vim', u'emacs'])

TASK [use vi-mode in readline] *************************************************changed: [ansible-demo.local]

PLAY RECAP *********************************************************************ansible-demo.local : ok=4 changed=3 unreachable=0 failed=0

Setup

(Recap)

Page 35: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Live Demo

35

Page 36: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

http://s.drx.tw/ansible1.kalug

Page 37: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

https://youtu.be/L4UDVP1lJQQ

Page 38: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Module

38

Page 39: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

http://docs.ansible.com/ansible/list_of_commands_modules.html

Page 40: 現代 IT 人一定要知道的 Ansible 自動化組態技巧
Page 41: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Docs » commands Modules

yes

Page 42: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Galaxy

42

Page 43: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

https://galaxy.ansible.com

Page 44: 現代 IT 人一定要知道的 Ansible 自動化組態技巧
Page 45: 現代 IT 人一定要知道的 Ansible 自動化組態技巧
Page 46: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

– (Debian Buzz)

46

Page 47: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

• Ansible Docs - http://docs.ansible.com/ansible/intro_installation.html

• Ansible: Up and Running - https://www.ansible.com/ansible-book

• Ansible (7:15) | Software Architecture School - http://goo.gl/nhykzE

• Ansible - http://get.soft-arch.net/ansible/

• | ・ - http://school.soft-arch.net/blog/90699/

metaphor-in-cm

• Ansible by sakana / Max - https://goo.gl/e9RwhE

• Ansible | - http://goo.gl/5gs1q9

• IT Ansible | - http://goo.gl/daAtVi

47

Free

Page 48: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

• Blasts Off Space Rocket From Cosmodrom In The Clouds, Polygonal Stock Illustration | dreamstime - http://goo.gl/6FAuiQ

• - http://www.ngtaiwan.com

• Using cloud-init and uvtool to initialize cloud instances | Rui - https://goo.gl/CbdvTH

• Books icon (PSD) | GraphicsFuel - http://www.graphicsfuel.com/2012/07/books-icon-psd/

• Avatar, business, company, group, manager, people, users icon | Icon search engine - https://goo.gl/Hm6ScX

• A Galaxy Just Appeared Out of Nowhere - http://chirpnews.com/2016/04/17/new-galaxy-appeared/

48

Page 49: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

49

Page 50: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

50

http:// .tw

Page 51: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

DevOps Taiwan

https://www.facebook.com/groups/DevOpsTaiwan/

https://devopstaiwan.slack.com/

https://gitter.im/DevOpsTW/

Page 52: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

http://www.vim.tw

Page 53: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

http://coscup.org

Page 54: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

http://mopcon.org

Page 55: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

Q & A

55

Page 56: 現代 IT 人一定要知道的 Ansible 自動化組態技巧

E N D