ansible-playbook site.yml applies the whole Aurora Store infrastructure: packages, time zone, users, motd, facts, PostgreSQL, nginx, firewall, the website and its checks. That's the right thing to do to keep the servers in their desired state, but day to day you often only want one part: deploy a new version of the website without reviewing packages, add a colleague without touching nginx, or check what would change in the configuration without waiting for everything else. Tags let you label tasks with names and then choose which ones to run or skip. In this lesson you'll learn how to tag tasks, blocks, roles, imports and includes (and how tags are inherited in each case), the --tags, --skip-tags and --list-tags options, and the special tags always, never, tagged, untagged and all, and you'll apply a consistent tagging strategy to Aurora.

Contents

  1. Why run only one part
  2. Tagging tasks
  3. --tags, --skip-tags and --list-tags
  4. Tag inheritance: blocks, plays and roles
  5. Inheritance with import and include: apply
  6. Special tags: always, never, tagged, untagged and all
  7. Aurora's tagging strategy
  8. Practice: tagging the common role
  9. Practice: tagging the nginx and postgresql roles
  10. Practice: tagging aurora_app and the playbooks
  11. Combining tags with --limit and --check
  12. Reminder: --start-at-task and --step
  13. Verifying and saving the work

  1. Why run only one part

Situation Without tags With tags
Deploy a new version of the website ansible-playbook webservers.yml: installs, configures, reviews the firewall… ansible-playbook webservers.yml --tags deploy
Add a colleague The whole of site.yml on all three servers ansible-playbook site.yml --tags users
Review configuration drift --check of everything --check --diff --tags config
Debug without touching the firewall Comment out tasks (and forget to uncomment them) --skip-tags firewall

A word of warning before you start: tags are a convenience, not a substitute for the full run. Aurora's desired state is that of site.yml with no filters, and that's what should be run regularly (in Lesson 8.3 a CI/CD system will do it). A partial run can leave unapplied something that another part needed; the tag design (section 7) tries to prevent that from happening.

  1. Tagging tasks

The tags keyword accepts a string or a list:

- name: Set the time zone
  community.general.timezone:
    name: "{{ common_timezone }}"
  tags: config

- name: Publish the local facts
  ansible.builtin.template:
    src: facts.fact.j2
    dest: "{{ common_facts_dir }}/{{ common_facts_name }}.fact"
  tags:
    - config
    - facts
  • A task can have several tags: it runs if any of them is requested.
  • Names are free-form. Use lowercase, no spaces, and a small, stable vocabulary (section 7).
  • Tags don't change anything if --tags and --skip-tags aren't used: without filters, everything runs as usual.

  1. --tags, --skip-tags and --list-tags

Option Effect
--tags config Runs only the tasks tagged config (and the always ones)
--tags config,users Those tagged config or users
--skip-tags firewall Runs everything except the tasks tagged firewall
--tags config --skip-tags facts Those tagged config that aren't tagged facts (--skip-tags wins)
--list-tags Shows the tags of each play, without running anything
--list-tasks --tags config Shows which tasks would run with that filter

--list-tags and --list-tasks are your allies: before a filtered run, check what's going to run. As you saw in Lesson 4.5, they can only see imported tasks; dynamically included ones appear as the inclusion task itself.

There are also equivalent options in ansible.cfg ([tags] with run and skip) and environment variables (ANSIBLE_RUN_TAGS, ANSIBLE_SKIP_TAGS), but setting filters permanently is a bad idea: someone will run site.yml believing it applies everything.

  1. Tag inheritance: blocks, plays and roles

Tags placed at a higher level are inherited downward:

Where you put tags What it applies to
A task That task
A block All the tasks in the block (including those in rescue and always)
A play All the tasks in the play, including its roles
An entry in roles: All the tasks in the role and in its dependencies
import_tasks / import_role All the imported tasks
include_tasks / include_role Only the inclusion itself (section 5)

An example with a role in roles: (illustrative):

  roles:
    - role: nginx
      tags: nginx
    - role: aurora_app
      tags: aurora_app

