SlideShare a Scribd company logo
1 of 14
Download to read offline
ANSIBLE
Written by Samuel Jiang
AGENDA
Ansible Introduction
Quick Start
Ansible hosts and vars
Ansible vault
Ansible playbook
Ansible roles
ANSIBLE INTRODUCTION
⾃自動化部署管理理⼯工具
GPL 3.0 授權
現由redhat管理理
MANAGEMENT COMPARE
Ansible Puppet Chef
Script Language YAML Custom DSL based on Ruby Ruby
Infrastructure
Controller machine applies
configuration on nodes via SSH
Puppet Master synchronizes
configuration on Puppet Nodes
Chef Workstations push configuration
to Chef Server, from which the Chef
Nodes will be updated
Requires specialized software
for nodes
No Yes Yes
Provides centralized point of
control
No. Any computer can be a controller Yes, via Puppet Master Yes, via Chef Server
QUICK START
sudo apt-get install ansible
vim hosts
ansible -i hosts -m ping all
//hosts

[server_groups]

host

ansible_ssh_host=//host_ip 

ansible_user=//username 

ansible_ssh_pass=//password
ANSIBLE TERMS
Inventory: 管理理的Server資訊統整檔案,要操作的Server相關資訊皆寫入其中
Playbook: Ansible 所要執⾏行行操作的檔案,以Yaml 格式撰寫
Task: 為⼀一個要執⾏行行的任務,通常有通⽤用的module 來來撰寫所執⾏行行的任務
Module: 為⼀一種汎⽤用指令包裝成模組供利利⽤用,like: apt-get install,官⽅方已提供多種模組
Role: 為了了有效管理理不同執⾏行行任務並達到共⽤用分層所制訂⼀一⽬目錄架構
Var: 在各playbook 或外部需提供額外之變數利利⽤用,與playbook相關聯聯
Facts: 與主機系統之變數,與每⼀一host或groups相關聯聯
Handlers: 提供像是reboot,reload 相關只需執⾏行行⼀一次之觸發動作撰寫
ANSIBLE STRUCTURE
.
├── group_vars // hosts 群組的共同資訊變數放入位置資料夾
│ ├── linux_vm // 可建置與群組名相同的資料,會讀裡⾯面的main.yml的資訊
│ └── windows_vm.yml. // 或直接建立與群組名相同的yml檔
├── hosts // 管理理統整server 列列表資訊的檔案
├── host_vars // hosts 資訊變數放入位⾄至資料夾,概念念與group_vars相同
├── playbook.yml // 所要執⾏行行⼀一系列列執⾏行行操作定義的playbook
└── roles // 針對不同類型的執⾏行行⼀一系列列操作以group⽅方式作管理理並放置於此資料夾
└── webserver //roles 名稱,在執⾏行行時此group的執⾏行行操作,會讀取資料夾下對應位置之資料
ANSIBLE CONTROL WINDOWS
Controller Machine:

安裝Winrm 相關Library
sudo apt-get install python-pip git libffi-dev libssl-dev -y
pip install ansible pywinrm
Node:

確保powershell 版本在3.0以上

https://docs.ansible.com/ansible/latest/intro_windows.html

如需搬動較⼤大型的檔案,調整powershell 內存memory ⼤大⼩小

Set-Item -Path WSMan:localhostShellMaxMemoryPerShellMB -value 2048





ANSIBLE HOSTS AND FACTS
//hosts

[server_groups]

server_host ansible_ssh_host= //server_ip 

ansible_user= //ssh_username

ansible_ssh_pass= //ssh_password

ansible_become_pass= //sudo_password

//除了了官⽅方提供的參參數,也可以⾃自定義變數進去,視為Facts

ANSIBLEVAULT
Ansible-vault entrypt file_name
Type Password
Type Password again
Done ….. not yet
ansible - -ask-vault-pass //執⾏行行ansible 時要求輸入密碼

ansible - -vault-password-file password_file //執⾏行行時⼀一併帶入密碼
檔案
ANSIBLE PLAYBOOK YAML
Playbook以Yaml 撰寫

都以開頭- - -
全以空⽩白間隔來來判斷格式正確與否
(Tab 會Error)

可⽤用ansible-lint 來來檢查yaml

撰寫是否正確

