Ansible: Tags
Ansible's tags are helpful to run a specific part of the playbook, rather running the whole playbook. Using tags, you can run or skip the tasks.
Tags can be added to one task or multiple tasks or role, block etc. Let see how to do that
Adding tags to a task
tasks:
- name: Assign the var
apt:
name: apache2
state: latest
tags:
- vars
- name: Enable and run httpd
apt:
name: httpd
state: started
enabled: 'yes'
tags:
- httpd
- vars
Adding tags to role
roles:
- role: config
vars:
port: 8003
tags: [ web, flask ]
Adding tags to block All the tasks in the block will share the same tag's
tasks:
- name: git tasks
tags: git
block:
- name: install apache
apt:
name: git
state: latest
Special tags Ansible have two special tags, never and always. If you add always tag to task, play, Ansible will always run the task or play, unless explicitly asked to skip with the help of (—skip-tags always)
On the other hand, never tag if assign to a task or play, Ansible will skip that task or play unless you explicitly asked it (—tags never).
Running the Playbook with tags
will run tasks with tag git :
ansible-playbook example.yml --tags "git"
will not run tasks with tag git :
ansible-playbook example.yml --skip-tags "git"
list all the tags :
ansible-playbook example.yml --list-tags
run all tasks, ignore tags (default behavior):
--tags all
run only tasks with either the tag tag1 or the tag tag2:
--tags [tag1, tag2]
run all tasks, except those with either the tag tag3 or the tag tag4:
--skip-tags [tag3, tag4]
run only tasks with at least one tag:
--tags tagged
run only tasks with no tags:
--tags untagged
That's all about the Ansible tags. Now go and tag your tasks :).
Cheers!