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.

- 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

Let us test this play book.

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

Now let is specify the tag play to select the tasks having play in the tags section.

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

Let us repeat this with the tag study this time.

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

You can see that the above commands executed only the tasks containing specified tags. This is how we can specify tags, we could also get the list of tags used in a play books using the list tags option.

ansible-playbook tags-demo-play.yml --list-tagsplaybook: tags-demo-play.yml

play #1 (localhost): Tags Demo Play TAGS: []
TASK TAGS: [play, study]

Tags can also be used to exclude certain tasks in a play!

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

Multiple tags can also be specified like this.

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

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!

Thank you for your time. Do follow me for more such tiny demo snippets.

Connecting SMEs (Subject Matter Experts) to businesses who need Help with Microservices, Machine Learning, Cloud, IaC, and DevOps and Training

Connecting SMEs (Subject Matter Experts) to businesses who need Help with Microservices, Machine Learning, Cloud, IaC, and DevOps and Training