DEV Community

Cover image for AI-Powered Ansible Playbooks: The future of automation
XLAB Steampunk
XLAB Steampunk

Posted on • Originally published at steampunk.si

AI-Powered Ansible Playbooks: The future of automation

Let’s explore the exciting realm of AI and its impact on creating high-quality, secure, reliable production-ready Ansible Playbooks. Within this blog post, we highlight the pivotal role played by tools like Ansible Lint and Steampunk Spotter in ensuring the quality and trustworthiness of Ansible content while using AI tools like ChatGPT.

Exploring the potential of AI in production-ready Infrastructure as Code

With the rise of ChatGPT, GitHub Copilot, Amazon CodeWhisperer, Ansible Lightspeed, and other AI tools, the demand for faster and more efficient content generation is growing. However, a question remains: Is AI advanced and reliable enough to produce production-ready Infrastructure as Code, particularly in the case of Ansible Playbooks?

We can achieve favorable outcomes for Ansible and similar tools with the widely recognized ChatGPT. Our objective is to ensure that our playbooks are production-ready and characterized by exceptional quality, reliability, security, and trustworthiness. We can utilize additional tools such as Ansible Lint and Steampunk Spotter to validate our examples.

Building an Ansible Playbook with ChatGPT and Spotter

Let’s embark on a quest to create an Ansible Playbook for setting up an application. Using Ansible, we aim to deploy a Hello World web page within an nginx Docker container. By providing specific requirements to ChatGPT, we obtain a playbook and instructions generated for us:

--- - name: Deploy web page using Ansible and Docker hosts: localhost gather_facts: false become: false tasks: - name: Create temporary directory tempfile: state: directory prefix: web_deploy_ dir: /tmp register: temp_dir - name: Copy index.html file copy: content: | <html> <head> <title>Hello World!</title> </head> <body> <h1>Hello world! I was deployed using Ansible.</h1> </body> </html> dest: "{{ temp_dir.path }}/index.html" - name: Create Docker container for web page docker_container: name: my-web-page image: nginx state: started ports: - "80:80" volumes: - "{{ temp_dir.path }}:/usr/share/nginx/html" - name: Wait for Nginx to start uri: url: "http://localhost/" return_content: yes register: response until: response.status == 200 retries: 10 delay: 2 - name: Display website URL debug: msg: "Web page deployed successfully! Access it at http://localhost/" 
Enter fullscreen mode Exit fullscreen mode

To ensure a high quality and reliability standard, Steampunk Spotter can help. Spotter is an Ansible Playbook Scanning tool that analyzes and offers recommendations for your playbooks.

We scan the playbook and determine there are unsupported parameters. In this scan, we allow Spotter to scan parameter values in full scan profile mode, including all the checks.

It carefully scans the playbook and highlights various issues, including fully qualified collection names (FQCNs), invalid and deprecated parameters, default parameter value changes, and best practice recommendations. Spotter proves invaluable in maintaining our desired level of excellence, as ChatGPT was not completely correct on this one.

Image description

Refining the playbook with Spotter and ChatGPT

Spotter not only detects issues but also provides automated fixes. Utilizing the spotter scan --rewrite command, we can automatically correct FQCN errors within our playbook. It also generates a requirements.yml file, ensuring compatibility between the version of the Ansible collection and the specific Ansible installation on our system.

For any remaining issues that require manual intervention, we turn to ChatGPT for assistance. By establishing a feedback loop with ChatGPT, we gradually achieve improved results. By incorporating Spotter’s output into ChatGPT, we iteratively enhance the playbook, effectively addressing the remaining errors.

We are still determining if the playbook is correct, so we “Spotter it out” again. We scan and find some new and existing results. We take the results and feed them back to ChatGPT and finally, we arrive at an updated playbook.

Image description

Leveraging Ansible Lint and finalizing the Playbook

While Steampunk Spotter goes beyond syntax checks and gives us in-depth feedback on our playbook, to ensure an additional level of quality of our syntax, we also subject the refined playbook to a scan using Ansible Lint. Utilizing its reformatting function, we can change the file according to the recommended guidelines. Ansible Lint proves invaluable by adding the necessary quotes to address issues with setting file permissions.

Our playbook reaches its final stage after multiple iterations involving ChatGPT, Spotter, and Lint. Spotter’s scan validates the playbook’s readiness, and we are now fully prepared to execute it confidently.

--- - name: Deploy web page using Ansible and Docker hosts: localhost gather_facts: false become: false tasks: - name: Create temporary directory ansible.builtin.tempfile: state: directory prefix: web_deploy_ register: temp_dir - name: Set permissions for temporary directory ansible.builtin.file: path: "{{ temp_dir.path }}" mode: '0755' - name: Copy index.html file ansible.builtin.copy: content: | <html> <head> <title>Hello World!</title> </head> <body> <h1>Hello world! I was deployed using Ansible.</h1> </body> </html> dest: "{{ temp_dir.path }}/index.html" mode: '0644' - name: Create Docker container for web page community.docker.docker_container: name: my-web-page image: nginx state: started published_ports: 80:80 volumes: - "{{ temp_dir.path }}:/usr/share/nginx/html" - name: Wait for Nginx to start ansible.builtin.uri: url: http://localhost/ return_content: true force: false use_proxy: true validate_certs: true force_basic_auth: false register: response until: response.status == 200 retries: 10 delay: 2 
Enter fullscreen mode Exit fullscreen mode

The role of AI tools and the importance of validation

While current AI tools may still need to be fully equipped to create production-ready Ansible Playbooks, there is a clear need to develop specialized tools with narrow intelligence. These generative AI tools and AI assistants hold great promise in delivering more accurate and tailored results for specific domains or purposes, such as learning Ansible or crafting complex playbooks. Expanding the capabilities of general-purpose AI solutions like ChatGPT by incorporating the option to customize results for specific queries can open many possibilities.

Tools like Ansible Lint and Steampunk Spotter are crucial to delivering high-quality, reliable, secure, and trustworthy Ansible content. These tools play a vital role in identifying issues and potential vulnerabilities in playbooks, and the playbooks and rules used to auto-remediate them can serve as excellent training examples for AI models. As we continue to explore the potential of AI in Ansible automation, it is evident that a collaborative effort between human expertise and AI capabilities is the way forward.

Image description

For more details and information on this topic, you can check our free webinar: AI-Powered Ansible Playbooks: Transforming the future of automation

With AI’s might and Spotter’s sight, Ansible automation can take flight. We invite you to try Steampunk Spotter!

Top comments (0)