Ansible Playbook应用

Playbooks 与 adhoc 相比,是一种完全不同的运用 ansible 的方式,是非常之强大的.简单来说,playbooks 是一种简单的配置管理系统与多机器部署系统的基础.与现有的其他系统有不同之处,且非常适合于复杂应用的部署.Playbooks 可用于声明配置,更强大的地方在于,在 playbooks 中可以编排有序的执行过程,甚至于做到在多组机器间,来回有序的执行特别指定的步骤.并且可以同步或异步的发起任务.我们使用 adhoc 时,主要是使用 /usr/bin/ansible 程序执行任务.而使用 playbooks 时,更多是将之放入源码控制之中,用之推送你的配置或是用于确认你的远程系统的配置是否符合配置规范.

目录结构

以下为playbook标准的目录结构,以不同级别目录层级的文件进行拆分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost playbooks]# tree .
.
├── playbooks.yml
├── roles
│ ├── common
│ │ ├── files
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── vars
│ ├── mysql
│ │ └── tasks
│ │ └── main.yml
│ ├── nginx
│ │ ├── handlers
│ │ │ └── main.yml
│ │ └── tasks
│ │ └── main.yml
│ └── php-fpm
│ └── tasks
│ └── main.yml
└── site.yml

site.yml,playbook主要配置文件,例:

1
2
3
4
5
6
7
8
---
- name: Install web
hosts: webserver
user: root
roles:
- common
- nginx
- mysql

../tasks/main.yml,任务文件,例:

1
2
3
4
---
- name: Install nginx
yum: name=nginx state=present
notify: start nginx

../handlers/main.yml,处理程序文件,例:

1
2
3
---
- name: start nginx
service: name=nginx state=started

../vars/main.yml,变量文件,例:

1
2
---
ntpserver: time.windows.com

../templates/,用于template调用,例:

1
2
3
---
- name: copy configure
template: src=nginx.conf dest=/etc/nginx/nginx.conf

../files/,其内的文件无需路径,直接引用,例:

1
2
3
---
- name: include files
- include: nginx.yml

条件与循环

如果系统为redhat则重启

1
2
3
4
tasks:
- name: reboot redhat host
command: /usr/sbin/reboot
when: ansible_os_family == "RedHat"

如果result为成功状态则重启

1
2
3
4
5
6
7
tasks:
- command: /bin/false
register: result
ignore_errors: True

- command: /usr/sbin/reboot
when: result|success

循环语句基础

1
2
3
4
5
6
7
tasks:
- name: install LNMP
yum: name={{ item }} state=present
with_items:
- nginx
- mysql-server
- php-fpm

循环还支持列表,使用with_flattened语句。 变量文件

1
2
3
---
packages_LNMP:
- [ 'nginx', 'mysql-server', 'php-fpm' ]

引用

1
2
3
4
- name: Install LNMP
yum: name={{ item }} state=present
with_flattened:
- packages_LNMP

简单示例

nginx安装的playbook示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
---
- hosts: webserver #指定主机或组
tasks:
- name: Install nginx epel
command: /usr/bin/rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- name: Install nginx
yum: name=nginx state=present
notify:
- start nginx

- name: configure nginx
template: src=letong.conf dest=/etc/nginx/conf.d/

- name: configure www dir
command: cp -r letong-web/ /www/

handlers:
- name: start nginx
service: name=nginx state=started

执行测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost playbooks]# ansible-playbook playbooks.yml 

PLAY [webserver] **************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.1.68]

TASK: [Install nginx] *********************************************************
changed: [192.168.1.68]

NOTIFIED: [start nginx] *******************************************************
changed: [192.168.1.68]

PLAY RECAP ********************************************************************
192.168.1.68 : ok=3 changed=2 unreachable=0 failed=0

访问测试

1
2
3
4
5
6
7
8
9
10
[root@localhost playbooks]# curl -I http://192.168.1.68
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Thu, 11 Dec 2015 05:00:13 GMT
Content-Type: text/html
Content-Length: 3700
Last-Modified: Wed, 22 Oct 2015 19:50:12 GMT
Connection: keep-alive
ETag: "54480a74-e74"
Accept-Ranges: bytes