Ansible tags allow you to run specific portions of your playbook, ignoring others. Since each invocation of ansible-playbook against a playbook requires multiple checks to determine whether or not a play should attempt to exectue, running an entire playbook will require more time than just running a portion of that playbook. Tags allow us to target a specific play or task. This can save significant time, particularly during testing, which may result in numerous executions to test individual changes.
We specify tags using the ‘tags’ directive:
tags: tag_1,tag_2...
To see what tags have been applied to a playbook, use ansible-playbook’s ‘--tags’ paraameter:
ansible-playbook --tags web --ask-become-pass playbooks/site.yml
or
ansible-playbook --tags “web,db” --ask-become-pass playbooks/site.yml
Here are two sections of our site.yaml playbook that apply tags:
- hosts: all
become: true
pre_tasks:
- name: Install Updates - Rocky
tags: always
dnf:
update_only: true
update_cache: true
[...]
- hosts: web_servers
become: true
tasks:
- name: Install Nginx Web Service
tags: nginx,rocky,web
dnf:
name:
- nginx
- pcp-pmda-nginx
state: latest
when: ansible_distribution == "Rocky"
The 'always' tag is a special tag that instructs Ansible to always run a play or task, regardless of whether it is specified. It has a complement, "never." These can both be overridden with "--skip-tags."
No comments:
Post a Comment