之前寫過一篇使用Ansible配置LAMP環境的Blog,這幾天一直在閱讀Ansible相關的書籍,如Ansible for DevOpsAnsible: Up and RunningAnsible Playbook Essentials。在準備寫一篇詳細的Ansible使用說明時,驟然發現之前寫的Try to Use Ansible in CentOS 7草稿並未上傳到博客。故而,在對此草稿進行修改後,將其公佈。

Ansible Introducation


Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates. — About Ansible


Site Link
Site https://www.ansible.com/
GitHub https://github.com/ansible/ansible
Documentation http://docs.ansible.com/ansible/
Blog https://www.ansible.com/blog
Twitter https://twitter.com/ansible

Red Hat於2015.10.16宣佈收購Ansible,詳情見 FAQ: Red Hat Acquires Ansible

Ansible使用Python開發(2.6或2.7),控制臺(Control Machine)通過SSH管理遠程主機(Managed Node),可同時對多臺主機進行並行(parallel)操作。


Type Explanation
Control Machine 控制主機,用於管理 Managed Node
Managed Node 被管理的主機


站在Managed Node的角度,二者的顯著差異是: * Ansible屬於 push-based * 由Control Machine推送 * Puppet屬於 pull-based * 由Managed Node通過安裝的agent進行週期性檢查,從Control Machine拉取配置信息到本地

( 出處 「Ansible Up and Running」 Page5 )

Ansible具有冪等性(idempotence),即相同命令運行多次,結果均相同。在配置工具(Configuration Tool)領域中有2種思想: * idempotence(冪等) * 每一次的運行結果都相同 * Convergence(收斂、趨同) * 運行多次,逐步接近預期效果

