ansible系列(26)--ansible的tags标签
1. tags标签
默认情况下, Ansible 在执行一个 playbook 时,会执行 playbook 中所有的任务。而标签功能是用来指定要运行 playbook 中的某个特定的任务;
- 为
playbook添加标签的方式有如下几种:- 对一个
task打一个标签; - 对一个
task打多个标签; - 对多个
task打一个标签;
- 对一个
task打完标签使用的几种方式:-t执行指定tag标签对应的任务;--skip-tags执行除--skip-tags标签之外的所有任务;
1.1 指定执行某个tags
编写playbook文件,为每个task都设置一个tag:
[root@xuzhichao playbook]# cat install_httpd.yml
---
- hosts: 192.168.20.23
remote_user: root
tasks:
- name: Install Htttpd Server
yum:
name: httpd
state: present
tags:
- install_httpd
- install_apache
- name: Configure Httpd Server
copy:
src: conf/httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
owner: "root"
group: "root"
mode: "0644"
notify: Restart Httpd Server
tags: conf_httpd
- name: Init Httpd Server
copy:
src: file/test.html.j2
dest: /var/www/html/test.html
owner: "apache"
group: "apache"
mode: "0644"
tags: init_httpd
- name: Start Httpd Server
service:
name: httpd
state: started
enabled: yes
tags: start_httpd
handlers:
- name: Restart Httpd Server
service:
name: httpd
state: restarted
查看此playbook所有的tag:
[root@xuzhichao playbook]# ansible-playbook install_httpd.yml --list-tags
playbook: install_httpd.yml
play #1 (192.168.20.23): 192.168.20.23 TAGS: []
TASK TAGS: [conf_httpd, init_httpd, install_apache, install_httpd, start_httpd]
指定运行的任务的tag,使用 -t 指定 tags 标签对应的任务, 多个 tags 使用逗号隔开即可:
[root@xuzhichao playbook]# ansible-playbook -C -t install_httpd,start_httpd install_httpd.yml
PLAY [192.168.20.23] ******************************************************************************************************************************************
TASK [Install Htttpd Server] **********************************************************************************************************************************
ok: [192.168.20.23]
TASK [Start Httpd Server] *************************************************************************************************************************************
changed: [192.168.20.23]
PLAY RECAP ****************************************************************************************************************************************************
192.168.20.23 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.2 指定排除某个tags
使用 --skip-tags 排除不执行的 tags :
[root@xuzhichao playbook]# ansible-playbook --skip-tags install_httpd,start_httpd install_httpd.yml
PLAY [192.168.20.23] ******************************************************************************************************************************************
TASK [Configure Httpd Server] *********************************************************************************************************************************
changed: [192.168.20.23]
TASK [Init Httpd Server] **************************************************************************************************************************************
ok: [192.168.20.23]
RUNNING HANDLER [Restart Httpd Server] ************************************************************************************************************************
changed: [192.168.20.23]
PLAY RECAP ****************************************************************************************************************************************************
192.168.20.23 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

浙公网安备 33010602011771号