1 of 22

淺談 Ansible 自動化管理工具

黃俊宏 sakana / Max

2 of 22

Agenda

  • Ansible ? Why Ansible?
  • 預備知識
  • 安裝 Ansible
  • The role of Ansible
  • 與 shell script 的差異??
  • Hello Ansible!! 第一個 ansible 指令
  • Inventory file / ansible.cfg 介紹
  • Ansible Module 介紹與實作
  • Playbook 介紹與實做
  • Reference

3 of 22

Who is Max?

  • Study Area 小雜工
  • GNOME Foundation Member
  • GNOME.Asia Committee Member
  • openSUSE members

4 of 22

5 of 22

About Ansible

  • Ansible - configuration management tool 組態管理工具
  • Online resource 線上資源
    • Documentation 文件
      • http://docs.ansible.com/
    • Ansible Galaxy
      • https://galaxy.ansible.com/
      • 社群共享 Role 集散地
    • Ansible Project Google Group 討論群組

6 of 22

Why Ansible

  • Infrastructure as Code
  • 語法簡單(play book 以YAML 語法撰寫)
  • 不需要安裝client (clientless)
  • Push-based
    • Pull-based: Agent check to server by time. (Chef / Puppet by default)
    • Push-based: Server push change by order.
    • 你可以決定何時進行設定.
  • Very thin layer of abstraction 以原有的習慣進行部署
    • Ansible focus on writing playbooks that designed to run on specific operating system. You don't need to learn a new set of abstractions that hide the differences between operating systems.

7 of 22

預備知識?

  • 透過 SSH 連接遠端主機
  • 使用命令列操作 (pipes and redirection).
  • 安裝套件.
  • 使用 sudo 指令.
  • 管理檔案權限.
  • 管理 services .
  • 了解 / 設定環境變數.
  • 寫 scripts ( Any language )

8 of 22

安裝 Ansible

  • http://docs.ansible.com/ansible/intro_installation.html
  • openSUSE
    • # zypper install ansible
  • Mac
    • $ sudo easy_install pip
    • $ sudo pip install ansible
  • CentOS 7
    • $ sudo yum install epel-release -y
    • $ sudo yum install ansible
  • Ubuntu
    • $sudo apt-get install ansible

9 of 22

The role of Ansible

Control Machine 控制端

  • 用於控制部署遠端的機器.
  • 需要 python 2.6 以上版本.

Managed Node 遠端主機

  • 一個或是多個遠端主機
  • 需要 python 2.5 以上版本.

control machine

remote host

remote host

remote host

SSH

Playbook

10 of 22

與shell script 的差異??

  • 不用登入遠端去抓 shell script 與 執行 ( 中央集權 )
  • 針對所有主機同時進行, 按照順序執行任務
  • 有許多現成的 Module 可用,用法習慣跟原系統差不多
  • 有別人寫好的 Role 可以參考與套用 ( Ansible-galaxy )
  • 語法簡單, 容易上手( YAML )
  • 可以利用 fact 與變數執行 loop 或是其他的做法.

11 of 22

Hello Ansible! 第一個 ansible 指令

實驗環境

  • JupyterHub
    • IP:
    • Port:
    • 帳號: ansible 密碼: StudyArea2015
  • 請修改 playbook目錄下 hosts
    • 填入 遠端 IP於 ansible_ssh_host=
    • 填入 遠端 port 於 ansible_ssh_port=
    • 記得 File -- > Save

Hands on Lab

  • 方式 1:
    • 開啓 playbook目錄下 Ansible_SA
      • 按 shift + Enter 執行 block
    • 嘗試執行第一個 ansible 指令
    • ansible 對象 -m ping
  • 方式 2:
    • New -- > Terminal
    • 切換到 playbook 目錄下手動執行指令

12 of 22

Inventory file

  • Ansible lists hosts in text files, called inventory files.
  • 將遠端主機相關資訊以文字檔案的方式建立稱為 inventory file, 常見的檔案名稱為 hosts
  • 語法 servername options
  • 可以設定群組 [ 群組名稱 ] 來組織對象
  • 常用選項
    • ansible_ssh_host -- Remote Host IP
    • ansible_ssh_user -- Remote SSH User Name
    • ansible_ssh_private_key_file -- SSH Key
    • ansible_ssh_port -- ssh port
    • ansible_ssh_pass -- ssh password
  • 如果有定義到 ansible.cfg[ defaults ] 就可以不列出

13 of 22

ansible.cfg

可以設定一些預設行為, 不需要逐一設定在 hosts

[defaults]

# hostfile -- 主機 ip 對照

hostfile = hosts

# remote_user -- 遠端使用者名稱

remote_user = root

# private_key_file -- SSH privite key path

# host_key_checking -- 不詢問加入 ssh 金鑰

host_key_checking = False

# 設定 retry files (*.retry) 存放路徑, 預設放家目錄

# 我自己喜歡指定在目前目錄, 以免作完實驗家目錄一堆 .retry

retry_files_save_path = ./ansible-retry

# 平行處理數量, 預設是 5 個, 應該不一定會用到先記下來

# forks = 20

14 of 22

Ansible Module

  • Ansible 使用上, 會根據不同的功能呼叫不同的 Module

15 of 22

Demo / Hands on Lab

16 of 22

Playbook

Playbook

Play

Hosts

tasks

Module

Play

Hosts

tasks

Module

Play

Hosts

tasks

Module

Play

Hosts

tasks

Module

control machine

remote host

remote host

remote host

SSH

Playbook

17 of 22

Playbook

  • A script is called a playbook.
    • 包含要進行組態的主機
    • 以及順序進行的工作
    • 包含許多的 play( *.yml )
  • Ansible 針對所有的主機同時(平行)執行tasks.
    • Ansible 會等待 所有主機 task 完成之後才會進行下一個 task.
    • Ansible 按照 順序 執行 tasks.
  • Ansible playbooks 以 YAML 語法撰寫, 簡單易讀.
  • 使用 ansible-playbook 指令執行.
    • ansible-playbook
    • ansible-playbook --verbose 顯示詳細資訊
    • ansible-playbook --check 不實際執行 dry run

18 of 22

Playbook

YAML 語法

  • Yet Another Markup Language
  • Start of File
    • 以 3 個 --- 開始, 不加上去也可以
  • 註解
    • 以 # 來進行單行註解
    • #就是註解開始
  • Strings
    • 不一定要加上引號
    • 可是有的時候為了易讀性, 可以使用 單引號或是雙引號
  • Booleans
    • 使用 True 或是 Yes 都可以視為真
    • 但是還是用 True 不會混亂
  • Lists
    • They are delimited with hyphens, like this:
    • - My Fair Lady
    • - Oklahoma
    • # inline 格式list
    • [My Fair Lady, Oklahoma, The Pirates of Penzance]
  • Dictionaries
    • They look like this:
    • address: 742 Evergreen Terrace
    • state: North Takoma
    • # inline 格式
    • {address: 742 Evergreen Terrace, city: Springfield, state: North Takoma}

19 of 22

Plays

  • 每一個 play 包含
    • A set of hosts to configure 目標主機
    • A list of tasks to be executed on those hosts. 工作內容
  • 常用的設定
    • name - 執行的 play 或是 task 名稱.
    • sudo - 要不要執行 sudo.
    • vars - 變數設定.
  • Tasks
    • 要在遠端主機執行的工作.
      • Modules

20 of 22

Demo / Hands on Lab

21 of 22

Reference

  • OReilly - Ansible Up & Running
  • Blog 文章 sakananote
  • 整理的心智圖 Mindmap
    • 心智圖推薦使用 freemind

22 of 22

Thanks You