FreeKB - Ansible Getting Started with Tags
Ansible - Getting Started with Tags

Tags can be used to only play certain task or to not play certain tasks. The tags module is used to create tags, like this.

tags: foo

 

Or like this.

tags:
  - foo

 

For example, this playbook has two tasks, where one task is tagged "foo" and the other task is tagged "bar".

---
- hosts: all
  tasks:
    - name: foo task
      debug:
        msg: "foo task"
      tags:
        - foo

    - name: bar task
      debug:
        msg: "bar task"
      tags:
        - bar

 

The --tags command line option can be used to only play the "foo" task, like this.

ansible-playbook example.yml --tags "foo"

 

Which would only play the foo task, like this.

TASK [foo task] 
ok: [server1.example.com] => {
    "msg": "foo task"
}

 

Likewise, the --skip-tags command line option can be used to skip tasks that have a certain play. In this example, the "foo" task is skipped, like this.

ansible-playbook example.yml --skip-tags "foo"

 

Which would only play the bar task, like this.

TASK [bar task] 
ok: [server1.example.com] => {
    "msg": "bar task"
}

 


Tagging every task

Every task is a playbook can be tagged with the same tag. However, the better way to do this is to create the tag before tasks, like this. 

---
- hosts: all
  tags: foo
  tasks:
  . . .

 

Or like this.

---
- hosts: all
  tags:
    - foo
  tasks:
  . . .

 

Or like this.

---
- hosts: all
  tags: [ foo ]
  tasks:
  . . .

 


Tagging Roles

To run every task in a role, the tags module can be used with the role, like this. If you only want certain tasks in the role to be run, then you would apply tags to each task in the role.

---
- hosts: all
  roles:
    - role: foo
      tags: bar

 


list tasks

The --list-tasks command line option can be used to output the tasks that would have been invoked.

ansible-playbook example.yml --tags "foo" --list-tasks

 

Which should return something like this.

playbook: example.yml

  play #1 (all): all TAGS: []
    tasks:
      name of your first task  TAGS: [foo, bar]
      name of your second task  TAGS: [foo, bar]
      name of your third task  TAGS: [foo]

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter bdd25 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |