SlideShare a Scribd company logo
[ chusiang@tossug ~ ] $ cat .profile
# Author: 凍仁翔 / chusiang (at) drx.tw
# Blog: http://note.drx.tw
# Modified: 2017-03-22 13:44
3/e
關於我
• 凍仁翔 (@chusiang_lai)。
• 1 年年以上的 Ansible 使⽤用經驗。
• ⽬目前維護的 Roles 有:
• gitbook
• php7 (php-fpm)
• switch-apt-mirror
• testlink
• vim-and-vi-mode
• zabbix-agent
2
坐穩囉
要開始了了!
3
Outline
I. 現代 IT ⼈人是什什麼?
4
Outline
I. 現代 IT ⼈人是什什麼?
II. 導入⾃自動化組態的好處是什什麼?
5
Outline
I. 現代 IT ⼈人是什什麼?
II. 導入⾃自動化組態的好處是什什麼?
III. Ansible 是什什麼?
6
Outline
I. 現代 IT ⼈人是什什麼?
II. 導入⾃自動化組態的好處是什什麼?
III. Ansible 是什什麼?
IV. 怎麼部署 Ansible 環境?
7
Outline
I. 現代 IT ⼈人是什什麼?
II. 導入⾃自動化組態的好處是什什麼?
III. Ansible 是什什麼?
IV. 怎麼部署 Ansible 環境?
V. 怎麼操作 Ansible?
8
Outline
I. 現代 IT ⼈人是什什麼?
II. 導入⾃自動化組態的好處是什什麼?
III. Ansible 是什什麼?
IV. 怎麼部署 Ansible 環境?
V. 怎麼操作 Ansible?
VI. Q & A
9
Ⅰ. 現代 IT ⼈人是什什麼?
10
DevOps
DevOps 是什什麼?
Agile Tour 台中 by 正瑋 x Mina @ 2017.01.14
11
現代 IT ⼈人是什什麼?
12
以前的 IT ⼈人 現代的 IT ⼈人
從裝機到架站 需耗費數⼩小時 (hr) 不⽤用 30 分 (min)
上班
每天敲敲指令、裝裝機器,
常常忘了了改過什什麼
寫 code 當總統

寫 code 管機器
下班 寫不完的⼯工作⽇日記
幫⾃自⼰己寫⼯工具 

(為了了提早下班)
Ⅱ. 導入⾃自動化組態的好處是什什麼?
13
※ 註:組態 = Configuration management (CM)
導入 Ansible ⾃自動化組態,
我們可以減少服務中斷時間
、測試基礎建設、降低意外
風險,更更可以讓開發、測試
和正式環境無縫接軌。
圖片來來源 - Ansible as Automation Glue
14
"⼯工⼈人" 組態 ⾃自動化組態
重複組態的⼈人⼒力力成本 ⾼高 低
⼈人為失誤風險 ⾼高 低
可測試性 難 易易
模組化 難 易易
提早下班 難 易易
導入⾃自動化組態的好處是什什麼?
15
Ⅲ. Ansible 是什什麼?
16
Ansible 取名⾃自知名⼩小說

《安德的遊戲》,是
虛構的超光速通訊裝置。
有了了它我們就可以像 Ender

指揮戰艦般的操控伺服器。
電影海海報 - https://goo.gl/4xftZT
17
Ansible 是近年年來來知名度不
斷上升的 DevOps ⾃自動化
軟體,雖然⾃自 2013 年年創立⾄至
今不到 4 年年,但由於其採⽤用

無代理理程式的架構,部署
靈活,程式碼易易讀,因⽽而
迅速成為受矚⽬目的 DevOps
⼯工具。
iThome - http://goo.gl/yJbWtz
18
Ansible 是什什麼?
• 與 Puppet, SaltStack, Chef 並列列其四的⾃自動化組態設定
⼯工具 (Infrastructure as Code),其簡單易易⽤用的特性讓⼈人⼀一
⽤用就愛上,在 DevOps 界也佔有⼀一席之地。
• 使⽤用 Push 架構,只需 Python 和 SSH 即可操作,不⽤用額
外裝 Angent。