With this, --tags nginx would run all the tasks of the nginx role. But look at the last row of the previous table that deals with roles: the aurora_app tag would also be inherited by the nginx dependency of aurora_app (Lesson 4.6), so the nginx tasks would end up with both tags and --tags aurora_app would drag them along too. Per-role tags also overlap with something Aurora already has: each layer has its own playbook (common.yml, dbservers.yml, webservers.yml), which is the natural way to choose which component to run. That's why Aurora does not tag the roles in roles: and uses functional tags instead (what kind of work a task does), which cut across all the roles.

  1. Inheritance with import and include: apply

You saw this coming in Lesson 4.5. With an import, the tags are copied to each imported task; with an include, they only apply to the inclusion task:

# The tasks in install.yml get the "packages" tag.
- name: Install nginx
  ansible.builtin.import_tasks: install.yml
  tags: packages

# Only the inclusion has the "firewall" tag; the tasks in firewall.yml don't.
- name: Configure the firewall
  ansible.builtin.include_tasks: firewall.yml
  tags: firewall

What happens with the include when you run --tags firewall? The inclusion runs (it has the tag), loads firewall.yml… and its tasks are skipped, because they don't have the tag. The result is baffling: "included" and not a single task. The solution is apply, which applies keywords to the included tasks:

- name: Configure the firewall
  ansible.builtin.include_tasks:
    file: firewall.yml
    apply:
      tags: firewall
  tags: firewall

You need both: tags on the inclusion, so that the inclusion runs with --tags firewall; and apply: tags, so that the included tasks have it too. The same goes for include_role:

- name: Apply the nginx role
  ansible.builtin.include_role:
    name: nginx
    apply:
      tags: web
  tags: web

With import_role, on the other hand, tags on the import is enough, just as with import_tasks.

  1. Special tags: always, never, tagged, untagged and all

Tag On a task it means… As a filter (--tags/--skip-tags)
always It always runs, even when filtering by other tags. It's only skipped with --skip-tags always (or with its other tag in --skip-tags) --skip-tags always disables it
never It never runs, unless another of its tags (or never) is explicitly requested --tags never runs them
tagged — (not put on tasks) --tags tagged: only the tasks with some tag
untagged — --tags untagged: only the tasks without tags
all — --tags all: all of them (the default)

Typical uses:

  • always for what can't be left out even when you filter: parameter validations, loading variables that other tasks need, final checks. You've already seen it without knowing: the Validating arguments against arg spec task that Ansible adds because of argument_specs is tagged always (Lesson 4.6).
  • never for optional tasks you only want on demand: diagnostics, reports, dangerous operations. It's combined with another tag that's used to request it: tags: [never, info] only runs with --tags info.
  • untagged for auditing the strategy: --list-tasks --tags untagged shows the tasks you forgot to tag.

  1. Aurora's tagging strategy

Before tagging, it's a good idea to agree on a small vocabulary. Aurora uses these tags:

Tag Meaning Roles that use it
packages Package installation (and associated services) common, nginx, postgresql
config Configuration files and system parameters common, nginx, postgresql, aurora_app
users System and database users common, postgresql
firewall Firewall rules and activation nginx
deploy Deployment of the store website aurora_app
facts Local facts (also config) common
info (+ never) Diagnostics that only run on demand Playbooks
always Validations, variable loading and checks Roles and playbooks

Criteria the design follows:

  • Functional tags, not per-role ones: --tags users manages the system users (common) and the PostgreSQL ones in one go. To choose a component, there are already the per-layer playbooks and --limit.
  • Every task has at least one tag (checked with untagged). That way no filter accidentally leaves "orphan" tasks out.
  • What other tasks need goes with always: if a task loads variables or checks requirements that the others use, it must run with any filter.
  • Final checks go with always: a partial run should also end by verifying that the store works.

  1. Practice: tagging the common role

The common role has a single task file. Group the related tasks into blocks and tag each group. The complete roles/common/tasks/main.yml:

