Ansible Tags Usage — a Tiny Demonstration

Tags help Ansible tasks to be selected from a play or play-book. If you need to play(execute) only certain specific tasks you can specify the tags needed while executing the ansible-playbook command. Let us have a quick demonstration of this. We will first create the YAML file, say tags-demo-play.yml as below.

Image for post
Image for post
- name: Tags Demo Play
hosts: localhost
gather_facts: false
connection: local
tasks:
- name: Going to play
debug: msg="I am playing.."
tags: play
- name: Going to study
debug: msg="I am studying.."
tags: study
ansible-playbook tags-demo-play.ymlPLAY [Tags Demo Play] **********************************************************

TASK [Going to play] ***********************************************************
ok: [localhost] => {
"msg": "I am playing.."
}

TASK [Going to study] **********************************************************
ok: [localhost] => {
"msg": "I am studying.."
}

PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
ansible-playbook tags-demo-play.yml -t playPLAY [Tags Demo Play] **********************************************************

TASK [Going to play] ***********************************************************
ok: [localhost] => {
"msg": "I am playing.."
}

PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
ansible-playbook tags-demo-play.yml -t studyPLAY [Tags Demo Play] **********************************************************

TASK [Going to study] **********************************************************
ok: [localhost] => {
"msg": "I am studying.."
}

PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
ansible-playbook tags-demo-play.yml --list-tagsplaybook: tags-demo-play.yml

play #1 (localhost): Tags Demo Play TAGS: []
TASK TAGS: [play, study]
ansible-playbook tags-demo-play.yml --skip-tags play

PLAY [Tags Demo Play] **********************************************************

TASK [Going to study] **********************************************************
ok: [localhost] => {
"msg": "I am studying.."
}

PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
ansible-playbook tags-demo-play.yml -t play,study

PLAY [Tags Demo Play] **********************************************************

TASK [Going to play] ***********************************************************
ok: [localhost] => {
"msg": "I am playing.."
}

TASK [Going to study] **********************************************************
ok: [localhost] => {
"msg": "I am studying.."
}

PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0

What are the use cases for tags?

When you have big play books containing several tasks, you may want to carefully tags then viz. install, setup, backup, restore etc. so that you can create multipurpose play books instead of creating separate files. This also adds to the readability and self documentation of play books!

Written by

AWS Certified DevOps Engineer & Solutions Architect Professional — Docker | Kubernetes | DevOps — Trainer | Running | Swimming | Cycling

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store