Introduction

Red Hat's Ansible VS Code extension now embeds a Model Context Protocol (MCP) server that enables AI-assisted playbook development. Write automation in natural language, get context-aware linting, and scaffold compliant playbooks — all inside your editor.

Features

Natural Language to Playbook

Prompt: "Create a playbook to deploy nginx with SSL on Ubuntu, 
         enable HTTP/2, and set up log rotation"

Generated:
---
- name: Deploy Nginx with SSL and HTTP/2
  hosts: webservers
  become: true
  vars:
    domain: example.com
    ssl_cert_path: /etc/ssl/certs/{{ domain }}.pem
    ssl_key_path: /etc/ssl/private/{{ domain }}.key

  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        state: present
        update_cache: true

    - name: Deploy SSL configuration
      ansible.builtin.template:
        src: nginx-ssl.conf.j2
        dest: /etc/nginx/sites-available/{{ domain }}
      notify: Reload nginx

    - name: Configure log rotation
      ansible.builtin.copy:
        content: |
          /var/log/nginx/*.log {
              daily
              rotate 14
              compress
              delaycompress
              missingok
              notifempty
          }
        dest: /etc/logrotate.d/nginx

  handlers:
    - name: Reload nginx
      ansible.builtin.service:
        name: nginx
        state: reloaded

Context-Aware Linting

The MCP server provides:

  • FQCN suggestions — auto-complete fully qualified collection names
  • Deprecation warnings — flag modules marked for removal
  • Best practice hints — suggest handlers over inline restarts
  • Security checks — warn on no_log: false for sensitive tasks

Compliant-by-Design Scaffolding

Prompt: "Scaffold a role for CIS Level 1 RHEL 9 hardening"

Generated role structure:
roles/cis_hardening/
├── defaults/main.yml    # CIS control toggles
├── tasks/
│   ├── main.yml
│   ├── section1_initial_setup.yml
│   ├── section2_services.yml
│   ├── section3_network.yml
│   ├── section4_logging.yml
│   └── section5_access.yml
├── handlers/main.yml
├── meta/main.yml        # Galaxy metadata
└── molecule/            # Pre-configured tests
    └── default/
        └── converge.yml

Installation

# Install from VS Code marketplace
code --install-extension redhat.ansible

# Or search in Extensions panel:
# "Ansible" by Red Hat

Enable MCP Server

// .vscode/settings.json
{
  "ansible.lightspeed.enabled": true,
  "ansible.lightspeed.URL": "https://c.ai.ansible.com",
  "ansible.mcp.enabled": true,
  "ansible.mcp.aapUrl": "https://controller.example.com",
  "ansible.mcp.aapToken": "${env:AAP_TOKEN}"
}

AI-Assisted Workflows

1. Explain Existing Playbook

Select code → Right-click → "Ansible: Explain This"

2. Fix Linting Issues

AI suggests fixes for ansible-lint violations with one-click apply.

3. Generate Tests

"Generate Molecule tests for this role" — creates converge.yml, verify.yml, and assertions.

4. Refactor to Role

Select inline tasks → "Ansible: Extract to Role" — creates full role structure.

MCP Tool Integration

The embedded MCP server exposes:

ToolDescription
ansible.lintRun ansible-lint with AI-enhanced suggestions
ansible.explainExplain playbook/task behavior
ansible.generateNatural language to YAML
ansible.refactorExtract roles, optimize loops
ansible.testGenerate Molecule test scenarios
ansible.docsContext-aware documentation lookup

Best Practices

  1. Review AI output — always validate generated YAML before committing
  2. Use with ansible-lint — AI generation + linting = quality assurance
  3. Connect to AAP — enables inventory-aware suggestions
  4. Set organization standards — configure custom rules for AI to follow
  5. Iterate with prompts — refine generated code through conversation

Conclusion

The Ansible VS Code extension with MCP transforms playbook development from manual YAML editing to AI-assisted automation engineering. Natural language generation, intelligent linting, and compliant scaffolding make it the most productive way to write Ansible automation in 2026.