關於Ansible的idempotence,其開發者Michael DeHaan寫過一篇 Idempotence, convergence, and other silly fancy words we use too often,有興趣者可自行查看。 ( 出處 「Ansible Up and Running」 Page7

Ansible Installation

Ansible官方文檔 Installation,介紹了Ansible的獲取途徑、運行要求、安裝方式。

Get Ansible

Ansible的代碼公佈在GitHub上,其Project地址是 https://github.com/ansible/ansible, Tarball頁面 http://releases.ansible.com/ansible/

也可通過EPEL Repository安裝,當前版本是1.9.4



Control Machine Requirements

  • 須安裝Python 2.6 或 2.7

Please note that some modules and plugins have additional requirements, for modules these need to be satisfied on the ‘target’ machine and should be listed in the module specific docs.

Managed Node Requirements

  • 仍須安裝Python,如果版本低於2.5,須額外安裝python-simplejson
  • 如果SELinux是被啓動的,則須安裝libselinux-python以支持copy、file、template等功能,可通過yum模塊在node中安裝

Ansible暫不支持Python 3,須額外安裝Python 2.6或2.7,並在inventory中添加ansible_python_interpreter參數

Ansible只需安裝在Control Machine即可,可以是個人筆電。

Insatllation Approach

Via Yum

  • 配置EPEL Repository
  • 執行sudo yum -y install ansible --enablerepo=epel即可

From Source

從GitHub下載Ansible源碼,可通過make rpm製作rpm包安裝。須先安裝rpm-buildmakeasciidocgitpython-setuptoolspython2-devel


$ git clone git://github.com/ansible/ansible.git --recursive
$ cd ./ansible
$ make rpm
$ sudo rpm -Uvh ./rpm-build/ansible-*.noarch.rpm


$ sudo yum install gcc make rpm-build asciidoc git python python-devel python-setuptools python2-crypto libevent-devel

$ git clone git://github.com/ansible/ansible.git --recursive
$ cd ./ansible
$ make rpm
$ sudo rpm -Uvh ./rpm-build/ansible-*.noarch.rpm


[flying@lemp ~]$ ansible --version
ansible 2.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

[flying@lemp ~]$ rpm -qi ansible
Name        : ansible
Version     : 2.1.0
Release     : 0.git201603261154.4db7e12.devel.el7.centos
Architecture: noarch
Install Date: Sun 27 Mar 2016 10:56:43 AM CST
Group       : Development/Libraries
Size        : 14104355
License     : GPLv3
Signature   : (none)
Source RPM  : ansible-2.1.0-0.git201603261154.4db7e12.devel.el7.centos.src.rpm
Build Date  : Sun 27 Mar 2016 10:56:01 AM CST
Build Host  : lemp
Relocations : (not relocatable)
URL         : http://www.ansible.com
Summary     : SSH-based application deployment, configuration management, and IT orchestration platform
Description :

Ansible is a radically simple model-driven configuration management,
multi-node deployment, and orchestration engine. Ansible works
over SSH and does not require any software or daemons to be installed
on remote nodes. Extension modules can be written in any language and
are transferred to managed machines automatically.
[flying@lemp ~]$

Dependent Package

如果想正常使用Ansible,還須安裝一些必要的安裝包,以下安裝包有部分與 From Source 部分重複。

yum install -y PyYAML libtomcrypt libtommath libyaml sshpass libevent-devel python-devel python-babel python-backports python-backports-ssl_match_hostname python-ecdsa python-httplib2 python-jinja2 python-keyczar python-markupsafe python-paramiko python-pyasn1 python-setuptools python-six python2-crypto libselinux-python

#如果managed node的Python版本低於2.5
yum install -y python-simplejson

Configuration Files

Ansible官方文檔 Configuration file 指出Ansible配置文件有4個

  • ANSIBLE_CONFIG (an environment variable) 環境變量
  • ansible.cfg (in the current directory) 當前目錄中
  • .ansible.cfg (in the home directory) 用戶家目錄中
  • /etc/ansible/ansible.cfg

使用rpm -qc命令查詢

[flying@lemp ~]$ rpm -qc ansible
[flying@lemp ~]$

Relevent Command

Ansible提供如下命令 * ansible: run a task on a target host(s) 在目標主機運行任務 * ansible-doc: show documentation on Ansible modules 查看Ansible相關模塊的幫助信息 * ansible-playbook: run an ansible playbook 運行playbook * ansible-vault: manage encrypted ansible vars files (YAML). 管理加密後的YAML文件 * ansible-console * ansible-galaxy: manage roles using galaxy.ansible.com 創建role * ansible-pull: pull playbooks from VCS server and run them using this machine as the target

[flying@lemp ~]$ rpm -ql ansible | grep -Ei '/usr/bin'
[flying@lemp ~]$

Ansible Component

Ansible通過playbook進行配置、部署工作,具體功能由各module實現。爲提升代碼的複用(reuse)性,可使用role組織playbook,而role的創建可通過Ansible Galaxy實現。

關於playbook,中文被翻譯爲劇本,但個人認爲 藍圖任務清單 更爲形象、更易於理解。

Ansible Module

moduleAnsible中佔據重要地位,目前有如下模塊: * Cloud Modules * Clustering Modules * Commands Modules * Database Modules * Files Modules * Inventory Modules * Messaging Modules * Monitoring Modules * Network Modules * Notification Modules * Packaging Modules * Source Control Modules * System Modules * Utilities Modules * Web Infrastructure Modules * Windows Modules

所有模塊 All Modules列表。


How to Learn

要想熟練使用並掌握Ansible,一個必要條件是能熟練使用GNU/Linux,以下方面須瞭解: * 文件系統、文件權限 * 用戶管理 * 軟件包管理(rpm、yum、編譯) * bash命令 * 環境變量、系統服務(service或systemd) * Shell腳本 * 關係型數據庫(MySQL或MariaDB)

如何高效地學習Ansible,個人建議 * 閱讀官方文檔(系統、權威) * 閱讀其他人寫的playbook,GitHub上有很多 * 多練習

推薦使用Vagrant搭建測試環境,如何使用參見本人博客 vagrant部分。




Change Logs

  • 2016.03.27 12:31 Sun Asia/Beijing
    • 初稿修改完成
  • 2016.03.28 10:58 Mon Asia/Beijing
    • 添加Bibliography

  • Note Time: 2016.03.27 12:31 Sun
  • Note Location: Asia/Beijing
  • Writer: lempstacker