---
- name: Common packages
  tags: packages
  block:
    - name: Install the common packages (Debian family)
      ansible.builtin.apt:
        name: "{{ common_packages }}"
        state: present
        update_cache: true
        cache_valid_time: 3600
      when: ansible_facts['os_family'] == 'Debian'

    - name: Install the common packages (Red Hat family)
      ansible.builtin.dnf:
        name: "{{ common_packages }}"
        state: present
      when: ansible_facts['os_family'] == 'RedHat'

- name: Set the time zone
  community.general.timezone:
    name: "{{ common_timezone }}"
  tags: config

- name: Team users
  tags: users
  block:
    - name: Manage the team users
      ansible.builtin.user:
        name: "{{ item.name }}"
        comment: "{{ item.comment | default(omit) }}"
        shell: /bin/bash
        state: "{{ item.state | default('present') }}"
      loop: "{{ common_users }}"
      loop_control:
        label: "{{ item.name }}"

    - name: Grant administrator privileges to the team admins
      ansible.builtin.user:
        name: "{{ item.name }}"
        groups: sudo
        append: true
      loop: "{{ common_users }}"
      loop_control:
        label: "{{ item.name }}"
      when:
        - item.admin | default(false)
        - item.state | default('present') == 'present'

- name: Configure the welcome message (motd)
  ansible.builtin.template:
    src: motd.j2
    dest: /etc/motd
    owner: root
    group: root
    mode: "0644"
  tags: config

- name: Local facts
  tags:
    - config
    - facts
  block:
    - name: Create the local facts directory
      ansible.builtin.file:
        path: "{{ common_facts_dir }}"
        state: directory
        owner: root
        group: root
        mode: "0755"

    - name: Publish the local facts
      ansible.builtin.template:
        src: facts.fact.j2
        dest: "{{ common_facts_dir }}/{{ common_facts_name }}.fact"
        owner: root
        group: root
        mode: "0644"
      register: common_local_facts

    - name: Re-read the local facts if they have changed
      ansible.builtin.setup:
        filter: ansible_local
      when: common_local_facts is changed
  • The tasks haven't changed: they've only been grouped. The execution order is exactly the same.
  • The keywords of each block (name, tags) go before block:, as agreed in Lesson 5.5.
  • The blocks group tasks that must go together: if you ran "Publish the local facts" without "Re-read the local facts", the information in memory would be out of date. With the tag on the block, it's impossible to separate them by mistake.

  1. Practice: tagging the nginx and postgresql roles

In roles split into files, the tags go on the imports in tasks/main.yml and are inherited by all the tasks in each file.

roles/nginx/tasks/main.yml:

---
- name: Check the role parameters
  ansible.builtin.assert:
    # ... (that, fail_msg and quiet unchanged, Lesson 5.5)
  tags: always

- name: Install nginx
  ansible.builtin.import_tasks: install.yml
  tags: packages

- name: Configure nginx
  ansible.builtin.import_tasks: configure.yml
  tags: config

- name: Configure the site
  ansible.builtin.import_tasks: site.yml
  tags: config

- name: Manage the service
  ansible.builtin.import_tasks: service.yml
  tags:
    - packages
    - config

- name: Configure the firewall
  ansible.builtin.include_tasks:
    file: firewall.yml
    apply:
      tags: firewall
  when: nginx_firewall_enabled
  tags: firewall
  • The range check goes with always: it's a validation, like the one in argument_specs.
  • service.yml ("Ensure nginx is started and enabled") has two tags: after installing and after configuring, the service must be running.
  • The firewall uses the apply form from section 5.

roles/postgresql/tasks/main.yml:

---
- name: Install PostgreSQL
  ansible.builtin.import_tasks: install.yml
  tags: packages

- name: Configure PostgreSQL
  ansible.builtin.import_tasks: configure.yml
  tags: config

- name: Manage users and databases
  ansible.builtin.import_tasks: users.yml
  tags: users

- name: Manage access control
  ansible.builtin.import_tasks: access.yml
  tags: config

There's a trap here. Try running only the configuration:

