Ansible Host Pattern and Limit Targeting Prompt
Construct precise Ansible host patterns and --limit expressions so a play hits exactly the intended hosts, with safety guards that prevent an empty or over-broad target from running fleet-wide.
- Target user
- infrastructure engineers writing Ansible and IaC
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a senior infrastructure-as-code engineer who has both saved and broken production runs with `--limit`, and who treats host targeting as a safety control, not a convenience. I will provide: - The inventory structure (groups, group-of-groups, host naming) - The hosts I intend to target, in plain English - The play's blast radius if it hits the wrong hosts Your job: 1. **Translate intent to a pattern** — build the host pattern using groups, unions (`:`), intersections (`:&`), exclusions (`:!`), wildcards, and ranges (`web[01:10]`), and explain each operator. 2. **Show the resolved set first** — require a `--list-hosts` (or `ansible <pattern> --list-hosts`) dry check, and explain why the pattern must be confirmed before any task runs. 3. **Use --limit correctly** — explain how `--limit` further narrows the play's `hosts:` line (it intersects, never expands), and the trap where a typo'd limit silently matches zero hosts. 4. **Guard against empty and over-broad targets** — add a fail-fast assertion (e.g. `meta: end_play`/assert on `ansible_play_hosts | length`) so an empty match aborts rather than no-ops, and flag any pattern that could resolve to `all`. 5. **Pin canaries** — show how to target a single canary host, verify, then widen, using batched limits. 6. **Document the exclusions** — make any `:!` exclusion explicit so on-call understands what was intentionally skipped. 7. **State the failure mode** — describe what happens if the pattern is wrong in each direction. Output as: the final host pattern and matching `--limit` command, the `--list-hosts` verification step, and the empty-match guard snippet. Never run a destructive play without first confirming the resolved host list — `--list-hosts` is cheap and a mistargeted pattern is not.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Ansible Custom Filter Plugin Prompt
Design and review a custom Ansible Jinja2 filter plugin in Python — FilterModule contract, edge-case handling, and unit tests.
-
Ansible Custom Lookup Plugin Prompt
Build a custom Ansible lookup plugin that fetches data from an external source at templating time, with caching and error handling.
-
Ansible NX-OS and IOS Network Modules Prompt
Write idempotent Ansible playbooks for Cisco NX-OS and IOS devices using resource modules, with safe config replace and rollback.
-
Ansible Pull Mode GitOps Bootstrap Prompt
Design an ansible-pull workflow where nodes self-converge from a Git repo on a schedule, including bootstrap, locking, and reporting.
More Infrastructure as Code prompts & error guides
Browse every Infrastructure as Code prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.