AI-Powered Playbook Generation Intermediate

The most immediate benefit of combining AI with Ansible is the ability to generate complete, production-quality playbooks from natural language descriptions. Instead of spending hours writing YAML and looking up module parameters, you describe what you need and let the AI handle the syntax.

Prompt Engineering for Playbooks

The quality of AI-generated playbooks depends heavily on how you frame your prompts. A well-structured prompt should include the target platform, desired outcome, any constraints, and the expected variable structure.

Prompt Example
# Effective prompt for playbook generation:

Generate an Ansible playbook that:
- Target: Cisco IOS-XE routers (using cisco.ios collection)
- Task: Configure OSPF area 0 on all interfaces in the 10.0.0.0/8 range
- Requirements:
  - Use BFD for fast failure detection
  - Set hello interval to 10, dead interval to 40
  - Enable MD5 authentication with key "{{ ospf_auth_key }}"
  - Include pre-check to verify current routing table
  - Add handlers for service restart if needed
  - Use ansible-vault for sensitive variables
- Output: Complete playbook with inventory example

Generating a VLAN Configuration Playbook

Let us walk through a practical example of generating a VLAN management playbook using AI.

YAML
# AI-generated playbook: VLAN configuration for Cisco switches
---
- name: Configure VLANs on access switches
  hosts: access_switches
  gather_facts: no
  vars_files:
    - vars/vlan_definitions.yml

  tasks:
    - name: Backup current configuration
      cisco.ios.ios_config:
        backup: yes
        backup_options:
          dir_path: ./backups/{{ inventory_hostname }}

    - name: Create VLANs
      cisco.ios.ios_vlans:
        config: "{{ vlans }}"
        state: merged
      register: vlan_result

    - name: Assign VLANs to interfaces
      cisco.ios.ios_l2_interfaces:
        config: "{{ l2_interfaces }}"
        state: merged

    - name: Verify VLAN creation
      cisco.ios.ios_command:
        commands:
          - show vlan brief
      register: vlan_verify

    - name: Save configuration
      cisco.ios.ios_config:
        save_when: modified

Multi-Vendor Playbook Generation

AI excels at generating playbooks that work across multiple vendor platforms. By providing context about your multi-vendor environment, the AI can produce playbooks with appropriate module selection and platform-specific handling.

Pro Tip: When generating multi-vendor playbooks, include the Ansible collections you have installed and your inventory group structure. This helps the AI produce playbooks that match your existing automation framework.

Iterative Refinement

Rarely will the first AI-generated playbook be perfect. Use an iterative approach:

  1. Generate the initial playbook

    Start with a clear, detailed prompt describing your requirements.

  2. Review and identify gaps

    Check for missing error handling, incorrect module usage, or security issues.

  3. Refine with follow-up prompts

    Ask the AI to add specific improvements: "Add error handling for unreachable hosts" or "Include idempotency checks."

  4. Test in a lab environment

    Always test AI-generated playbooks against non-production devices first.

Common Pitfalls

Watch Out: AI models may generate playbooks using deprecated modules or incorrect parameter names. Always verify module documentation and test thoroughly before deploying to production.
PitfallImpactMitigation
Deprecated modulesPlaybook fails or produces warningsVerify against current Ansible docs
Wrong collectionModule not found errorsSpecify exact collection in prompt
Missing error handlingPartial config changes on failureAlways request rescue/always blocks
Hardcoded credentialsSecurity vulnerabilityRequest vault-encrypted variables

Try It Yourself

Open your preferred AI assistant and try generating an Ansible playbook for configuring BGP peering between two routers. Include authentication, route filtering, and verification tasks.

Next: Config Validation →