Skip to main content
In this lesson you will use the Claude Code For Beginners CLI to generate and refine an Ansible playbook that installs and configures the Apache (httpd) web server on RHEL-based hosts. The walkthrough covers setting up a control-node workspace, generating a baseline playbook with Claude Code, refactoring to best practices (FQCNs, templates, lineinfile, handlers), validating, and running the playbook.
A presentation slide titled "Writing Playbook With Claude Code CLI" showing an illustration of a person at a computer. The slide notes testing Claude Code CLI’s ability to generate an Ansible playbook for Apache (httpd) setup.
Overview — workflow
  • Create a working directory and inventory
  • Configure ansible.cfg
  • Use Claude Code to generate site.yml (the playbook)
  • Validate and refactor the playbook (use FQCNs, add template, lineinfile, handlers)
  • Run ansible-lint / validate
  • Execute the playbook and verify the result
For quick reference, here’s the workflow in a compact table:
A dark-themed slide titled "Demo" listing six numbered steps. It outlines setting up a working directory, configuring the Claude Code CLI, generating and validating a playbook (site.yml), asking Claude to refactor it, and validating/executing the refactored playbook.
Preparation — create the control-node workspace Run these commands in your Claude Code workspace to create a working directory and files:
Example inventory (inventory)
Example ansible.cfg
Generate an initial playbook with Claude Code Start an interactive Claude Code session and ask it to create a playbook named site.yml that installs httpd on hosts in the webservers group (RHEL-based systems). Example CLI snippet:
Claude Code will typically generate a simple playbook like this. Open site.yml in your editor to inspect it. Initial generated site.yml
Refactor to use Fully Qualified Collection Names (FQCNs) Best practice: use FQCNs such as ansible.builtin.yum and ansible.builtin.service to avoid ambiguity and ensure the intended module is executed. Ask Claude Code to refactor the playbook to FQCNs or update it yourself. site.yml with FQCNs
Add a Jinja2 template to serve host facts Create a template that renders host-specific facts (hostname and fqdn) to /var/www/html/index.html. Example template (index.html.j2): index.html.j2
Deploy the template and use lineinfile for attribution Rather than embedding the attribution directly into the template, demonstrate combining modules: deploy the template using ansible.builtin.template, then add an attribution line using ansible.builtin.lineinfile. Notify a handler to reload httpd when the file changes. Tasks to add
Add the handler
Consolidated site.yml Below is the full playbook that combines installation, service management, template deployment, line insertion, and the handler.
Linting and validation Before running the playbook in production, consider running ansible-lint to catch common issues:
Execute the playbook Run the playbook from your control node:
Example playbook execution (trimmed)
Verify on the managed host Confirm the web page is served and contains the expected facts and attribution.
Wrapping up You have used Claude Code For Beginners to scaffold an Ansible playbook, refactored it to follow best practices (FQCNs, templates, handlers), and executed it against a managed RHEL-based host. This pattern—generate, inspect, refactor, and validate—helps you move quickly while keeping playbooks maintainable and predictable. Links and references
Use FQCNs (ansible.builtin.*) in playbooks to avoid ambiguity and ensure the intended module is executed.

Watch Video