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.
# 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.
# 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.
Iterative Refinement
Rarely will the first AI-generated playbook be perfect. Use an iterative approach:
- Generate the initial playbook
Start with a clear, detailed prompt describing your requirements.
- Review and identify gaps
Check for missing error handling, incorrect module usage, or security issues.
- Refine with follow-up prompts
Ask the AI to add specific improvements: "Add error handling for unreachable hosts" or "Include idempotency checks."
- Test in a lab environment
Always test AI-generated playbooks against non-production devices first.
Common Pitfalls
| Pitfall | Impact | Mitigation |
|---|---|---|
| Deprecated modules | Playbook fails or produces warnings | Verify against current Ansible docs |
| Wrong collection | Module not found errors | Specify exact collection in prompt |
| Missing error handling | Partial config changes on failure | Always request rescue/always blocks |
| Hardcoded credentials | Security vulnerability | Request 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 →
Lilly Tech Systems