• Python 陣營的組態設定⼯工具。
19
Ⅳ. 怎麼部署 Ansible 環境?
20
觀念念、安裝、設定
Ansible 是怎麼運作的?
透過 inventory 定義 Managed node,並藉由 SSH 與 Python 進⾏行行溝通。
21
怎麼安裝 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
怎麼設定 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
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
Ⅴ. 怎麼操作 Ansible?
25
Ad-Hoc command, Playbook* (Module)
Ad-Hoc
command
and
26
Playbook
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
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
Playbooks 是什什麼?
• 比 Shell Script 更更具結構化的腳
本語⾔言,是⼀一鍵部署的好物。
• 使⽤用 YAML 格式,寫 code 就
如同寫⽂文件,簡單易易讀。
• 通常會有 Play, Task, Module。
• 可使⽤用 Jinja2 (template 系統)
表達式,並⽀支援變數、判斷式

、迴圈等語法。
Baby Playbook Onesie - http://goo.gl/GKJvXn
29
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"
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
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
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
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
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)
Module
36
http://docs.ansible.com/ansible/list_of_commands_modules.html
Automate with Ansible basic (3/e)
Docs » commands Modules
有 yes 就⼀一定要⽤用
https://goo.gl/ntmX4n
實 戰 演 練
Practice
41
練習環境為 Control Machine (Ubuntu 14.04) + Managed node*2 (Debian 8, CentOS 6)。
透過 Ansible 和 Jupyter 操控 Managed node
42
Chu-Siang Lai / chusiang@drx.tw
Play the Ansible on Jupyter notebook !
參參考⽂文獻
• 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
圖片來來源
• 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
46
http://萍⽔水相逢.tw
DevOps Taiwan
https://www.facebook.com/groups/DevOpsTaiwan/
https://devopstaiwan.slack.com/
https://gitter.im/DevOpsTW/
Ansible Taiwan
https://telegram.me/ansible_tw
https://github.com/ansible-tw
http://ansible.tw
Q & A
嚴禁拍打餵食
49
E N D

More Related Content

What's hot (20)

PDF
20150604 docker 新手入門
azole Lai
 
PPTX
Docker 基礎介紹與實戰
Bo-Yi Wu
 
PPTX
浮云脱水小说站的搭建
jondynet
 
PDF
Vagrant教學
Onnie Fong
 
PDF
Deployment with Capistrano
旭 張
 
PDF
Docker應用
Jui An Huang (黃瑞安)
 
XLS
Puppet安装总结
Yiwei Ma
 
PPT
善用工具
wang hongjiang
 
PDF
Ruby on Rails 開發環境建置 for Ubuntu
MarsZ Chen
 
PDF
Ruby on Rails 開發環境建置 for Mac
MarsZ Chen
 
PDF
Docker
旭 張
 
PPTX
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
scott liao
 
PPTX
Shell,信号量以及java进程的退出
wang hongjiang
 
PPTX
OpenResty/Lua Practical Experience
Ho Kim
 
PDF
Node js实践
jay li
 
PPT
Node.js在淘宝的应用实践
taobao.com
 
PPT
编辑器设计U editor
taobao.com
 
PPTX
Docker tutorial
azole Lai
 
PPT
Software Engineer Talk
Larry Cai
 
PPTX
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
Will Huang
 
20150604 docker 新手入門
azole Lai
 
Docker 基礎介紹與實戰
Bo-Yi Wu
 
浮云脱水小说站的搭建
jondynet
 
Vagrant教學
Onnie Fong
 
Deployment with Capistrano
旭 張
 
Puppet安装总结
Yiwei Ma
 
善用工具
wang hongjiang
 
