Skip to content

Selecting Tasks to Run Using Tasks


Every playbook covered in this course runs its entire task list, every time. Tags change that — labeling parts of a playbook so you can selectively run, or selectively skip, just the pieces you actually need right now, without editing the file at all.

Tagging A Task

- name: Install myapp
  ansible.builtin.package:
    name: myapp
    state: present
  tags:
    - install

tags: takes a list — a task can carry more than one:

- name: Deploy config
  ansible.builtin.template:
    src: app.conf.j2
    dest: /etc/myapp/app.conf
  tags:
    - configure
    - deploy

--tags: Running Only What’s Tagged

ansible-playbook site.yaml --tags install

Runs only tasks tagged install — everything else is skipped entirely for this run, without touching the playbook file itself.

--skip-tags: Running Everything Except What’s Tagged

ansible-playbook site.yaml --skip-tags configure

The inverse — every task runs except the ones tagged configure.

Tag Inheritance

Tags set on a block or an entire play apply to every task inside, the same propagation behavior established for other block-level keywords throughout this course:

- name: Configuration Steps
  block:
    - name: Deploy config
      ansible.builtin.template:
        src: app.conf.j2
        dest: /etc/myapp/app.conf
    - name: Ensure a tuning setting is correct
      ansible.builtin.lineinfile:
        path: /etc/myapp/app.conf
        regexp: '^max_connections'
        line: "max_connections = 100"
  tags:
    - configure

Both tasks inherit the configure tag from the block, without needing it repeated on each one individually.

A Realistic Example: Phased Deployment Tags

Tagging the natural phases of a deployment — install, configure, restart — the way a real long playbook is genuinely organized:

- name: Full Deployment
  hosts: servers
  tasks:
    - name: Install myapp
      ansible.builtin.package:
        name: myapp
        state: present
      tags:
        - install

    - name: Deploy config
      ansible.builtin.template:
        src: app.conf.j2
        dest: /etc/myapp/app.conf
      tags:
        - configure

    - name: Restart service
      ansible.builtin.systemd:
        name: myapp
        state: restarted
      tags:
        - restart
ansible-playbook site.yaml --tags configure

Just the config deployment runs — no reinstalling the package, no unnecessary restart — genuinely useful the moment you’re iterating on configuration specifically, without wanting to redo everything else around it every single time.

Best Practices

  • Tag at a meaningful phase levelinstall, configure, restart — rather than tagging every single trivial task individually, which adds overhead without adding real selectivity.
  • Use consistent tag names across a project, so --tags configure means the same thing in every playbook that uses it, not something that has to be re-learned per file.
  • Reach for --tags/--skip-tags for iterative development and targeted re-runs, not as a substitute for actually splitting a playbook into logical files when it’s grown large enough to warrant that instead.
Last updated on