Skip to main content

Ansible Playbook Generator

Generate Ansible playbooks with roles, handlers, templates, vault-encrypted secrets, and idempotent task design for server configuration.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
Generate Ansible playbooks for deploying and configuring a high-availability web application stack targeting web servers, app servers, and database servers running Ubuntu 22.04 LTS. The infrastructure has 12 hosts across webservers, appservers, dbservers, monitoring. Create: 1) Inventory structure using YAML inventory with children groups with groups webservers, appservers, dbservers, monitoring, group_vars for shared configuration, and host_vars for host-specific overrides. 2) A role (webapp) with: tasks/main.yml broken into tagged includes (install, configure, deploy, verify), handlers for restart nginx, reload app, update firewall rules, templates for nginx.conf.j2, app-config.yml.j2, systemd-service.j2 using Jinja2 with proper variable fallbacks, and defaults/main.yml with documented default values. 3) Ansible Vault integration: encrypt database_password, api_keys, ssl_certificate_key using vault, configure vault-password-file for CI, and use !vault | for inline encrypted strings in group_vars. 4) Idempotent task design: use proper modules (instead of shell/command), register results and use changed_when/failed_when for app health endpoint returns 200, database accepts connections, SSL certificate is valid, and implement check mode (--check) compatibility. 5) Error handling: block/rescue/always for deployment fails mid-rollout, config renders invalid, service fails to start, with rescue tasks that rollback to previous version, restore config backup, alert ops team and always tasks that log outcomes. 6) A site.yml playbook that orchestrates roles in proper order with serial: 1 for first host, then 25% at a time rolling deployment strategy and pre_tasks for verify disk space, check connectivity to artifact repository, ensure no other deployment in progress. 7) Molecule testing for the role with Docker driver and Testinfra verification.

What this prompt does

This prompt generates Ansible playbooks for [automation_goal] targeting [target_hosts] running [target_os], across [host_count] hosts organized into [inventory_groups]. It builds an inventory in [inventory_format] with group_vars for shared config and host_vars for overrides, then a role named [role_name] whose tasks/main.yml is split into tagged includes (install, configure, deploy, verify), with handlers for [handler_triggers], Jinja2 templates for [template_files], and a documented defaults/main.yml.

It integrates Ansible Vault to encrypt [secret_variables], configures a vault-password-file for CI, and uses inline encrypted strings in group_vars. The emphasis is idempotency: proper modules over shell/command, changed_when/failed_when for [custom_status_checks], and check-mode (--check) compatibility. It adds block/rescue/always error handling for [failure_scenarios] where the rescue tasks run [recovery_actions] and the always tasks log outcomes, a site.yml orchestrating roles with a [serial_strategy] rolling strategy and [pre_checks] pre-tasks, and Molecule testing with a [test_driver] driver plus Testinfra verification. The playbooks that hold up over time are the idempotent ones, which is why the prompt leans so hard on proper modules and honest status checks.

When to use it

  • You're configuring Linux servers and want repeatable, idempotent runs
  • You need secrets encrypted at rest with Ansible Vault, including in CI
  • You want a clean role structure with tagged task phases and handlers
  • You need rolling deployments that update hosts in controlled batches
  • You want block/rescue error handling with automatic rollback on failure
  • You want the role tested with Molecule before it touches real servers
  • You're sharing roles across a team and want documented defaults and clear tags

Example output

The AI returns an inventory file, a role directory (tasks, handlers, templates, defaults), vault-encrypted variable examples, idempotent task definitions with changed_when/failed_when, block/rescue/always structures for [failure_scenarios], a site.yml with the [serial_strategy] and [pre_checks], and a Molecule scenario using [test_driver] with Testinfra checks. Expect YAML grouped per numbered section so you can adopt the role, the vault setup, or the error handling on their own.

Pro tips

  • Prefer real modules over shell/command for everything in [automation_goal], since shell commands rarely report changed state correctly
  • Encrypt every secret in [secret_variables] with Vault and point CI at a vault-password-file rather than committing the password
  • Use changed_when and failed_when for [custom_status_checks] so tasks report status honestly and stay idempotent
  • Set [serial_strategy] conservatively (one host, then a percentage) so a bad deploy doesn't hit the whole fleet at once
  • Put recovery logic from [recovery_actions] in rescue blocks so a mid-rollout failure rolls back instead of leaving hosts broken
  • Run the [pre_checks] pre-tasks first so you fail fast on disk space or connectivity before touching production
  • Run the Molecule [test_driver] scenario in CI so role regressions are caught before production

Frequently Asked Questions

How does the prompt keep playbooks idempotent?
It favors proper Ansible modules over raw shell or command tasks, and uses `changed_when` and `failed_when` for `[custom_status_checks]` so each task reports its real state. It also asks for check-mode (--check) compatibility, so you can preview changes without applying them.
Where are secrets like the database password stored?
Secrets in `[secret_variables]` are encrypted with Ansible Vault, either as inline encrypted strings in group_vars or in vault files. For CI, a vault-password-file is configured so pipelines can decrypt without anyone committing the password to the repo.
Does it support rolling deployments rather than updating all hosts at once?
Yes. The site.yml uses a `[serial_strategy]`, such as one host first then 25% at a time, so updates roll out in controlled batches. This limits the blast radius if a deployment goes wrong partway through.
What happens if a deployment fails midway?
The prompt wraps risky steps in block/rescue/always for `[failure_scenarios]`. The rescue section runs `[recovery_actions]` like rolling back to the previous version or restoring a config backup, and the always section logs the outcome, so a partial failure doesn't leave hosts in a broken state.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Terraform & Infrastructure as Code Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support