pip install ansible-lint
執⾏行行⽅方式

ansible-playbook -i host playbook.yml
ANSIBLE PLAYBOOK EXAMPLE
- - -

- name: test //此次playbook 執⾏行行描述

hosts: all //指定執⾏行行的host

vars: //變數宣告位置,與facts 不⼀一樣,此變數是所有host 共⽤用⼀一個,需注意

- hello: hello

tasks: //要執⾏行行的tasks 動作宣告,由上⽽而下依序執⾏行行

- debug: var=hello

notify:

- call in every action

handlers: //要執⾏行行的handler 動作宣告,觸發條件為task changed,同樣由上⽽而下依序
執⾏行行

- name: call in every action

debug: msg=“call in every action, but execute only one time”

ANSIBLE ROLE STRUCTURE
Roles
├── common
│ ├── defaults //宣告變數默認值,隨時都可被覆蓋
│ ├── files //檔案放置位置,只要在copy/script module寫對應檔名
即會抓取檔案
│ ├── handlers //宣告handlers,會先讀取main.yml
│ ├── meta //宣告與其他roles的dependencies,會先讀取main.yml
│ ├── tasks //宣告tasks,會先讀取main.yml
│ ├── templates //template放置位置,只要在template module寫對
應檔名即會抓取檔案
│ └── vars //宣告定義變數,會先讀取main.yml
REFERENCE
Ansible-first-book

https://www.gitbook.com/book/ansible-book/ansible-first-book/
details
DigitalOcean Tutorials

https://www.digitalocean.com/community/tutorials?q=ansible

More Related Content

What's hot

Docker tutorial
Docker tutorialDocker tutorial
Docker tutorialazole Lai
 
ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索Will Huang
 
Laravel - 系統全攻略(續)
Laravel - 系統全攻略(續)Laravel - 系統全攻略(續)
Laravel - 系統全攻略(續)Vincent Chi
 
Deployment with Capistrano
Deployment with CapistranoDeployment with Capistrano
Deployment with Capistrano旭 張
 
Package安裝與使用
Package安裝與使用Package安裝與使用
Package安裝與使用Shengyou Fan
 
AWS EC2 for beginner
AWS EC2 for beginnerAWS EC2 for beginner
AWS EC2 for beginnerazole Lai
 
Docker 基礎介紹與實戰
Docker 基礎介紹與實戰Docker 基礎介紹與實戰
Docker 基礎介紹與實戰Bo-Yi Wu
 
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用Shengyou Fan
 
Angular 4 網站開發最佳實務 (Modern Web 2017)
Angular 4 網站開發最佳實務 (Modern Web 2017)Angular 4 網站開發最佳實務 (Modern Web 2017)
Angular 4 網站開發最佳實務 (Modern Web 2017)Will Huang
 
COSCUP 2016 Laravel 部署工作坊 - 部署指南
COSCUP 2016 Laravel 部署工作坊 - 部署指南COSCUP 2016 Laravel 部署工作坊 - 部署指南
COSCUP 2016 Laravel 部署工作坊 - 部署指南Shengyou Fan
 
Software Engineer Talk
Software Engineer TalkSoftware Engineer Talk
Software Engineer TalkLarry Cai
 
快快樂樂學 Angular 2 開發框架
快快樂樂學 Angular 2 開發框架快快樂樂學 Angular 2 開發框架
快快樂樂學 Angular 2 開發框架Will Huang
 
Frontend Devops at Cloudinsight
Frontend Devops at CloudinsightFrontend Devops at Cloudinsight
Frontend Devops at CloudinsightYan Wang
 
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 TaiwanAzure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 TaiwanWill Huang
 
DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略Will Huang
 
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)Will Huang
 

What's hot (20)

Docker tutorial
Docker tutorialDocker tutorial
Docker tutorial
 
ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索
 
Laravel - 系統全攻略(續)
Laravel - 系統全攻略(續)Laravel - 系統全攻略(續)
Laravel - 系統全攻略(續)
 
Deployment with Capistrano
Deployment with CapistranoDeployment with Capistrano
Deployment with Capistrano
 
Docker
DockerDocker
Docker
 
Package安裝與使用
Package安裝與使用Package安裝與使用
Package安裝與使用
 
