ansible-playbook中标签的使用 tags

在某些情况下,我们可能只需要选择性的执行playbook中的某一部分,这时候就需要用到playbook的标签(tags)功能,在playbook中添加标签可以让playbook在运行时选择性的执行被标签的内容。

playbook中标签的使用

此处沿用上一节中所使用的httpd的yaml文件,再次将httpd.conf文件进行修改,上一节中httpd的端口从8080改为了80,在这里再次将端口从80改为8080。

修改httpd端口,将其改为8080

1
2
[root@ansible data]# vim httpd.conf
Listen 8080 #在配置文件中找到Listen,修改为8080

编辑yaml文件,对文件内的内容贴标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@ansible data]# vim httpd.yaml
---
#install httpd
- hosts: webserver
remote_user: root

tasks:
- name: install
yum: name=httpd
- name: config
copy: src=/data/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart service
tags: config #将config段贴上标签
- name: service
service: name=httpd state=started

handlers:
- name: restart service
service: name=httpd state=restarted

由于此时http服务已经安装,不需要再次安装此服务,我们只需要执行被标签的config部分就行,标签的config执行时,触发了handler的重启服务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#使用ansible-playbook -t 选项指定标签名字,就能执行指定的标签
[root@ansible data]# ansible-playbook -t config httpd.yaml

PLAY [webserver] ************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [192.168.73.135]
ok: [192.168.73.134]

TASK [config] ***************************************************************************************************
changed: [192.168.73.135]
changed: [192.168.73.134]

RUNNING HANDLER [restart service] *******************************************************************************
changed: [192.168.73.135]
changed: [192.168.73.134]

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

验证远程服务器端口是否被改为8080

1
2
3
4
5
6
[root@ansible data]# ansible webserver -a 'ss -tnl |grep 8080'
192.168.73.135 | CHANGED | rc=0 >>
LISTEN 0 128 :::8080 :::*

192.168.73.134 | CHANGED | rc=0 >>
LISTEN 0 128 :::8080 :::*

playbook多标签的使用

我们可以在yaml文件中使用多个不同的标签,也可以使用多个相同的标签,让playbook执行标签时一次执行多个不同的操作,从而实现按类来进行执行被标签的内容。

再次修改http端口将其改为80端口

1
2
3

[root@ansible data]# vim httpd.conf
Listen 80

创建yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@ansible data]# vim httpd.yaml

---
#install httpd
- hosts: webserver
remote_user: root

tasks:
- name: install
yum: name=httpd
- name: config
copy: src=/data/httpd.conf dest=/etc/httpd/conf/httpd.conf
tags: config #对config贴上config标签
- name: service #service和restart service的标签相同,不再使用notify和handlers来触发服务的重启
service: name=httpd state=started
tags: service
- name: restart service
service: name=httpd state=restarted
tags: service

由于没有了notify和handlers,再次执行playbook需要将config标签和service标签一起执行,此时playbook就会将service和restart service全部都执行一遍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@ansible data]# ansible-playbook -t config -t service httpd.yaml

PLAY [webserver] ************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [192.168.73.134]
ok: [192.168.73.135]

TASK [config] ***************************************************************************************************
changed: [192.168.73.135]
changed: [192.168.73.134]

TASK [service] **************************************************************************************************
ok: [192.168.73.134]
ok: [192.168.73.135]

TASK [restart service] ******************************************************************************************
changed: [192.168.73.135]
changed: [192.168.73.134]

PLAY RECAP ******************************************************************************************************
192.168.73.134 : ok=4 changed=2 unreachable=0 failed=0
192.168.73.135 : ok=4 changed=2 unreachable=0 failed=0

验证端口是否被更改为了80

1
2
3
4
5
6
[root@ansible data]# ansible webserver -a 'ss -tnl |grep 80'
192.168.73.135 | CHANGED | rc=0 >>
LISTEN 0 128 :::80 :::*

192.168.73.134 | CHANGED | rc=0 >>
LISTEN 0 128 :::80 :::*