Ruby on Rails 開發環境建置 for Ubuntu
MarsZ Chen
 
Ruby on Rails 開發環境建置 for Mac
MarsZ Chen
 
Docker
旭 張
 
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
scott liao
 
Shell,信号量以及java进程的退出
wang hongjiang
 
OpenResty/Lua Practical Experience
Ho Kim
 
Node js实践
jay li
 
Node.js在淘宝的应用实践
taobao.com
 
编辑器设计U editor
taobao.com
 
Docker tutorial
azole Lai
 
Software Engineer Talk
Larry Cai
 
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
Will Huang
 

Viewers also liked (6)

PDF
Continuous Delivery Workshop with Ansible x GitLab CI
Chu-Siang Lai
 
PDF
Git and Github basic with SourceTree
Chu-Siang Lai
 
PDF
Automate with Ansible basic (2/e, English)
Chu-Siang Lai
 
PDF
Continuous Delivery with Ansible x GitLab CI
Chu-Siang Lai
 
PDF
Continuous Delivery with Ansible x GitLab CI (2e)
Chu-Siang Lai
 
PDF
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)
Chu-Siang Lai
 
Continuous Delivery Workshop with Ansible x GitLab CI
Chu-Siang Lai
 
Git and Github basic with SourceTree
Chu-Siang Lai
 
Automate with Ansible basic (2/e, English)
Chu-Siang Lai
 
Continuous Delivery with Ansible x GitLab CI
Chu-Siang Lai
 
Continuous Delivery with Ansible x GitLab CI (2e)
Chu-Siang Lai
 
Continuous Delivery Workshop with Ansible x GitLab CI (2nd+)
Chu-Siang Lai
 
Ad

Similar to Automate with Ansible basic (3/e) (13)

PPTX
Ansible 101
YI-CHING WU
 
PDF
Ansible introduction
Chyi-Shian Jiang
 
PDF
Continuous Delivery Workshop with Ansible x GitLab CI (3rd)
Chu-Siang Lai
 
PDF
ansible
Claire Chang
 
PDF
Is it really easy for companies to import Ansible automation
Chu-Siang Lai
 
ODP
新北市教師工作坊 -- Bash script programming 介紹
fweng322
 
ODP
GNU Build System
imacat .
 
PPTX
20121111 linux intro
Chang Mt
 
PPT
20030623 linuxbasic and-security
建融 黃
 
PDF
Continuous Delivery Workshop with Ansible x GitLab CI (5th)
Chu-Siang Lai
 
PDF
跟著 geerlingguy 大神
一起測試 Ansible Roles
Chen Cheng-Wei
 
PDF
Introduce to Linux command line
Wen Liao
 
PDF
makefile20141121
Kevin Wu
 
Ansible 101
YI-CHING WU
 
Ansible introduction
Chyi-Shian Jiang
 
Continuous Delivery Workshop with Ansible x GitLab CI (3rd)
Chu-Siang Lai
 
ansible
Claire Chang
 
Is it really easy for companies to import Ansible automation
Chu-Siang Lai
 
新北市教師工作坊 -- Bash script programming 介紹
fweng322
 
GNU Build System
imacat .
 
20121111 linux intro
Chang Mt
 
20030623 linuxbasic and-security
建融 黃
 
Continuous Delivery Workshop with Ansible x GitLab CI (5th)
Chu-Siang Lai
 
跟著 geerlingguy 大神
一起測試 Ansible Roles
Chen Cheng-Wei
 
Introduce to Linux command line
Wen Liao
 
makefile20141121
Kevin Wu
 
Ad

More from Chu-Siang Lai (13)

PDF
My DevOps Tour 2.3
Chu-Siang Lai
 
PDF
The System Engineer in Agile Team
Chu-Siang Lai
 
PDF
My DevOps Tour 0.1
Chu-Siang Lai
 
PDF
See the Agile from Mindset
Chu-Siang Lai
 
