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. |
-
Click on the Ansible extension icon in the left sidebar to open the Ansible extension panel.
-
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:
-
Targets the
allhosts (includes both web and database groups) -
Uses privilege escalation (
become: true) -
Defines these variables:
-
user_name— set topadawan -
web_package— set tohttpd -
web_service— set tohttpd -
db_package— set tomariadb-server -
db_service— set tomariadb
-
-
Performs these tasks in order:
-
Create the user defined in
user_namewith a home directory (on all hosts) -
Install the package defined in
web_package(only on thewebgroup) -
Ensure the web service is running and enabled (only on the
webgroup) -
Deploy a Jinja2 template from
templates/index.html.j2to/var/www/html/index.htmlon the web group only -
Install the package defined in
db_package(only on thedatabasegroup) -
Ensure the database service is running and enabled (only on the
databasegroup) -
Deploy a Jinja2 template from
templates/motd.j2to/etc/motdon all hosts
-
|
When specifying conditionals, tell the coding assistant to use |
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:
This ensures the generated playbook matches what the validation system expects. |
-
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. |
-
The playbook will open in a new tab. Review the output to confirm it includes:
-
A
vars:section withuser_name,web_package,web_service,db_package, anddb_service -
Tasks with
when:conditionals targeting both thewebanddatabasegroups -
Two
template:tasks — one forindex.html.j2(web servers) and one formotd.j2(all hosts) -
A
handlers:section for restarting Apache
-
-
Save the file as
system_setup.ymlin 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 |
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
button to proceed to the next challenge.
-
The
button will validate your steps. If anything is missing, an error will appear so you can correct it before proceeding. -
You can also click
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.