ansible-playbook dbservers.yml --tags config --check
TASK [postgresql : Configure the PostgreSQL parameters] ************************
fatal: [db1]: FAILED! =>
  msg: '''postgresql_config_dir'' is undefined'

postgresql_config_dir (and postgresql_service, postgresql_packages) are loaded with include_vars in the first task of install.yml (Lesson 4.5), and install.yml is tagged packages, which you didn't request. This is exactly the case covered by the criterion "what other tasks need goes with always". In roles/postgresql/tasks/install.yml, add the tag to that task:

- name: Load the distribution variables
  ansible.builtin.include_vars: "{{ lookup('ansible.builtin.first_found', postgresql_os_vars) }}"
  vars:
    postgresql_os_vars:
      files:
        - "{{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_major_version'] }}.yml"
        - "{{ ansible_facts['os_family'] }}.yml"
      paths:
        - vars
  tags: always

The task will now have packages (inherited from the import) and always (its own): with always, it runs with any filter. Repeat the test and you'll see it works.

  1. Practice: tagging aurora_app and the playbooks

In roles/aurora_app/tasks/main.yml, the requirement (the owner user) goes with always, the deployment with deploy, and the database configuration with deploy and config:

---
- name: Check that the website owner user exists
  ansible.builtin.getent:
    database: passwd
    key: "{{ aurora_app_owner }}"
  tags: always

- name: Create the web root directory
  ansible.builtin.file:
    # ... (unchanged)
  tags: deploy

- name: Apply the web server's pending changes
  ansible.builtin.meta: flush_handlers

- name: Deploy the store home page
  tags: deploy
  block:
    # ... (block, rescue and always unchanged, Lesson 5.5)

- name: Publish the store static pages
  ansible.builtin.copy:
    # ... (unchanged)
  tags: deploy

- name: Create the store configuration directory
  ansible.builtin.file:
    # ... (unchanged)
  tags:
    - deploy
    - config

- name: Publish the database connection details
  ansible.builtin.template:
    # ... (unchanged, with diff: false)
  tags:
    - deploy
    - config

meta: flush_handlers doesn't need a tag: it's an instruction for Ansible that does nothing if there are no pending handlers. The tags on the deployment block also reach its rescue and its always, so the rollback works the same with --tags deploy.

In the playbooks, the post_tasks checks get always and the nginx version becomes an on-demand diagnostic. webservers.yml:

  post_tasks:
    - name: Wait for the store website to respond
      ansible.builtin.uri:
        url: "http://localhost:{{ nginx_port }}/"
        return_content: true
      register: aurora_web_check
      until: aurora_web_check.status == 200
      retries: 5
      delay: 3
      check_mode: false
      tags: always

    - name: Get the nginx version
      ansible.builtin.command:
        cmd: nginx -v
      register: nginx_version_cmd
      changed_when: false
      check_mode: false
      tags:
        - never
        - info

    - name: Show the nginx version
      ansible.builtin.debug:
        msg: "{{ inventory_hostname }}: {{ nginx_version_cmd.stderr }}"
      tags:
        - never
        - info

    - name: Check that the store website works
      ansible.builtin.assert:
        that:
          - aurora_web_check.status == 200
          - "'Aurora Store' in aurora_web_check.content"
        fail_msg: "The website on {{ inventory_hostname }} does not show the Aurora Store page"
        success_msg: "The website on {{ inventory_hostname }} responds correctly (HTTP {{ aurora_web_check.status }})"
      tags: always

And in dbservers.yml, the two post_tasks checks ("Check that PostgreSQL accepts connections" and "Check that the store can authenticate to its database") get tags: always.

The two nginx version tasks must have the same tags: if only the second one had info, it would run without the first and fail with an undefined variable.

Check the result:

ansible-playbook site.yml --list-tags
playbook: site.yml

  play #1 (aurora): Common configuration for Aurora Store servers	TAGS: []
      TASK TAGS: [always, config, facts, packages, users]

  play #2 (dbservers): Configure the Aurora Store database servers	TAGS: []
      TASK TAGS: [always, config, packages, users]

  play #3 (webservers): Configure the Aurora Store web servers	TAGS: []
      TASK TAGS: [always, config, deploy, firewall, info, never, packages]

And that nothing has been left untagged:

ansible-playbook site.yml --list-tasks --tags untagged

The list for each play should come out empty. (Depending on the ansible-core version, --list-tasks may also show the meta tasks, which have no tag and don't matter.)

  1. Combining tags with --limit and --check

Tags choose which tasks; --limit chooses which hosts; --check and --diff decide whether it's applied. Combined, they allow very precise operations:

# Add a colleague on all the servers
ansible-playbook site.yml --tags users

# Deploy the website only on web1
ansible-playbook webservers.yml --tags deploy --limit web1

# Has anyone changed web2's configuration by hand?
ansible-playbook webservers.yml --tags config --check --diff --limit web2

# Everything except the firewall (for example, while you diagnose a network problem)
ansible-playbook webservers.yml --skip-tags firewall

# See the nginx version of the web servers
ansible-playbook webservers.yml --tags info

The output of --tags users on site.yml shows the strategy clearly:

PLAY [Common configuration for Aurora Store servers] ***************************
TASK [common : Validating arguments against arg spec 'main' - ...] *************
TASK [common : Manage the team users] ******************************************
TASK [common : Grant administrator privileges to the team admins] **************
PLAY [Configure the Aurora Store database servers] *****************************
TASK [postgresql : Validating arguments against arg spec 'main' - ...] *********
TASK [postgresql : Load the distribution variables] ****************************
TASK [postgresql : Create the PostgreSQL users] ********************************
TASK [postgresql : Create the databases] ***************************************
TASK [Check that PostgreSQL accepts connections] *******************************
TASK [Check that the store can authenticate to its database] *******************
PLAY [Configure the Aurora Store web servers] **********************************
TASK [nginx : Validating arguments against arg spec 'main' - ...] **************
TASK [nginx : Check the role parameters] ***************************************
TASK [aurora_app : Validating arguments against arg spec 'main' - ...] *********
TASK [aurora_app : Check that the website owner user exists] *******************
TASK [Wait for the store website to respond] ***********************************
TASK [Check that the store website works] **************************************

(The results of each task have been omitted.) Only users, plus the validations and checks. Notice that each play's fact gathering also takes place: gather_facts always runs, whatever the filter (in Lesson 5.8 you'll see how to control it).

Two precautions when combining:

  • Handlers aren't filtered by tags: if a task that runs notifies a handler, the handler runs. You don't need to tag handlers.
  • A filtered run doesn't guarantee the complete state: --tags deploy on a new server would deploy the website without installing nginx. For new servers, always do the full run.

  1. Reminder: --start-at-task and --step

In Lesson 3.7 you met two other ways of running only part of a playbook, designed for debugging rather than for operations:

Option What it does Limitations
--start-at-task "name" Starts at the task with that name and runs everything after it Skips earlier register and set_fact; doesn't find tasks from include_*
--step Asks before each task (y/n/c) Interactive: for debugging only
--tags / --skip-tags Selects by tags Depends on consistent tagging

Tags are the tool for daily work; --start-at-task and --step are for resuming a run or stepping through it while you debug.

  1. Verifying and saving the work

A full run should still give changed=0 (tags don't change anything without filters), except that the two nginx version tasks no longer appear because they're tagged never:

ansible-playbook site.yml
ansible-playbook webservers.yml --tags deploy --check
ansible-playbook site.yml --tags users --check

Save the work:

git add roles/ webservers.yml dbservers.yml
git commit -m "Functional tags: packages, config, users, firewall, deploy, info"

Common Mistakes and Tips

  • --tags that "does nothing": the name doesn't match (check with --list-tags) or the tasks are in an include_* without apply.
  • A task fails with an undefined variable only with --tags: the variable is defined in a task with another tag (set_fact, include_vars, register). That task must be tagged always or share a tag with the ones that use it.
  • Per-role tags that drag dependencies along: the tags of an entry in roles: are inherited by the role's dependencies.
  • Using --tags as your normal way of working: it leaves parts of the infrastructure unapplied for weeks. The full run should be the usual one.
  • A vocabulary that grows out of control (nginx-config, conf, configuration…): nobody remembers which tag to use. Document the vocabulary (for example, in the project's README.md) and review it with --list-tags.
  • Tip: after tagging, always run --list-tasks --tags untagged. Untagged tasks are skipped with any --tags, and they're usually the ones that were added later without thinking about tagging.

Exercises

Exercise 1: What runs?

Without running anything, say which tasks of the webservers.yml play would run with each command: (a) --tags firewall; (b) --skip-tags config; (c) --tags info,deploy; (d) --tags never. Then check it with --list-tasks.

Exercise 2: An on-demand diagnostic

Add two tasks to dbservers.yml, in post_tasks, that only run with --tags info: one that gets the PostgreSQL version with psql -V and another that shows it.

Exercise 3: The include without apply

Temporarily remove the apply block from the inclusion of firewall.yml in the nginx role and run ansible-playbook webservers.yml --tags firewall --check. Describe the output and explain why. Restore the file afterwards.

Solutions

Exercise 1. In every case, the always tasks also run (the nginx and aurora_app validations, "Check the role parameters", "Check that the website owner user exists", "Wait for the store website to respond" and "Check that the store website works").

  • (a) The inclusion of firewall.yml and its three tasks.
  • (b) Everything except what's tagged config: the nginx installation, the firewall, the website deployment (except the configuration directory and db.conf, which are also config). "Ensure nginx is started" is tagged packages and config, and --skip-tags wins: it's skipped.
  • (c) The two nginx version tasks and all the deploy ones.
  • (d) The tasks tagged never (the two nginx version ones).

Exercise 2.

    - name: Get the PostgreSQL version
      ansible.builtin.command:
        cmd: psql -V
      register: postgresql_version_cmd
      changed_when: false
      check_mode: false
      tags:
        - never
        - info

    - name: Show the PostgreSQL version
      ansible.builtin.debug:
        msg: "{{ inventory_hostname }}: {{ postgresql_version_cmd.stdout }}"
      tags:
        - never
        - info

ansible-playbook site.yml --tags info will then show both the nginx version and the PostgreSQL version. If you add it to Aurora, include it in the next commit.

Exercise 3. The output shows included: .../firewall.yml for web1, web2, but none of its three tasks: the inclusion has the firewall tag and runs, but the loaded tasks don't have it and the filter discards them. With apply: tags: firewall, the tasks get the tag when they're included. Restore the file with git checkout roles/nginx/tasks/main.yml.

Conclusion

You've learned to tag tasks, blocks, plays, roles and imports, and you know that with includes the tags only affect the inclusion unless you use apply. You know --tags, --skip-tags and --list-tags, the special tags always (validations and checks that can't be left out), never (on-demand diagnostics), tagged, untagged (for auditing) and all, and how to combine them with --limit and --check. Aurora now has a vocabulary of functional tags (packages, config, users, firewall, deploy, facts and info) applied to its four roles and its playbooks, with always on the validations, on the postgresql variable loading and on the post_tasks checks. With --tags deploy --limit web1 you now know how to deploy the website on a single server. But deploying server by server by hand doesn't scale, and while a web server is being updated, it keeps receiving visitors. In the next lesson, Rolling Deployments and Delegation, you'll add a load balancer to Aurora and make the deployment go through the web servers in turn, taking each one out of the load balancer while it's being updated, so that the store doesn't stop working for a single second.

Ansible Course: From Beginner to Advanced

Module 1: Introduction to Ansible

Module 2: Ansible Fundamentals

Module 3: Playbooks

Module 4: Roles

Module 5: Advanced Playbook Techniques

Module 6: Ansible Galaxy

Module 7: Ansible Tower, AWX and Automation Platform

Module 8: Ansible for DevOps

Module 9: Ansible Best Practices

Module 10: Advanced Ansible Topics

Module 11: Final Project

© Copyright 2026. All rights reserved