PDF
Writing skills for Information Technology
Chu-Siang Lai
 
PDF
My DevOps Tour 1.0
Chu-Siang Lai
 
PDF
helloTux 2012
Chu-Siang Lai
 
PDF
Unity & Googlizer
Chu-Siang Lai
 
PDF
使用 Multi-sites 技術快速建置多 Drupal 網站
Chu-Siang Lai
 
PDF
歡迎來到 Ubuntu 9.10 Release Party (台中)
Chu-Siang Lai
 
PDF
無痛入門 Chromecast
Chu-Siang Lai
 
ODP
Ubuntu 藍星侵略計劃
Chu-Siang Lai
 
PDF
Intro of Network, WiFi on Ubuntu
Chu-Siang Lai
 
My DevOps Tour 2.3
Chu-Siang Lai
 
The System Engineer in Agile Team
Chu-Siang Lai
 
My DevOps Tour 0.1
Chu-Siang Lai
 
See the Agile from Mindset
Chu-Siang Lai
 
Writing skills for Information Technology
Chu-Siang Lai
 
My DevOps Tour 1.0
Chu-Siang Lai
 
helloTux 2012
Chu-Siang Lai
 
Unity & Googlizer
Chu-Siang Lai
 
使用 Multi-sites 技術快速建置多 Drupal 網站
Chu-Siang Lai
 
歡迎來到 Ubuntu 9.10 Release Party (台中)
Chu-Siang Lai
 
無痛入門 Chromecast
Chu-Siang Lai
 
Ubuntu 藍星侵略計劃
Chu-Siang Lai
 
Intro of Network, WiFi on Ubuntu
Chu-Siang Lai
 

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
  • 4. Outline I. 現代 IT ⼈人是什什麼? 4
  • 5. Outline I. 現代 IT ⼈人是什什麼? II. 導入⾃自動化組態的好處是什什麼? 5
  • 6. Outline I. 現代 IT ⼈人是什什麼? II. 導入⾃自動化組態的好處是什什麼? III. Ansible 是什什麼? 6
  • 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
  • 10. Ⅰ. 現代 IT ⼈人是什什麼? 10 DevOps
  • 11. DevOps 是什什麼? Agile Tour 台中 by 正瑋 x Mina @ 2017.01.14 11
  • 12. 現代 IT ⼈人是什什麼? 12 以前的 IT ⼈人 現代的 IT ⼈人 從裝機到架站 需耗費數⼩小時 (hr) 不⽤用 30 分 (min) 上班 每天敲敲指令、裝裝機器, 常常忘了了改過什什麼 寫 code 當總統 寫 code 管機器 下班 寫不完的⼯工作⽇日記 幫⾃自⼰己寫⼯工具 (為了了提早下班)
  • 15. "⼯工⼈人" 組態 ⾃自動化組態 重複組態的⼈人⼒力力成本 ⾼高 低 ⼈人為失誤風險 ⾼高 低 可測試性 難 易易 模組化 難 易易 提早下班 難 易易 導入⾃自動化組態的好處是什什麼? 15
  • 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
  • 20. Ⅳ. 怎麼部署 Ansible 環境? 20 觀念念、安裝、設定
  • 21. Ansible 是怎麼運作的? 透過 inventory 定義 Managed node,並藉由 SSH 與 Python 進⾏行行溝通。 21
  • 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
  • 25. Ⅴ. 怎麼操作 Ansible? 25 Ad-Hoc command, Playbook* (Module)
  • 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)
  • 39. Docs » commands Modules 有 yes 就⼀一定要⽤用
  • 41. 實 戰 演 練 Practice 41
  • 42. 練習環境為 Control Machine (Ubuntu 14.04) + Managed node*2 (Debian 8, CentOS 6)。 透過 Ansible 和 Jupyter 操控 Managed node 42
  • 43. Chu-Siang Lai / [email protected] Play the Ansible on Jupyter notebook !
  • 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
  • 50. E N D