Ansible簡述
Absible 使用 模塊(Modules)來定義配置任務。模塊可以用標準腳本語言(Python,Bash,Ruby,等等)編寫,這是一個很好的做法,使每個模塊冪等。Ansible 劇本(YAML語言編寫)角色映射到 節點/虛擬機上,以對這些節點/虛擬機 進行編排有序的執行。
Ansible 支持 ad-hoc 任務:管理任務(不必冪等)以在多個節點/虛擬機上執行。Ansible具有無代理架構:你不需要在配置的節點/虛擬機上安裝Ansible(然而,這些節點必須安裝 python 環境),Ansible 通過SSH或者通過 PowerShell(Windows 機器)從單個控制機器上配置管理節點/虛擬機,並且能夠支持並行配置多個節點。
Ansible 可以用來配置類 unix 系統或 Windows 節點/虛擬機。
Ansible 使用 Python 語言編寫。許可:GNU General Public License.
Ansible playbook 示例:
- name: Install nginx
yum: name=nginx state=present- name: restart nginx
service: name=nginx state=restarted enabled=yes
什麼是Ansible
Ansible 是 DevOps 項目基礎支撐工具之一,是第一款實現讀/ 寫跨平臺 的“ Infrastructure- as- code” 工具,從系統管理者到開發者,都可使用Ansible自動化部署並維護整個應用的生命週期,實現持續交付。
Ansible 軟件及公司
在維基百科裏列出了二十多個, 其中Puppet、Chef和Salt,以及CFEngine、Vagrant和NixOS,大家都可能耳熟能詳 了。不過後起之秀Ansible( http:// www. ansible. com/)的人氣更高,已經是當今最常用的管理基礎架構的開源管理工具之一。
運維自動化的發展
- 純手工階段
- 腳本階段
- 工具階段
Ansible:"Ansible is Simple IT Automation"
- 一款簡單的自動化工具
- 無代理(無需再所要管理的系統上安裝任何軟件)
- Github上最熱門的開源自動化工具之一,可免費使用
Ansible提供
- 應用部署
- 配置管理
- 任務自動化
目標用戶:
- 運維&開發
爲什麼選擇Ansible
特點
- 無客戶端
- 簡單,易懂
- 擴展性強,可管理從幾十臺到數千臺節點
- ssh連接,安全
- 強大社區,大量module,role拿來即用
- 冪等性
Ansible工作原理
Ansible核心構成
- Inventory:定義Ansible被管理主機的清單
- Modules:包括Ansible自帶的核心模塊及自定義模塊
- Playbooks:劇本,定義Ansible多任務配置文件,由Ansible自動執行
- API:python接口,提供二次開發及系統整合能力
- Plugins:完成模塊功能的補充,包括連接插件、郵件插件等
Ansible安裝
yum方式
yum install epel-release
yum install ansible -y
apt-get方式
apt-get install software-properties-common
apt-add-repository ppa:ansible/ansible
apt-get update
apt-get install ansible
pip方式
pip install ansible
文件安裝
- 可執行文件:/usr/bin/ansible*
- 配置文件目錄:/etc/ansible/
- ansible-config
- hosts
- roles/
- Python lib文件:usr/lib/python2.7/site-packages/ansible
- Help文檔
重要
- 在/etc/ansible/ansible-config裏關閉host_key_checking
Inventory
- Ansible 通過讀取默認的主機清單配置/etc/ansible/hosts,可以同時連接到多個遠程主機上執行任務,也可以同時使用多個清單文件,甚至可以動態地或者從雲資源中拉取清單。
Ansible ad-hoc
- ad-hoc是什麼
完成實時的。一次性的,簡單的工作- ad-hoc 語法規則
常用參數
- -i 指定inventory文件
- -m 指定模塊
- -a 參數命令
- -u 指定訪問用戶
- -k 輸入密碼
- -K 輸入sudo密碼
- -f 指定並行數量(默認5)
Ansible常用模塊
- ping:用於檢測遠程主機是否存活
- shell:在遠程主機上執行shell命令
- script:在遠端機器執行本地腳本
- yum/apt:用於安裝軟件包
- service:用於管理服務
- file:用於配置文件屬性
- copy:複製文件到遠程主機
- setup :查看遠程主機的基本信息
ping
檢查目標主機手否存活
示例:
- ansible -i yard all -m ping
shell
在目標主機執行shell命令
- chdir:運行shell之前cd到某個目錄
示例:
- ansible all -m shell -a "cat /etc/centos-release"
- ansible all -m shell -a "cat centos-release chdir=/etc"
script
在目標機器上執行本地腳本
- chdir:先進入目標文件夾再執行腳本
示例:
ansible db -m script -a "/tmp/add_env.sh"
ansible db -m script -a "/root/test.py chdir=/var/www/"
apt/yum
apt、yum 模塊分別用於管理Ubuntu系列和RedHat系列系統軟件包
- name:軟件包名
- state:軟件包的狀態(present/installed/absent/removed)
示例
- 確保httpd包已經安裝
ansible webservers -m apt -a "name=httpd state=present"- 確保安裝包到一個特定的版本
ansible webservers -m apt -a "name=httpd-1.5 state=present"- 確保一個軟件包沒有被安裝
ansible webservers -m apt -a "name=httpd state=absent"
service
管理目標機器服務的狀態
- name:必選項,服務名稱
- state:對當前服務執行啓動,停止、重啓、重新加載等操作
(started,stopped,restarted,reloaded)- enabled:是否開機啓動 yes|no
- runlevel:運行級別
- arguments:給命令行提供一些選項
示例:
- ansible test -m service -a "name=httpd state=started enabled=yes"
- ansible test -m service -a "name=network state=restarted args=eth0"
- ansible test -m service -a "name=httpd state=stopped"
copy
將文件從ansible管理機拷貝到目標機器
- src:源文件
- dest:目標路徑
- backup:覆蓋之前,是否備份原文件
- owner:設定文件/目標的屬主
- group:設定文件/目標的屬組
- mode:設定文件/目標的權限
示例:
- ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=644"
- ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes"
file
操作目標機器文件屬性,新建/刪除文件,文件夾及鏈接文件
- group:定義文件/目錄的屬組
- owner:定義文件/目錄的屬主
- mode:定義文件/目錄的權限
- path:必選項,定義文件/目錄的路徑
- state:定義文件狀態(directory/link/absent/touch/hard)
示例:
- ansible test -m file -a "src=/etc/fstab dest=/tmp/fstab state=link"
- ansible test -m file -a "path=/tmp/ansible mode=755 group=test owner=test"
- ansible test -m file -a "path=/tmp/test state=directory"
setup
蒐集系統信息
- 蒐集主機的所有系統信息
ansible all -m setup- 蒐集系統信息並以主機名爲文件名分別保存在/tmp/facts 目錄
ansible all -m setup --tree /tmp/facts- 蒐集和內存相關的信息
ansible all -m setup -a "filter=ansible_*_mb"- 蒐集網卡信息
ansible all -m setup -a "filter=ansible_eth[0-2]"
模塊幫助
- ansible-doc -l #獲取所有模塊列表
- ansible-doc module_name #獲取模塊的詳細信息
- 在線幫助:
http://docs.ansible.com/ansible/latest/modules_by_category.html
Playbook
- 以YAML文件作爲載體
- 描述多個任務的集合
- 使目標主機按照既定順序執行任務以達到期望狀態
- 通過ansible-playbook命令執行
什麼是YAML語言
一種人性化的數據格式定義語言
YAML:YAML Ain't a Markup Language
- 結構感強,可讀性好
- 通用的數據串行化格式
這是一個yaml
---
#一位職工記錄
name: David
job: Developer
skill: Elite
employed:True
foods:
-Apple
-Orange
Languages:
ruby:Elite
python:Elite
語法要求:
- 以‘---’作爲起始句
- 大小寫敏感
- 使用縮進表示層級關係
- 縮進時不允許使用Tab鍵,只允許使用空格
- 縮進的空格數目不重要,相同層級的元素左側對齊即可
數據類型:
- 對象:鍵值對的集合,又稱爲映射(mapping)/哈希(hashes)/字典(dictionary)
- 數組:一組按次序排列的值,又稱爲序列(sequence)/列表(list)
- 純量(scalars):單個的、不可再分的值
對象
name: David
job: Developer
{''name":"David","job":"Developer"}
數組
- apple
- orange
["apple",'orange']
符合結構
students:
- name: Lucy
age:15
- name: Lily
age: 13
{students:[{"name":"Lucy","age":15,{"name":"Lily","age":13}}]}
布爾值
create_key: yes/no
gather_facts: true/False
sudoer: T/F
再看yaml
YAML語法檢查
Pyhton
Python -c 'import yaml,sys;print yaml.load(sys.stdin)'< test.yamlYAML Lint
http://www.yamllint.com
Ansible命令工具
Ansible-playbook test.yaml --syntax-check
Play
參考鏈接:http://blog.51cto.com/weiweidefeng/1895261
未完待續,持續更新