默认情况下,Ansible在执行一个playbook时,会执行playbook中定义的所有任务。Ansible的标签(Tags)功能可以给单独任务甚至整个playbook打上标签,然后利用这些标签来指定要运行playbook中的个别任务,或不执行指定的任务。
- 打标签的方式有几种,比如: 对一个task打一个标签、对一个task打多个标签、对多个task打一个标签
- 对task打完标签应该如何使用,-t: 执行指定的tag标签任务, --skip-tags: 执行--skip-tags之外的标签任务
使用-t指定tags执行
cat 21.yml
---
- hosts: lbremote_user: roottasks:- name: Install Nfs Serveryum: name=nfs-utils state=presenttags: install_nfs- name: Service Nfs Serverservice: name=nfs-server state=started enabled=yestags: start_nfs-serveransible-playbook 21.yml -t install_nfs
ansible-playbook 21.yml -t install_nfs,start_nfs-server
使用--skip-tags排除不执行的tags
cat 22.yml
---
- hosts: webvars:http_port: 1234tasks:- name: Add Nginx Yum Repoyum_repository:name: nginxdescription: nginx repobaseurl: http://nginx.org/packages/centos/$releasever/$basearch/enabled: yesgpgcheck: yesgpgkey: https://nginx.org/keys/nginx_signing.key- name: Install Nginxyum:name: nginxstate: installed- name: Index FIlecopy:content: "This is ansible website ansible.com"dest: /usr/share/nginx/html/index.html- name: Copy Nginx.d/conf Filetemplate:src: ./www.confdest: /etc/nginx/conf.d/default.confbackup: yestags: push_config_filenotify: Restart Nginx- name: Start Nginxsystemd:name: nginxstate: startedenabled: yeshandlers:- name: Restart Nginxsystemd:name: nginxstate: reloadedansible-playbook 22.yml --skip-tags push_config_file
playbook任务标签介绍