Using AI to Develop Ansible Playbooks

All Articles Culture Data Management Level 12 News Python Salesforce Software Development Testing

Recently, I needed to write some new Ansible scripts to deploy a project to the server. As usual for a developer who spends most of his time figuring out business logic, writing code to match it, and writing tests to prove it, I don't spend a ton of time with Ansible.

This time, instead of digging through documentation first, I tried something new: describe the goal of the task to ChatGPT and have AI give me a playbook as a starting point.

I found that writing an initial Ansible playbook is an ideal task for generative AI. In terms of language, Ansible playbooks have a much simpler grammar than the English language as a whole. The number of possible modules to use is also quite limited.

AI, Ansible, and the Consultant

What the LLM gives us is something much closer to the result than a blank file. That means we have a choice in the types of development tasks we can take on.

For those of us billing by the hour or keeping an eye on capacity, our clients appreciate the efficiency when we use AI to do what AI can do.

As a developer who understands his finitude, I appreciate having a tool that allows me to reserve my brain cycles for the more interesting problems.

Caveats with AI and Playbooks

Inevitably, the model gives us a starting point that is close to, but not quite, what we need. We could spend time doing manual adjustments. Often, though, we can lean on the LLM's conversational ability, clarify the initial request, and have the model update the output accordingly.

I say “starting point” because AI doesn't know what it doesn't know. Just as I need to be careful when developing my own Ansible playbooks, I'm not about to run just any AI-generated playbook on a server.

The upside is that I don't have to spend the time figuring out what module or what options. The model has already ingested all of that information, and it can generate output that matches.

When reviewing the output before modifying it to suit the intended purpose and running it on a server, it certainly helps to be a trained, experienced developer who knows something about Ansible. This expertise is necessary to avoid doing something unintentionally deleterious with the playbook.

As with any scripting solution, Ansible has several inherent footguns that could be pulled out at any time. Care is needed to ensure I'm not wiping out configuration that I don't want to, applying a bad set of file permissions, or installing the wrong package.

Automate Ansible with AI

Without AI, I know enough to do a thing or two without looking at the documentation. But for most situations, I have to go in assuming there's a way to do it, and then I have to find that way.

I'll spend time determining what Ansible module best fits the problem at hand. Then, I'll need to look over all of the options for the module, figure out which set I need, and script up the playbook.

It's not just one and done at that point, either. Ansible scripts have a way of getting part way through, then throwing a semi-obscure error - now I need to spend some time in the trial-and-error phase of troubleshooting this thing until it gets it right.

The payoff, of course, is having a reusable playbook to automate a task. It makes sense, and helps both us and clients, to leverage AI to automate the playbook process.

Originally published on 2025-02-13 by Matt Lewellyn

Reach out to us to discuss your complex deployment needs (or to chat about Star Trek)