1. tags
只执行某个任务而不是全部任务,使用打标签的方法tags
格式一:
[root@server4 ~]# cat mkdir.yml
---
- hosts: testB
remote_user: root
tasks:
- name: task1
file: path=/testdir/task1
state=touch
tags: t1
- name: task2
file: path=/testdir/task2
state=touch
tags: t2
# 测试:
[root@server4 ~]# ansible-playbook --tags=t2 mkdir.yml
PLAY [testB] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server3]
TASK [task2] *******************************************************************
changed: [server3]
PLAY RECAP *********************************************************************
server3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@server4 ~]# ansible-playbook --skip-tags=t2 mkdir.yml # 不执行t2其它的都执行
PLAY [testB] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server3]
TASK [task1] *******************************************************************
changed: [server3]
PLAY RECAP *********************************************************************
server3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
格式二:
[root@server4 ~]# cat mkdir.yml
---
- hosts: testB
remote_user: root
tasks:
- name: task1
file: path=/testdir/task1
state=touch
tags:
- t1
- name: task2
file: path=/testdir/task2
state=touch
tags: [‘t2’]
给多个task使用多个标签
格式一:
[root@server4 ~]# cat tags.yml
---
- hosts: testB
remote_user: root
tasks:
- name: install httpd
tags: httpd,package
yum:
name=httpd
state=latest
- name: start httpd
tags: httpd,service
service:
name: httpd
state: started
格式二:
[root@server4 ~]# cat tags.yml
---
- hosts: testB
remote_user: root
tags: httpd
tasks:
- name: install httpd
tags: ['package']
yum:
name=httpd
state=latest
- name: start httpd
tags:
- service
service:
name: httpd
state: started
查看playbook中有多少个标签
[root@server4 ~]# ansible-playbook --list-tags tags.yml
playbook: tags.yml
play #1 (testB): testB TAGS: [httpd]
TASK TAGS: [httpd, package, service]
1.1 always
当task中的tags有always时,这个task每次都会执行
[root@server4 ~]# cat tags1.yml
---
- hosts: testB
remote_user: root
tasks:
- name: task1
file: path=/testdir/task1
state=touch
tags: t1
- name: task2
file: path=/testdir/task2
state=touch
tags: t2
- name: task3
file: path=/testdir/task3
state=touch
tags: t3,always
# 测试:
[root@server4 ~]# ansible-playbook --tags=t2 mkdir.yml # 只执行t2但是t3中有always,所以t3也会被执行
PLAY [testB] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server3]
TASK [task2] *******************************************************************
changed: [server3]
PLAY RECAP *********************************************************************
server3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
如果不想执行always,可以跳过always
[root@server4 ~]# ansible-playbook --tags t1 --skip-tags always mkdir.yml
PLAY [testB] *************************************************************************************************************************************
TASK [task1] *************************************************************************************************************************************
changed: [server3]
PLAY RECAP ***************************************************************************************************************************************
server3 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
只跳过指定task的always
[root@server4 ~]# cat tags1.yml
---
- hosts: testB
remote_user: root
tasks:
- name: task1
file: path=/testdir/task1
state=touch
tags: t1,always
- name: task2
file: path=/testdir/task2
state=touch
tags: t2
- name: task3
file: path=/testdir/task3
state=touch
tags: t3,always
[root@server4 ~]# ansible-playbook --tags t2 --skip-tags t3 tags1.yml # 跳过t3
PLAY [testB] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server3]
TASK [task1] *******************************************************************
changed: [server3]
TASK [task2] *******************************************************************
changed: [server3]
PLAY RECAP *********************************************************************
server3 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.2 关于tags的其它标签用法:tagged、untagged、all
1.3 nerver
nerver标签:表示不执行task3
[root@server4 ~]# cat tags1.yml
---
- hosts: testB
remote_user: root
tasks:
- name: task1
file: path=/testdir/task1
state=touch
tags: t1
- name: task2
file: path=/testdir/task2
state=touch
tags: t2
- name: task3
file: path=/testdir/task3
state=touch
tags: t3,never
# 测试:
[root@server4 ~]# ansible-playbook tags1.yml
PLAY [testB] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server3]
TASK [task1] *******************************************************************
changed: [server3]
TASK [task2] *******************************************************************
changed: [server3]
PLAY RECAP *********************************************************************
server3 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0