AWS EC2 for beginner
AWS EC2 for beginnerAWS EC2 for beginner
AWS EC2 for beginner
 
Docker 基礎介紹與實戰
Docker 基礎介紹與實戰Docker 基礎介紹與實戰
Docker 基礎介紹與實戰
 
Docker應用
Docker應用Docker應用
Docker應用
 
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
 
Angular 4 網站開發最佳實務 (Modern Web 2017)
Angular 4 網站開發最佳實務 (Modern Web 2017)Angular 4 網站開發最佳實務 (Modern Web 2017)
Angular 4 網站開發最佳實務 (Modern Web 2017)
 
Vagrant教學
Vagrant教學Vagrant教學
Vagrant教學
 
COSCUP 2016 Laravel 部署工作坊 - 部署指南
COSCUP 2016 Laravel 部署工作坊 - 部署指南COSCUP 2016 Laravel 部署工作坊 - 部署指南
COSCUP 2016 Laravel 部署工作坊 - 部署指南
 
Software Engineer Talk
Software Engineer TalkSoftware Engineer Talk
Software Engineer Talk
 
快快樂樂學 Angular 2 開發框架
快快樂樂學 Angular 2 開發框架快快樂樂學 Angular 2 開發框架
快快樂樂學 Angular 2 開發框架
 
Frontend Devops at Cloudinsight
Frontend Devops at CloudinsightFrontend Devops at Cloudinsight
Frontend Devops at Cloudinsight
 
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 TaiwanAzure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
 
DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略
 
Docker Build
Docker BuildDocker Build
Docker Build
 
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
 

Similar to Ansible introduction

Ruby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuRuby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuMarsZ Chen
 
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...Laird Cheng
 
Appserv setup
Appserv setupAppserv setup
Appserv setupHt Wang
 
0118 Windows Server 2008 的伺服器核心 (Server Core)
0118 Windows Server 2008 的伺服器核心 (Server Core)0118 Windows Server 2008 的伺服器核心 (Server Core)
0118 Windows Server 2008 的伺服器核心 (Server Core)Timothy Chen
 
Linux集群应用实战 通过lvs+keepalived搭建高可用的负载均衡集群系统(第二讲)
Linux集群应用实战 通过lvs+keepalived搭建高可用的负载均衡集群系统(第二讲)Linux集群应用实战 通过lvs+keepalived搭建高可用的负载均衡集群系统(第二讲)
Linux集群应用实战 通过lvs+keepalived搭建高可用的负载均衡集群系统(第二讲)hik_lhz
 
Cent os 安装 subversion
Cent os 安装 subversionCent os 安装 subversion
Cent os 安装 subversionYUCHENG HU
 
Puppet安装总结
Puppet安装总结Puppet安装总结
Puppet安装总结Yiwei Ma
 
Track2 -何斌--openstack 在京东的应用实践
Track2 -何斌--openstack 在京东的应用实践Track2 -何斌--openstack 在京东的应用实践
Track2 -何斌--openstack 在京东的应用实践OpenCity Community
 
GNU Build System
GNU Build SystemGNU Build System
GNU Build Systemimacat .
 
自动化运维管理
自动化运维管理自动化运维管理
自动化运维管理frankwsj
 
Sery lvs+keepalived
Sery lvs+keepalivedSery lvs+keepalived
Sery lvs+keepalivedcolderboy17
 
Subversion备份服务器的创建
Subversion备份服务器的创建Subversion备份服务器的创建
Subversion备份服务器的创建shadowfalao
 
The Application of OpenStack in 360buy, by Bin He
The Application of OpenStack in 360buy, by Bin HeThe Application of OpenStack in 360buy, by Bin He
The Application of OpenStack in 360buy, by Bin HeHui Cheng
 
2, installation
2, installation2, installation
2, installationted-xu
 
台北市研習_LAMP_20140815
台北市研習_LAMP_20140815台北市研習_LAMP_20140815
台北市研習_LAMP_20140815fweng322
 
Open whisk makes your cloud serverless 20180320
Open whisk makes your cloud serverless 20180320Open whisk makes your cloud serverless 20180320
Open whisk makes your cloud serverless 20180320Ying Chun Guo
 
