Tags used in Ansible

Tags

In the event where you have a huge playbook, it might be helpful to run just explicit pieces of it as opposed to running the whole playbook. This can be achieved by using Ansible tags. By the use of tags to run or avoid chosen tasks is a two-venture measure:

  • By adding tags to your assignments, either independently or with the help of tag inheritance from a block, role, play, or import.
  • Choose or skip tags whenever you execute your playbook.

Including tags with the tags keyword

Tags can be added to a solitary task or incorporate. Similarly, by characterizing tags at the degree of a block, play, role, or import, you can add them with numerous tasks. All the use cases are addressed by tags keyword. The keyword tags consistently characterize tags and add to those tasks which it doesn’t choose or skip tasks for running. Depending on the tags, you can choose or skip tasks according to tags at the command line whenever you execute a playbook.

Adding tags to singular tasks

At the easiest level, you can apply at least one tag to an individual assignment. You can join tags with the assignments in playbooks, in task records, or inside a role. In the following illustration, you can see that tags link two different tasks with various tags:

In the event where you need to run the setup and bundles/packages part of an extremely long playbook, at that point, you can utilize the – tags choice on the command line.

Also, on the off chance that you need to run a playbook without certain tagged assignments, at that point, you can utilize the – skip-tasks command-line alternative.

Adding tags to includes

You can apply tags to dynamic remembers for a playbook. Likewise, with tasks on an individual assignment, tags on an include_* task apply just to the incorporate itself, not to any tasks inside the included document or role. In the event that you add mytag to a dynamic incorporate, at that point run that playbook with — tags mytag, Ansible executes the include itself, executes any tasks inside the included record or role labeled with mytag, and skirts any tasks inside the included document or part without that tag.

You add tags to includes a similar way you add tags to some other assignment:

Tag inheritance: adding tags to various tasks

On the off chance that you need to apply the same type of tag or tags to various tasks without adding a tags line to each task, you can characterize the tags at the degree of your play or block or when you include a role or import a file. Ansible applies the tags down the reliance chain to all child tasks. With the roles and imports, Ansible affixes the tags set by the roles area or import to any tags set on singular tasks or blocks inside the role or imported file. This is called tag inheritance. It is helpful because there is no need to tag each task. Nonetheless, the tags actually apply to the tasks independently.

Adding tags to roles

There are three different ways to add tags to roles:

  1. Include similar tags or labels to all assignments in the part by setting tags under roles.
  2. Include similar tags or labels to all errands in the function by setting labels on a static import_role in your playbook.
  3. Add a tag or label to singular tasks or blocks inside the role itself. This is the main methodology that permits you to choose or skirt a few tasks inside the job. To choose or skip tasks inside the role, you should have labels set on singular assignments or squares, utilize the dynamic include_role in your playbook, and include similar tags or labels to the incorporate. At the point you utilize this methodology and afterward run your playbook with – tags foo, Ansible runs the remember itself in addition to any assignments for the job that additionally have the tag foo.

At the point of consolidating a function in your playbook statically with the roles keyword, Ansible adds any tags you characterize to every task in the role. For instance:

Special tags: always and never

Ansible stores two label names for exceptional conduct: consistently and never. In the event where you appoint the always tag to a task or play, Ansible will consistently execute that assignment or play, except if you explicitly skip it by using – – skip-tags always.

New in version 2.5.

On the off chance that you allocate the never tag to an assignment or play, Ansible will avoid that task or play except if you explicitly demand it (- – tags never).

The rarely-utilized debug task in the illustration above possibly runs when you explicitly demand the troubleshoot or never labels.

Arranging tags globally

In the event where you run or skirt certain labels, as a matter of course, you can utilize the TAGS_RUN and TAGS_SKIP choices in the Ansible arrangement to set those defaults.

Pin It on Pinterest

Share This