1.1 - Generate a Comprehensive Playbook with the Coding Assistant

Your team manages a set of web servers and a database server. In this challenge, you will use the Red Hat Ansible VS Code extension’s Automation Coding Assistant to generate a complete playbook that sets up the web tier with Apache and a status page, configures MariaDB on the database tier, and applies common settings across all hosts — all from a single well-structured prompt.

Instead of writing YAML by hand or iterating through multiple attempts, you will learn how to construct a comprehensive prompt that gives the coding assistant all the context it needs to generate exactly what you want in one shot.

1. Understanding Comprehensive Prompts

💡 What makes a good LLM prompt for playbook generation?

When prompting the coding assistant to generate a playbook, specificity and structure produce better results than vague requests. A comprehensive prompt should include:

  • Target hosts — which inventory group or specific hosts

  • Required privilege escalation — does it need become: true?

  • Variables to define — reusable values like usernames, package names, service names

  • Tasks in logical order — what to install, configure, start, and verify

  • Conditionals — which tasks run only on specific host groups

  • Templates — dynamic configuration files that use Jinja2 and host facts

The more detail you provide, the more accurate the coding assistant’s output will be.

2. Hands-On Experience

☑️ Task 1 - Open the Playbook Generator

Switch to the VS Code tab at the top of the screen.

  1. Click on the Ansible extension icon in the left sidebar to open the Ansible extension panel.

  2. Click the "Generate a Playbook with Ansible" button in the extension panel. This opens the playbook generation wizard where you will paste your prompt in the next task.

☑️ Task 2 - Construct and submit the comprehensive prompt

Stay in the VS Code tab.

You will construct a prompt that asks the coding assistant to generate a playbook covering your team’s infrastructure needs. Your prompt should request a playbook that:

  1. Targets the all hosts (includes both web and database groups)

  2. Uses privilege escalation (become: true)

  3. Defines these variables:

    • user_name — set to padawan

    • web_package — set to httpd

    • web_service — set to httpd

    • db_package — set to mariadb-server

    • db_service — set to mariadb

  4. Performs these tasks in order:

    1. Create the user defined in user_name with a home directory (on all hosts)

    2. Install the package defined in web_package (only on the web group)

    3. Ensure the web service is running and enabled (only on the web group)

    4. Deploy a Jinja2 template from templates/index.html.j2 to /var/www/html/index.html on the web group only

    5. Install the package defined in db_package (only on the database group)

    6. Ensure the database service is running and enabled (only on the database group)

    7. Deploy a Jinja2 template from templates/motd.j2 to /etc/motd on all hosts

When specifying conditionals, tell the coding assistant to use when: inventory_hostname in groups['web'] for web-only tasks and when: inventory_hostname in groups['database'] for database-only tasks.

Use this prompt (copy it exactly):

Create an Ansible playbook named "Web and Database Infrastructure Setup" that targets all hosts and uses privilege escalation.

Define variables for user_name (padawan), web_package (httpd), web_service (httpd), db_package (mariadb-server), and db_service (mariadb).

Include these tasks in order:
1. Create a user with the name from user_name variable, ensuring a home directory is created
2. Install the package from web_package variable, only on the web group using the conditional: when: inventory_hostname in groups['web']
3. Ensure the web service (from web_service) is running and enabled, only on the web group using the same conditional
4. Deploy a Jinja2 template from templates/index.html.j2 to /var/www/html/index.html, only on the web group using the same conditional
5. Install the package from db_package variable, only on the database group using the conditional: when: inventory_hostname in groups['database']
6. Ensure the database service (from db_service) is running and enabled, only on the database group using the same conditional
7. Deploy a Jinja2 template from templates/motd.j2 to /etc/motd on all hosts

Include a handler to restart Apache when needed, and use ansible.builtin modules throughout.

Copy the prompt above exactly as shown. The coding assistant works best with specific, detailed prompts that include:

  • The exact conditional syntax to use (when: inventory_hostname in groups['web'] and when: inventory_hostname in groups['database'])

  • Module family preference (ansible.builtin modules)

  • Handler requirements

This ensures the generated playbook matches what the validation system expects.

  1. Paste the prompt above into the playbook generator you opened in Task 1.

After you paste the prompt, the coding assistant will show a simplified version as a list of suggested steps. This is expected — the extension summarizes your prompt for its step-by-step review. You can edit, reorder, add, or remove steps in this list before generating the playbook. For this lab, the defaults should be fine — review the suggested steps and click "Create Playbook" to proceed.

  1. The playbook will open in a new tab. Review the output to confirm it includes:

    • A vars: section with user_name, web_package, web_service, db_package, and db_service

    • Tasks with when: conditionals targeting both the web and database groups

    • Two template: tasks — one for index.html.j2 (web servers) and one for motd.j2 (all hosts)

    • A handlers: section for restarting Apache

  2. Save the file as system_setup.yml in the /home/rhel/ansible-files/ directory:

    • Press Ctrl+S

    • Navigate to /home/rhel/ansible-files/

    • Name the file system_setup.yml

    • Click Save

Always review AI-generated code before using it. Large language models are powerful tools, but they do not guarantee correct output every time. The generated playbook may contain subtle issues — wrong module parameters, missing options, or logic that doesn’t match your intent. Treat LLM output as a strong first draft that still requires human review, not as production-ready code.

If the coding assistant’s output is missing a required section (vars, handlers, conditionals, or template task), you can click the Solve button to get a working version, then compare it to what you generated to understand what was different.

3. Learning Outcomes

By completing this module, you now understand:

  • How to construct a comprehensive LLM prompt that produces accurate, production-ready playbooks

  • Why specificity matters when prompting the coding assistant (variables, conditionals, templates)

  • How to use the coding assistant’s generation feature to create a full playbook from a single prompt

  • The structure of a real-world playbook that manages web and database infrastructure across different server groups

4. Embracing the Next Challenge

✅ Next Challenge

Once you have completed the tasks, press the Next button to proceed to the next challenge.

  • The Next button will validate your steps. If anything is missing, an error will appear so you can correct it before proceeding.

  • You can also click Solve to auto-complete the challenge.

🐛 Encountered an issue?

If you have encountered an issue or noticed something not quite right, please open an issue on the Introduction to automation coding assistant repository.