NodeJS快速服务端开发 朝沐金风 Shanghai
NodeJS快速服务端开发 朝沐金风 ShanghaiNodeJS快速服务端开发 朝沐金风 Shanghai
NodeJS快速服务端开发 朝沐金风 ShanghaiJackson Tian
 

Similar to Ansible introduction (20)

Ruby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuRuby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for Ubuntu
 
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...
 
Ansible 101
Ansible 101Ansible 101
Ansible 101
 
Appserv setup
Appserv setupAppserv setup
Appserv setup
 
1~60
1~601~60
1~60
 
0118 Windows Server 2008 的伺服器核心 (Server Core)
0118 Windows Server 2008 的伺服器核心 (Server Core)0118 Windows Server 2008 的伺服器核心 (Server Core)
0118 Windows Server 2008 的伺服器核心 (Server Core)
 
Linux集群应用实战 通过lvs+keepalived搭建高可用的负载均衡集群系统(第二讲)
Linux集群应用实战 通过lvs+keepalived搭建高可用的负载均衡集群系统(第二讲)Linux集群应用实战 通过lvs+keepalived搭建高可用的负载均衡集群系统(第二讲)
Linux集群应用实战 通过lvs+keepalived搭建高可用的负载均衡集群系统(第二讲)
 
Cent os 安装 subversion
Cent os 安装 subversionCent os 安装 subversion
Cent os 安装 subversion
 
Puppet安装总结
Puppet安装总结Puppet安装总结
Puppet安装总结
 
Track2 -何斌--openstack 在京东的应用实践
Track2 -何斌--openstack 在京东的应用实践Track2 -何斌--openstack 在京东的应用实践
Track2 -何斌--openstack 在京东的应用实践
 
GNU Build System
GNU Build SystemGNU Build System
GNU Build System
 
自动化运维管理
自动化运维管理自动化运维管理
自动化运维管理
 
Sery lvs+keepalived
Sery lvs+keepalivedSery lvs+keepalived
Sery lvs+keepalived
 
Subversion备份服务器的创建
Subversion备份服务器的创建Subversion备份服务器的创建
Subversion备份服务器的创建
 
The Application of OpenStack in 360buy, by Bin He
The Application of OpenStack in 360buy, by Bin HeThe Application of OpenStack in 360buy, by Bin He
The Application of OpenStack in 360buy, by Bin He
 
运维自动化
运维自动化运维自动化
运维自动化
 
2, installation
2, installation2, installation
2, installation
 
台北市研習_LAMP_20140815
台北市研習_LAMP_20140815台北市研習_LAMP_20140815
台北市研習_LAMP_20140815
 
Open whisk makes your cloud serverless 20180320
Open whisk makes your cloud serverless 20180320Open whisk makes your cloud serverless 20180320
Open whisk makes your cloud serverless 20180320
 
NodeJS快速服务端开发 朝沐金风 Shanghai
NodeJS快速服务端开发 朝沐金风 ShanghaiNodeJS快速服务端开发 朝沐金风 Shanghai
NodeJS快速服务端开发 朝沐金风 Shanghai
 

