在工作中有用到ansible用于自动部署和环境配置,这里整理了一份很详尽的使用指南,如果有用到的可以看看。关于使用ansible自动部署一个网站和docker化,将在下一篇文章中介绍,敬请期待。文章内容主要翻译整理自ansible官方网站推荐的
Ansible-Up and Running
一书。
1 为什么选择Ansible
来源:ansible一词源于科幻小说,是一种超光速通信设备。
Ansible is the simplest way to automate apps and IT infrastructure。
750+模块,19000+ github stars。
配置管理、应用部署等。配置管理工具有Chef, Puppet, Salt等,应用部署(将代码编译或打包然后传输到服务器部署并启动服务)工具有Capistrano,Fabric等,ansible集两者于一身,操作很简单但是功能强大。此外,还可以对多个服务器进行服务编排,支持openstack,amazon ec2, docker等。
ansible使用了一个DSL(domain-specific language)描述服务器状态。执行的文件称为playbook,文件格式为yaml。ansible简约而不简单。比起puppet的繁琐的配置和复杂语法( Puppet基础篇4-安装、配置并使用Puppet | Puppet运维自动化经验分享 ),简直是一股清流。 图2描述了ansible执行过程,执行了两个task和一个handler,先是使用了一个apt模块在web1,web2,web3上面执行了安装nginx的任务,再是用template模块拷贝了配置文件。另外,执行了一个notify nginx的handler重启了nginx。
执行流程:
1. 创建一个python脚本用于安装nginx包。
2. 拷贝python脚本到web1,web2,web3。
3. 分别在web1,web2,web3上执行该脚本。
4. 等待脚本在所有服务器上执行完毕。
5. 接着执行下一个task。
注意的几点:
- 1.在各个服务器执行脚本的过程是并行的,有个forks参数可以指定,默认是5,即一次可以在5个服务器上并行执行脚本。
- 2.要在所有的服务器都执行完第一个task后才会接着执行第二个task。(新版本新增了异步参数,一个服务器在执行完了它的任务后可以不等其他服务器执行完直接执行下一个task)。
- 3.ansible执行任务顺序与playbook中的顺序一致。
优势:
- 语法易读。yaml->json好比markdown->html。ansible的playbook可以被称之为可以执行的README。
- 远程主机不需要安装任何东西。(这有点夸大了,python2.5+(python2.4+simplejson模块)和ssh是必须的,当然这现在已经是Linux服务器标配了)
- push-based。如chef和puppet是pull-based,先将文件修改推送到中心服务器,其他服务器的agent定期拉取新的配置管理脚本并在本机执行。而在ansible是push-based的,先在中心服务器修改playbook,执行该playbook,ansible会连接到各个服务器并执行模块改变服务器状态。push-based的好处就是只在需要的时候才操控去改变目标服务器状态。如果你更倾向于pull-based模式,可以用ansible-pull。
- ansible可以很方便的scaled down,单机跟多机没有什么区别。 Simple things should be simple, complex things should be possible 。
- 很轻量级的抽象。不像puppet之类的工具,有很高的抽象,比如有package这个概念,用于不用区分服务器版本来安装模块。但是在ansible中,提供的是apt和yum模块,由你自己采用,不要再额外学一些抽象的语法,简化你的学习成本。也有人觉得这是ansible的缺点,优缺点与否,各有评判。
2 安装配置
2.1 安装
pip install ansible
依赖环境:python
2.2 配置
配置ansible.cfg文件,ansible配置文件寻找路径:
1. File specified by the ANSIBLE_CONFIG environment variable
2. ./ansible.cfg (ansible.cfg in the current directory)
3. ~/.ansible.cfg (.ansible.cfg in your home directory)
4. /etc/ansible/ansible.cfg
ansible.cfg配置文件实例
[defaults]
hostfile=/etc/ansible/hosts
private_key_file = /Users/ssj/.ssh/id_rsa_ansible
remote_user = ssj
remote_port = 22
host_key_checking = False
注意,如果是在服务器上,不要放置private key,可以通过ssh forward。
2.3 测试
简单执行命令测试是否成功 ( -vvvv可以看到更多细节信息),”changed”:false表示执行ping模块没有改变服务器状态,”ping”:pong表示模块执行后输出结果为pong。你也可以将ping模块改成command,加上参数执行指定命令。比如 ansible testserver -m command -a uptime
,当然,command是默认模块,因此还可以简化为 ansible testserver -a uptime
。
#hosts
[testserver]
127.0.0.1
#run command,-i hosts可以省去。
ssj@ssj-mbp ~/ansible $ ansible testserver -i hosts -m ping
127.0.0.1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
3 实体关系图
- playbook包含很多个play
- play中包含name,tasks,hosts,vars,handles属性。
- tasks中包含各个真正执行的module,如apt,copy,file, git, svn,service,command,notify,mysql等。具体的模块参数和使用文档在这里
4 一个例子
---
- name: Configure webserver with nginx and tls
hosts: webservers
sudo: True
vars:
key_file: /etc/nginx/ssl/nginx.key
cert_file: /etc/nginx/ssl/nginx.crt
conf_file: /etc/nginx/sites-available/default
server_name: localhost
tasks:
- name: Install nginx
apt: name=nginx update_cache=yes cache_valid_time=3600
- name: create directories for TLS certificates
file: path=/etc/nginx/ssl state=directory
- name: copy TLS key
copy: src=files/nginx.key dest={
{ key_file }} owner=root mode=0600
notify: restart nginx
- name: copy TLS certificate
copy: src=files/nginx.crt dest={
{ cert_file }}
notify: restart nginx
- name: copy nginx config file
template: src=templates/nginx.conf.j2 dest={
{ conf_file }}
notify: restart nginx
- name: enable configuration
file: dest=/etc/nginx/sites-enabled/default src={
{ conf_file }} state=link
notify: restart nginx
- name: copy index.html
template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html mode=0644
handlers:
- name: restart ng