Using ChatGPT to iteratively diagnose and fix a broken Ansible playbook, correcting modules, syntax, and service configuration while following Ansible best practices.
This guide demonstrates a practical workflow for diagnosing and fixing a broken Ansible playbook by running it, collecting real error output, and iterating with ChatGPT. The goal is to correct syntax, module usage, and variable issues while following Ansible best practices.What you’ll learn:
How to run a broken playbook and capture errors
Which common mistakes cause playbooks to fail
How to iterate with ChatGPT (or another LLM) to produce a corrected playbook
Best practices: FQCNs, correct service names, and privilege escalation
You joined a DevOps team that uses Ansible. Playbooks were written at different times by different people (and sometimes generated by AI). Many fail on first run or show syntax errors. Your task: take a broken playbook, run it, gather errors, and iterate with ChatGPT until the playbook runs successfully on the target hosts.
Create a deliberately buggy playbook called site.yml (or site.yaml) and iterate until fixed.Open the repository in an editor (VS Code) to inspect and edit site.yml.
When saving, linting and editor diagnostics will highlight obvious YAML issues. Copy the broken playbook and paste it into ChatGPT, asking for issues and corrections.I pasted the following broken playbook into ChatGPT:
I used a prompt like:
“This is an Ansible playbook with problems during execution. Please identify the issues and fix all possible problems.”Here’s the ChatGPT interface I used (for context):
After iterating with ChatGPT, incorporating the correct context (RHEL target, need for privilege escalation, best practices), we consolidated a single corrected playbook. It uses FQCNs, become: true, correct service/module names, and properly configured handlers.
Copy
---- name: Install and configure Apache on RHEL systems hosts: webservers become: true # Elevated privileges for tasks that require it vars: page_title: "Hello from Ansible Best Practices" tasks: - name: Ensure Apache is installed ansible.builtin.dnf: name: httpd state: present notify: restart apache - name: Deploy index.html file ansible.builtin.copy: content: "<html><body><h1>{{ page_title }}</h1></body></html>" dest: /var/www/html/index.html owner: apache group: apache mode: '0644' notify: restart apache - name: Ensure Apache service is running and enabled ansible.builtin.service: name: httpd state: started enabled: true - name: Add a line to index.html ansible.builtin.lineinfile: path: /var/www/html/index.html line: "Edited by Ansible" state: present insertafter: EOF notify: restart apache handlers: - name: restart apache ansible.builtin.service: name: httpd state: restarted
Always verify the environment (OS/distribution), service names, and file ownership before applying changes to production systems. Run playbooks against a non-production or test host first.