Ansible introduction

  • 2. AGENDA Ansible Introduction Quick Start Ansible hosts and vars Ansible vault Ansible playbook Ansible roles
  • 4. MANAGEMENT COMPARE Ansible Puppet Chef Script Language YAML Custom DSL based on Ruby Ruby Infrastructure Controller machine applies configuration on nodes via SSH Puppet Master synchronizes configuration on Puppet Nodes Chef Workstations push configuration to Chef Server, from which the Chef Nodes will be updated Requires specialized software for nodes No Yes Yes Provides centralized point of control No. Any computer can be a controller Yes, via Puppet Master Yes, via Chef Server
  • 5. QUICK START sudo apt-get install ansible vim hosts ansible -i hosts -m ping all //hosts
 [server_groups]
 host
 ansible_ssh_host=//host_ip 
 ansible_user=//username 
 ansible_ssh_pass=//password
  • 6. ANSIBLE TERMS Inventory: 管理理的Server資訊統整檔案,要操作的Server相關資訊皆寫入其中 Playbook: Ansible 所要執⾏行行操作的檔案,以Yaml 格式撰寫 Task: 為⼀一個要執⾏行行的任務,通常有通⽤用的module 來來撰寫所執⾏行行的任務 Module: 為⼀一種汎⽤用指令包裝成模組供利利⽤用,like: apt-get install,官⽅方已提供多種模組 Role: 為了了有效管理理不同執⾏行行任務並達到共⽤用分層所制訂⼀一⽬目錄架構 Var: 在各playbook 或外部需提供額外之變數利利⽤用,與playbook相關聯聯 Facts: 與主機系統之變數,與每⼀一host或groups相關聯聯 Handlers: 提供像是reboot,reload 相關只需執⾏行行⼀一次之觸發動作撰寫
  • 7. ANSIBLE STRUCTURE . ├── group_vars // hosts 群組的共同資訊變數放入位置資料夾 │ ├── linux_vm // 可建置與群組名相同的資料,會讀裡⾯面的main.yml的資訊 │ └── windows_vm.yml. // 或直接建立與群組名相同的yml檔 ├── hosts // 管理理統整server 列列表資訊的檔案 ├── host_vars // hosts 資訊變數放入位⾄至資料夾,概念念與group_vars相同 ├── playbook.yml // 所要執⾏行行⼀一系列列執⾏行行操作定義的playbook └── roles // 針對不同類型的執⾏行行⼀一系列列操作以group⽅方式作管理理並放置於此資料夾 └── webserver //roles 名稱,在執⾏行行時此group的執⾏行行操作,會讀取資料夾下對應位置之資料
  • 8. ANSIBLE CONTROL WINDOWS Controller Machine:
 安裝Winrm 相關Library sudo apt-get install python-pip git libffi-dev libssl-dev -y pip install ansible pywinrm Node:
 確保powershell 版本在3.0以上
 https://docs.ansible.com/ansible/latest/intro_windows.html
 如需搬動較⼤大型的檔案,調整powershell 內存memory ⼤大⼩小
 Set-Item -Path WSMan:localhostShellMaxMemoryPerShellMB -value 2048
 
 

  • 9. ANSIBLE HOSTS AND FACTS //hosts
 [server_groups]
 server_host ansible_ssh_host= //server_ip 
 ansible_user= //ssh_username
 ansible_ssh_pass= //ssh_password
 ansible_become_pass= //sudo_password
 //除了了官⽅方提供的參參數,也可以⾃自定義變數進去,視為Facts

  • 10. ANSIBLEVAULT Ansible-vault entrypt file_name Type Password Type Password again Done ….. not yet ansible - -ask-vault-pass //執⾏行行ansible 時要求輸入密碼
 ansible - -vault-password-file password_file //執⾏行行時⼀一併帶入密碼 檔案
  • 11. ANSIBLE PLAYBOOK YAML Playbook以Yaml 撰寫
 都以開頭- - - 全以空⽩白間隔來來判斷格式正確與否 (Tab 會Error)
 可⽤用ansible-lint 來來檢查yaml
 撰寫是否正確
 pip install ansible-lint 執⾏行行⽅方式
 ansible-playbook -i host playbook.yml
  • 12. ANSIBLE PLAYBOOK EXAMPLE - - -
 - name: test //此次playbook 執⾏行行描述
 hosts: all //指定執⾏行行的host
 vars: //變數宣告位置,與facts 不⼀一樣,此變數是所有host 共⽤用⼀一個,需注意
 - hello: hello
 tasks: //要執⾏行行的tasks 動作宣告,由上⽽而下依序執⾏行行
 - debug: var=hello
 notify:
 - call in every action
 handlers: //要執⾏行行的handler 動作宣告,觸發條件為task changed,同樣由上⽽而下依序 執⾏行行
 - name: call in every action
 debug: msg=“call in every action, but execute only one time”

  • 13. ANSIBLE ROLE STRUCTURE Roles ├── common │ ├── defaults //宣告變數默認值,隨時都可被覆蓋 │ ├── files //檔案放置位置,只要在copy/script module寫對應檔名 即會抓取檔案 │ ├── handlers //宣告handlers,會先讀取main.yml │ ├── meta //宣告與其他roles的dependencies,會先讀取main.yml │ ├── tasks //宣告tasks,會先讀取main.yml │ ├── templates //template放置位置,只要在template module寫對 應檔名即會抓取檔案 │ └── vars //宣告定義變數,會先讀取main.yml