Skip to main content
In this lesson you’ll add Claude Code to an Ansible workflow so you can generate ad-hoc commands and playbooks directly from the terminal. Claude Code is a lightweight CLI client for Anthropic’s Claude models that connects to the Claude API and helps developers author scripts, playbooks, and commands without leaving their shell.
A presentation slide titled "Integrating Claude Code CLI" with a central circular logo and three numbered points: "Built for developers," "Works directly from the terminal," and "Generates Ansible playbooks."
Target audience: systems engineers and DevOps practitioners managing Linux servers across multiple environments who want to pilot AI-assisted automation to speed up routine tasks.
The image is a presentation slide titled "Integrating Claude Code CLI" showing an illustration of a person working on a laptop with a chat/code window. To the right are three feature bullets: "Lightweight local CLI," "Secure API connection," and "Powered by Claude 3 models."
What you will do in this demo:
  • Verify required system packages and environment
  • Install the Claude Code CLI
  • Authenticate the CLI (interactive browser flow)
  • Validate Claude-generated Ansible ad-hoc commands and playbooks
A dark-themed presentation slide titled "Demo" showing a two-column, numbered list of steps: verify required system packages, install Claude Code CLI, authenticate Claude Code CLI, and validate playbook and Ansible ad-hoc command generation.

1. Prepare the VM and shell

Switch to the student virtual machine and ensure you are in the home directory:

2. Install Claude Code CLI

Install the CLI using the official installer script (curl piped to bash):
Sample installer output:

3. Authenticate the CLI

Start the interactive login flow:
The CLI presents the login options:
Choose the appropriate method. In this demo the user selects a Claude account and authenticates via Google; a browser window opens for the OAuth flow.
A computer desktop screenshot showing a Firefox browser open to the Claude.ai login page with a Google sign-in popup window loading. A smaller dialog on the page prompts the user to connect using a Google account (the prompt text appears in Romanian).
After completing the browser-based login, the CLI displays security notes describing model limitations and guidance.
A dark terminal-style screen displaying "Security notes" about Claude — warning that Claude can make mistakes and advising caution with code and prompt injection, plus a link to documentation. An orange ASCII-art character appears at the top left and a "Press Enter to continue…" prompt is shown.
Claude models can make mistakes and prompts might include unsafe instructions. Be cautious when executing generated code or granting filesystem access.
Allow the CLI the requested workspace permissions, complete the flow, and return to the shell. If the process is interrupted you may see:
Re-run claude login to retry if needed.

4. Quick verification: ask Claude for Ansible ad-hoc commands

Try a simple prompt to generate an Ansible ad-hoc ping command for your inventory:
Claude typically returns several valid variations. Common examples include: Note on the Ansible ping module:
  • The ping module does not send ICMP packets; it executes a small Python task on the remote and returns “pong” on success. It verifies:
    • SSH connectivity
    • SSH authentication
    • Python availability on the remote host
If your inventory doesn’t match Claude’s suggested pattern, Ansible will warn that the host pattern couldn’t be matched:
Inspect your workspace inventory to confirm host groups:

5. Interactive mode: compose playbooks with natural language

Run claude without flags to enter interactive mode. Use plain language or slash commands to control behavior and persist prompts:
Ask Claude to generate a small Ansible playbook. Example prompt:
Claude may produce a draft like this:
If you intended to target the webservers host group (not create a system group), update the playbook to set hosts: webservers and remove the group-creation task. Corrected example:
Save the playbook (for example, create_user.yml) and run it against your inventory:
This demonstrates how Claude Code can draft useful Ansible content that you then review and refine before applying to your environment.

Troubleshooting tips

  • If claude is not found after installation, ensure ~/.local/bin is in your PATH:
  • If browser login fails, try the alternate login method (Anthropic Console) or re-run claude login.
  • Always review generated code for security, prompt injection, and correctness before executing.

Helpful commands summary

This concludes the demo. You have installed and authenticated the Claude Code CLI, used it to generate Ansible ad-hoc commands and a playbook, and learned how to refine and run the output in your environment. Congratulations!

Watch Video