Playbook基本使用

01.使用Playbook的好处

1.1 Playbook好处

  • 根本上说playbook和shell脚本没有任何的区别,playbook就像shell一样,也是把一堆的命令组合起来,然后加入对应条件判断等等

  • 在shell脚本中是一条一条的命令,而在playbook中是一个一个的task任务构成,每个task任务可以看做shell中的一条命令;

  • shell脚本一般只是在当前服务器上执行,而playbook则是在不止一个服务器上执行,因此playbook需要在其中指定运行该playbook的服务器名。

1.2 yaml格式

  • 缩进表示层级关系
  • 不支持制表符“tab”缩进,使用空格缩进
  • 通常开头缩进 2 个空格
  • 字符后缩进 1 个空格,如冒号、逗号等
  • “---” 表示YAML格式,一个文件的开始
  • “#”注释

02.先来认识一下Playbook(自动部署Nginx)

2.1 nginx-playbook.yml

1
[root@k8s-node2 ~]# vim nginx-playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
---
- hosts: webservers # 对 webservers组中所有机器执行
gather_facts: no # 禁用 facts,可以加快执行速度
vars: # 添加变量
hello: Ansible
remote_user: root # 以什么用户来执行

tasks: # 多个task通过 name名字区分
- name: Add repo #### 第一:拉取代码
yum_repository:
name: nginx
description: nginx repo
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no
enabled: 1

- name: Install nginx #### 第二:安装nginx
yum:
name: nginx
state: latest

- name: Copy nginx configuration file #### 第三:配置nginx
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf

- name: Start nginx #### 第四:启动nginx
service:
name: nginx
state: started

- name: Create wwwroot directory #### 第五:创建网站根目录
file:
dest: /var/www/html
state: directory

- name: Create test page index.html #### 第六:创建“主页”
shell: echo "hello {{hello}}" > /var/www/html/index.html # 在这里可以引用变量

2.2 site.conf

1
2
3
4
5
6
7
8
9
[root@k8s-node2 ~]# vim site.conf
server {
listen 80;
server_name www.example.com;
location / {
root /var/www/html;
index index.html;
}
}

2.3 执行playbook

1
2
3
4
# 检查语法
[root@k8s-node2 ~]# ansible-playbook nginx-playbook.yml --syntax-check
# 执行playbook
[root@k8s-node2 ~]# ansible-playbook nginx-playbook.yml
  • 查看配置文件是否成功
1
2
3
4
5
6
7
8
9
10
[root@k8s-node1 tmp]# cat /etc/nginx/conf.d/site.conf 
server {
listen 80;
server_name www.example.com;
location / {
root /var/www/html;
index index.html;
}
}
[root@k8s-node1 tmp]# systemctl status nginx # 查看当前nginx服务状态

03. Playbook文件结构

1
2
3
4
5
6
7
8
9
10
11
12
---      # 标记文件的开始
- hosts: webservers # 指定该playbook在哪个服务器上执行
vars: # 表示下面是定义的变量,
http_port: 80 # 变量的形式,key: value,这里http_port是变量名,80是值
max_clients: 200
remote_user: root # 指定远程的用户名,这里缩进和vars保持了一致,说明变量的代码块已经结束。
tasks: # 下面构成playbook的tasks,每个task都有 - name: 开始,name指定该任务的名称。
- name: ensure apache is at the latest version # 指定该任务的名称。
yum: pkg=httpd state=latest # yum说明要是用的模板名称,后面指定对应的参数,这两行结合起来就相当于一个shell命令。

- name: write the apache config file # 每个task之间可以使用空行来做区分。
template: src=/srv/httpd.j2 dest=/etc/httpd.conf

04.在变更时执行操作(handlers)

4.1 修改配置文件

  • 修改nginx配置文件,从新加载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@k8s-node1 tmp]# vim verify-nginx-conf.yaml
---
- hosts: webservers
gather_facts: no
remote_user: root

tasks:
- name: Copy nginx configuration file
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf

- name: reload nginx
service: name=nginx state=reloaded
  • 执行playbook修改配置文件
1
2
[root@k8s-node2 ~]# ansible-playbook verify-nginx-conf.yaml --syntax-check
[root@k8s-node2 ~]# ansible-playbook verify-nginx-conf.yaml

4.2 handlers只有修改才会重启

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@k8s-node1 tmp]# vim verify-nginx-conf.yaml
---
- hosts: webservers
gather_facts: no
remote_user: root

tasks:
- name: Copy nginx configuration file
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf

- name: reload nginx
service: name=nginx state=reloaded
  • 由于 site.conf配置没有修改,所以 changed=0 (没有执行 reload)
1
2
3
4
[root@k8s-node2 ~]# ansible-playbook verify-nginx-conf.yaml 
PLAY RECAP **************************************************************************************************************************************************
192.168.56.65 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.56.66 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

05.任务控制(tags)

  • 在一个大的 playbook配置文件中,指定执行某一个任务
  • 比如下面这个案子nginx部署的项目,可以在每个模块中添加一个tags
  • 这样就可以单独管理

5.1 添加tags

1
[root@k8s-node2 ~]# vim nginx-playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
---
- hosts: webservers # 对 webservers组中所有机器执行
vars: # 添加变量
hello: Ansible
remote_user: root # 以什么用户来执行

tasks: # 多个task通过 name名字区分
- name: Add repo #### 第一:拉取代码
yum_repository:
name: nginx
description: nginx repo
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no
enabled: 1
tags: addrepo

- name: Install nginx #### 第二:安装nginx
yum:
name: nginx
state: latest
tags: install

- name: Copy nginx configuration file #### 第三:配置nginx
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf

- name: Start nginx #### 第四:启动nginx
service:
name: nginx
state: started
tags: startngx

- name: Create wwwroot directory #### 第五:创建网站根目录
file:
dest: /var/www/html
state: directory

- name: Create test page index.html #### 第六:创建“主页”
shell: echo "hello {{hello}}" > /var/www/html/index.html # 在这里可以引用变量

5.2 指定只执行指定tag

1
2
3
4
# 指定,只执行启动nginx这个步骤
[root@k8s-node2 ~]# ansible-playbook nginx-playbook.yml --tags "startngx"
# 跳过,除了不启动nginx,其他都执行
[root@k8s-node2 ~]# ansible-playbook nginx-playbook.yml --skip-tags "startngx"

06. Playbook文件调试

6.1 调试文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@k8s-node2 ~]# vim debug.yaml
---
- hosts: webservers # 对 webservers组中所有机器执行
vars: # 添加变量
hello: Ansible
remote_user: root # 以什么用户来执行

tasks: # 多个task通过 name名字区分
- name: Create wwwroot directory #### 第五:创建网站根目录
file:
dest: /var/www/html
state: directory

- name: Create test page index.html #### 第六:创建“主页”
shell: echo "hello {{hello}}" > /var/www/html/index.html # 在这里可以引用变量

- name: debug playbook file
debug: msg="输出:{{hello}}"
tags: debugtag

6.2 语法检查

1
2
[root@k8s-node2 ~]# ansible-playbook debug.yaml --syntax-check
playbook: debug.yaml

6.3 debug调试

1
2
3
4
5
6
7
8
[root@k8s-node2 ~]# ansible-playbook debug.yaml --tags debugtag
TASK [debug playbook file] **********************************************************************************************************************************
ok: [192.168.56.65] => {
"msg": "输出:Ansible"
}
ok: [192.168.56.66] => {
"msg": "输出:Ansible"
}

__END__