TExcelTAD
Home › Configuration Management: Ansible › Tags, --limit & Partial Runs

Tags, --limit & Partial Runs

A full playbook run on a fleet can take many minutes. Often you only need part of it: just the nginx config, just one host, just from the task that failed. Ansible has a flag for each of those.

Flag Narrows
--tags x,y / --skip-tags z tasks, by tag
--limit pattern (-l) hosts, within the play's hosts:
--start-at-task "name" skip everything before a task
--step ask before each task (interactive)
--list-tags, --list-tasks, --list-hosts show what would run

Tags

Tags go on tasks, blocks, roles or imports, and are inherited by everything inside:

bash · try it yourself
cat > site.yml <<'YML'
- name: Web server
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Packages
      ansible.builtin.debug: {msg: installing packages}
      tags: [packages]

    - name: Nginx config
      tags: [nginx, config]
      block:
        - name: Main config
          ansible.builtin.debug: {msg: nginx.conf}
        - name: Site config
          ansible.builtin.debug: {msg: site.conf}

    - name: App config
      ansible.builtin.debug: {msg: app.env}
      tags: [config]

    - name: Always checked
      ansible.builtin.debug: {msg: preflight}
      tags: [always]

    - name: Expensive benchmark
      ansible.builtin.debug: {msg: benchmarking}
      tags: [never, bench]
YML
ansible-playbook site.yml --list-tags
run() { echo "== $*"; ansible-playbook site.yml "$@" | grep '"msg"' | sed 's/ *"msg": //'; }
run
run --tags config
run --tags nginx
run --skip-tags config
run --tags bench

playbook: site.yml

  play #1 (localhost): Web server	TAGS: []
      TASK TAGS: [always, bench, config, never, nginx, packages]
==
"installing packages"
"nginx.conf"
"site.conf"
"app.env"
"preflight"
== --tags config
"nginx.conf"
"site.conf"
"app.env"
"preflight"
== --tags nginx
"nginx.conf"
"site.conf"
"preflight"
== --skip-tags config
"installing packages"
"preflight"
== --tags bench
"preflight"
"benchmarking"

Two special tags:

  • always runs unless you skip it explicitly (--skip-tags always). Use it for preflight checks and variable setup that later tasks depend on.
  • never runs only when one of its other tags is requested. Use it for dangerous or slow tasks ([never, reset_db]).

Tag by component (nginx, firewall, users) and by kind (config, packages). Don't tag every task with a unique tag: tags are for the slices people actually run.

--limit

--limit (-l) reduces the hosts to the intersection of the play's pattern and yours. Inventory patterns work, including a file of host names with @:

bash · try it yourself
cat > ansible.cfg <<'INI'
[defaults]
inventory = hosts.ini
interpreter_python = auto_silent
forks = 1
INI
cat > hosts.ini <<'INI'
[web]
web[1:4] ansible_connection=local
INI
cat > ping.yml <<'YML'
- name: Ping web
  hosts: web
  gather_facts: false
  tasks:
    - name: Ping
      ansible.builtin.ping:
YML
ansible-playbook ping.yml --limit web2 | grep '^ok'
ansible-playbook ping.yml --limit 'web*:!web1' | grep '^ok'
printf 'web3\nweb4\n' > retry.txt
ansible-playbook ping.yml --limit @retry.txt | grep '^ok'
ok: [web2]
ok: [web2]
ok: [web3]
ok: [web4]
ok: [web3]
ok: [web4]

The @file form is how you rerun only the hosts that failed last time: write their names to a file (older Ansible versions did this automatically as site.retry; retry_files_enabled is off by default now).

--start-at-task

After fixing the cause of a failure on task 37, you don't want to wait for tasks 1-36 again:

bash
$ ansible-playbook site.yml --limit web3 --start-at-task "Nginx site config"

Careful: the skipped tasks also don't register anything or set facts, so later tasks that depend on them may fail or misbehave. If a playbook is idempotent, a full rerun is usually just slower, not riskier.

Test yourself

A task is tagged [never, cleanup]. When does it run?

  1. Always
  2. Never
  3. Only with --tags cleanup (or --tags never)
  4. Only with --check

The play has hosts: web, you pass --limit db1. db1 is not in web. What runs?

  1. db1
  2. Nothing: the intersection is empty
  3. All web hosts
  4. An error about db1

Why put setup tasks under the always tag?

  1. They run faster
  2. They still run when someone uses --tags for a slice that depends on them
  3. They can't fail
  4. They run on every host