Ansible Tags

Ansible tags can be assigned to tasks in a playbook to identify specific tasks that can be executed selectively. You can specify a tag for each task in a playbook using the “tags” attribute.

Here is an example of using tags in a playbook:

- name: Example playbook using tags
  hosts: all
  gather_facts: false
  tasks:
  - name: Task 1
    debug:
      msg: "Task 1"
    tags: database
 
  - name: Task 2
    debug:
      msg: "Task 2"
    tags: web
 
  - name: Task 3
    debug:
      msg: "Task 3"
    tags: deploy, network

In this example, three tasks are defined, each with a different tag. Task 1 has the tag “database”, task 2 has the tag “web”, and task 3 has the tags “deploy” and “network”.

To execute a specific task or set of tasks, you can specify the tag or tags when executing the playbook using the –tags option:

ansible-playbook example.yml --tags database

In this example, only the task with the “database” tag will be executed.

You can also use the –skip-tags option to exclude specific tags:

ansible-playbook example.yml --skip-tags web

In this example, all tasks except the task with the “web” tag will be executed.

These are just a few examples of how you can use tags to selectively execute tasks in a playbook. You can use tags in a variety of ways to make your playbooks more flexible and easier to manage.

Please follow and like us:
Content Protection by DMCA.com