More Related Content
Similar to Automate with Ansible basic (3/e) (20)
More from Chu-Siang Lai (16)
Automate with Ansible basic (3/e)
- 1. [ chusiang@tossug ~ ] $ cat .profile
# Author: 凍仁翔 / chusiang (at) drx.tw
# Blog: http://note.drx.tw
# Modified: 2017-03-22 13:44
3/e
- 2. 關於我
• 凍仁翔 (@chusiang_lai)。
• 1 年年以上的 Ansible 使⽤用經驗。
• ⽬目前維護的 Roles 有:
• gitbook
• php7 (php-fpm)
• switch-apt-mirror
• testlink
• vim-and-vi-mode
• zabbix-agent
2
- 7. Outline
I. 現代 IT ⼈人是什什麼?
II. 導入⾃自動化組態的好處是什什麼?
III. Ansible 是什什麼?
IV. 怎麼部署 Ansible 環境?
7
- 8. Outline
I. 現代 IT ⼈人是什什麼?
II. 導入⾃自動化組態的好處是什什麼?
III. Ansible 是什什麼?
IV. 怎麼部署 Ansible 環境?
V. 怎麼操作 Ansible?
8
- 9. Outline
I. 現代 IT ⼈人是什什麼?
II. 導入⾃自動化組態的好處是什什麼?
III. Ansible 是什什麼?
IV. 怎麼部署 Ansible 環境?
V. 怎麼操作 Ansible?
VI. Q & A
9
- 12. 現代 IT ⼈人是什什麼?
12
以前的 IT ⼈人 現代的 IT ⼈人
從裝機到架站 需耗費數⼩小時 (hr) 不⽤用 30 分 (min)
上班
每天敲敲指令、裝裝機器,
常常忘了了改過什什麼
寫 code 當總統
寫 code 管機器
下班 寫不完的⼯工作⽇日記
幫⾃自⼰己寫⼯工具
(為了了提早下班)
- 18. Ansible 是近年年來來知名度不
斷上升的 DevOps ⾃自動化
軟體,雖然⾃自 2013 年年創立⾄至
今不到 4 年年,但由於其採⽤用
無代理理程式的架構,部署
靈活,程式碼易易讀,因⽽而
迅速成為受矚⽬目的 DevOps
⼯工具。
iThome - http://goo.gl/yJbWtz
18
- 19. Ansible 是什什麼?
• 與 Puppet, SaltStack, Chef 並列列其四的⾃自動化組態設定
⼯工具 (Infrastructure as Code),其簡單易易⽤用的特性讓⼈人⼀一
⽤用就愛上,在 DevOps 界也佔有⼀一席之地。
• 使⽤用 Push 架構,只需 Python 和 SSH 即可操作,不⽤用額
外裝 Angent。
• Python 陣營的組態設定⼯工具。
19
- 22. 怎麼安裝 Ansible?
• 只需在 Control Machine 安裝 Ansible;Managed node
則要有 Python 2.5+ 和 SSH。
22
# Debian & Ubuntu (apt).
$ sudo apt-get install ansible
# RHEL & CentOS (yum).
$ sudo yum install ansible
# Mac OS X (homebrew).
$ brew install ansible
# Python (pip).
$ sudo pip install ansible
- 23. 怎麼設定 Ansible?
• 藉由 ansible.cfg 來來設定 inventory (host file) 檔案路路徑、
Managed node (被控端) 使⽤用者名稱、SSH ⾦金金鑰 … 等。
23
$ vim ansible.cfg
[defaults]
# 指定 inventory 檔案路路徑。
hostfile = hosts
# 遠端使⽤用者名稱
remote_user = docker
#private_key_file = ~/.ssh/id_rsa
# host_key_checking: 不詢問加入 ssh ⾦金金鑰
host_key_checking = False
- 24. inventory 是什什麼?
• 主要⽤用來來定義 Managed node (被控端) 主機位址與群組,
也可⽤用來來設定 ssh 連線資訊。
24
$ 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 ansible_ssh_host=127.0.0.1 ansible_ssh_pass=pwd
[test]
ansible-test ansible_ssh_host=172.10.10.1 ansible_ssh_port=2222
[prod]
ansible-prod ansible_ssh_host=10.10.10.1 ansible_ssh_user=deploy
- 27. Ad-Hoc command 是什什麼?
• 簡短 (臨臨時性) 的指令,與⼀一般的 command line 操作模式
雷同,⼀一次只透過⼀一⾏行行指令進⾏行行操作。
27
# ⼀一般的 command line
$ ping ansible-demo.local
PING localhost (127.0.0.1): 56 data bytes
64 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 loss
round-trip min/avg/max/stddev = 0.037/0.037/0.037/0.000 ms
$ echo Hello World
Hello World
- 28. Ad-Hoc command 是什什麼?
• -m 後的各項參參數請參參考 Ansible 官⽅方的 Module Index
⽂文件。
28
# 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
- 29. Playbooks 是什什麼?
• 比 Shell Script 更更具結構化的腳
本語⾔言,是⼀一鍵部署的好物。
• 使⽤用 YAML 格式,寫 code 就
如同寫⽂文件,簡單易易讀。
• 通常會有 Play, Task, Module。
• 可使⽤用 Jinja2 (template 系統)
表達式,並⽀支援變數、判斷式
、迴圈等語法。
Baby Playbook Onesie - http://goo.gl/GKJvXn
29
- 30. 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"
- 31. 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"
Play
- 32. 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"
Task 1
Task 2
Task 3
- 33. Playbooks 是什什麼?
• ⼀一份 Playbook 可以有多個 Play 和 多個 Tasks。
• 此例例⽤用到了了 Play*1, Task*3 和 Module*3 (command, apt, lineinfile)。
33
$ 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
- 34. Playbooks 是什什麼?
• 執⾏行行 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
- 35. Playbooks 是什什麼?
• 執⾏行行 playbook。
35
$ 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)
- 42. 練習環境為 Control Machine (Ubuntu 14.04) + Managed node*2 (Debian 8, CentOS 6)。
透過 Ansible 和 Jupyter 操控 Managed node
42
- 44. 參參考⽂文獻
• 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 ⾃自動化組態技巧》系列列⽂文章 - https://goo.gl/EOjs4I
44
Free
- 45. 圖片來來源
• 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/
45