Learn how to use generative AI to generate, interpret and debug code and accelerate your workflows.
![](https://engineering.01cloud.com/wp-content/themes/magine_content/uploads/2024/02/image-46.png)
Infrastructure as Code (IaC) aids engineers in managing data, applications, and infrastructure in dynamic IT environments. Through a GitOps-driven approach, it ensures standardization, security, and operational consistency across various environments.
Generative artificial intelligence (AI) has emerged as a game-changer for IaC, transitioning from a best practice to an indispensable strategy. While OpenAI leads with tools like ChatGPT, other capable large language models (LLMs) like LLAMA by Meta offer diverse generative AI capabilities. Dell has published a white paper on constructing versatile LLM environments, showcasing the importance of these technologies.
Many AI tools can speed up your work, help you learn faster, and boost your career. Let me outline some of these capabilities.
Code Generation
If I had to decide between spending money on a monthly haircut or getting a subscription to a generative code assistant, I’d go for the latter. Tools like GitHub Copilot have become essential, even for simple tasks like shell scripting, not to mention crafting complex Ansible playbooks. They significantly speed up code creation for automation and ensure that each generated line of code comes with a meaningful comment. You can find numerous examples of how these tools aid in code generation in my Ansible for OpenManage Enterprise video.
Code Interpretation
Generative AI models possess the capability to analyze and comprehend code written in various programming languages, owing to their thorough training on millions of lines of code. When deciphering code, such as when refining code or enhancing existing automation scripts, providing the large language model (LLM) with code prompts can often be more straightforward than using plain English. Here’s a rapid illustration where I generate the Ansible counterpart of a traditional shell script using the latest ChatGPT 4 model.
The shell script input to generate a compressed backup of a directory is as follows:
#!/bin/bash
# Set the directory to back up and the backup location
DIRECTORY_TO_BACKUP="/home/user/data"
BACKUP_LOCATION="/backup"
TIMESTAMP=$(date +"%F")
BACKUP_FILE="backup-$TIMESTAMP.tar.gz"
# Create the backup
tar -czf $BACKUP_LOCATION/$BACKUP_FILE $DIRECTORY_TO_BACKUP
# Check if the backup was successful
if [ $? -eq 0 ]; then
echo "Backup completed successfully."
else
echo "Backup failed." >&2
fi
The initial Ansible output:
---
- name: Create Backup of Directory
hosts: all
tasks:
- name: Create backup file
ansible.builtin.archive:
path: /home/user/data
dest: /backup/backup-{{ ansible_date_time.iso8601_basic_short }}.tar.gz
format: gz
When I prompted the model for a more structured code block with variables, it returned:
---
- name: Create Backup of Directory with Variables
hosts: all
vars:
backup_source: /home/user/data # Define the source directory to back up
backup_destination: /backup # Define the backup destination directory
backup_filename: "backup-{{ ansible_date_time.iso8601_basic_short }}.tar.gz" # Backup filename with a timestamp
tasks:
- name: Create backup directory if it does not exist
ansible.builtin.file:
path: "{{ backup_destination }}"
state: directory
mode: '0755'
- name: Create backup file
ansible.builtin.archive:
path: "{{ backup_source }}"
dest: "{{ backup_destination }}/{{ backup_filename }}"
format: gz
The most recent ChatGPT interface enables file attachments, eliminating the need for manual copying and pasting of code for refactoring. Simply organize your files and attach them directly to your prompt.
Error Analysis and Debugging
If you believe that copying and pasting error messages onto Stack Overflow is the most effective method for debugging code, reconsider your approach. Code interpreter tools based on large language models (LLMs) can examine error messages and logs, often pinpointing the underlying causes of common issues. This capability extends to all types of code, including Ansible playbooks and Terraform modules, where an LLM can promptly suggest solutions, provide links to relevant documentation, or potentially automate the resolution process.
Prompt Engineering
Writing prompts has become a critical skill in determining an LLM’s response accuracy. The more specific and detailed your prompt, the more usable the response. Here are some IaC examples:
“I am working on a Terraform project where I need to provision an AWS EC2 instance with specific requirements: It should be of type ‘t2.micro’ within the ‘us-east-1’ region and include tags for ‘Name’ as ‘MyInstance’ and ‘Environment’ as ‘Development’. Could you provide me with the Terraform code snippet that defines this resource?”
Or:
“I need to create an Ansible playbook that performs a common operation: updating all packages on a group of Ubuntu servers. The playbook should be idempotent and only target servers in the ‘webservers’ group. It must also restart the ‘nginx’ service only if the updates require a restart. Can you generate the YAML code for this playbook?”
Create Your Own GPT (Global Project Tool)
If you’re accustomed to having a ChatGPT tab open in your browser and are skilled at crafting prompts, there’s a broader scope to explore with generative AI than just generating code.
With the recent release of GPT models and OpenAI’s Assistants API, you now have the chance to create a custom model tailored to your specific requirements. This customized model promises quicker and more precise responses. You can train GPT models using diverse inputs, such as policy documents, coding guidelines, or IT infrastructure sizing calculators. These trained models can then be employed by chatbots to handle inquiries from both customers and internal stakeholders. It’s essential to consider that there are associated costs with these capabilities, which vary depending on factors like the number of clients and usage.
Key elements in a custom GPT:
Code interpreter: Similar to the coding capabilities found in ChatGPT or GitHub Copilot, the code interpreter function in a custom GPT operates similarly. When crafting a custom GPT, provide users with the option to enable or disable the code interpreter feature. This flexibility aligns with the Assistants API’s pay-per-use pricing model, allowing users to opt out if they don’t require this functionality.
Knowledge retrieval: AI-driven knowledge retrieval systems swiftly fetch relevant technical documentation and best practices tailored to the current task, whether it’s developing an Ansible playbook or defining resources in Terraform. This instant access to information expedites the development process and ensures adherence to industry standards across various platforms.
Your own functions: If you’ve developed scripts or routines for calculations or decision-making processes, you can integrate them into your custom GPT. For instance, I recently witnessed the integration of a return on investment (ROI) calculator into a chatbot to assist website visitors in assessing the advantages of transitioning to solar power. You could develop tools like a sizer or performance benchmarking tool tailored to your target end user’s needs.
A Note of Caution about Proprietary and Sensitive Data
Although LLMs represent a significant advancement for programmers, it’s crucial to approach the training of AI models with caution, especially when using non-publicly available data. Depending on the context, implement thorough safeguards when handling sensitive or proprietary data in prompts or knowledge documents used for training. If your organization lacks such safeguards, take the initiative to establish them and contribute to enhancing your organization’s maturity in AI adoption.
Link to Article
https://thenewstack.io/generative-ai-tools-for-infrastructure-as-code/