Ansible Lightspeed is a generative AI tool that provides an efficient way for developers to create Ansible content and automation tasks for Ansible playbooks. In this article, we will explore how to install and use Ansible Lightspeed in Visual Studio Code. This makes it easy to create Ansible content; you can simply type in plain English descriptions and Ansible Lightspeed will generate the code recommendations for you.
Prerequisites
Before proceeding through this tutorial, make sure you have installed the following:
- Visual Studio Code (VS Code)
- Python 3.9+
- Ansible Automation Platform 2.x (see How to install Ansible Automation Platform 2.4 on RHEL 9.1).
1. Install Ansible extension in VS Code
The Ansible extension offers intelligent code completion, syntax highlighting, and error checking for YAML and Ansible-specific files. The extension allows users to execute Ansible tasks and playbooks directly from the editor and view the real-time output. With built-in support for Ansible Vault and Galaxy, developers can efficiently manage secrets and access a vast library of community-contributed roles.
-
In VS Code, click the extension icon in the left menu. In the search field, type
Ansible
to bring up the Ansible extension by Red Hat. - Click the Install button.
- After installing the Ansible extension in the integrated development environment (IDE), click on the little gear icon beside the Install button and select the Extension Setting option, as illustrated in Figure 1.
2. Enable Ansible Lightspeed in VS Code
You will get the following settings page of Ansible Extension, from which you can make changes to execution environment image, container registry, Ansible lint, and many more. This article only covers Lightspeed, so we will just enable the Ansible Lightspeed option and Ansible Lightspeed with Watson Code Assistant inline suggestions. Please refer to Figure 2 to set the configuration.
Note: If you get the error "Ansible-lint is missing," use the following commands to install it. There are two methods to install: pip
and dnf
.
- Install using
pip
:pip3 install ansible-lint
- Install with the
dnf
package manager:dnf install ansible-lint
3. Authenticate Lightspeed with GitHub
Click on the Ansible extension icon on the left bar. You will see the Connect button, as shown in Figure 3.
It will redirect to your default browser and open the GitHub login page.
Ansible Lightspeed requires GitHub authentication. Sign in using your GitHub credentials and follow the prompts. Once done, you will be redirected to VS Code and the Connect button should now be replaced with your GitHub ID.
4. Validate Ansible Lightspeed
On the bottom-right corner, you should see Lightspeed (Figure 4). Ansible Lightspeed is ready for you to write playbooks.
5. How to trigger Ansible Lightspeed
Lightspeed will recommend context based on the text you type in the name field in the playbook section. Once you hit the Enter key, you will get the suggestion as faded text. Hit the Tab key if everything looks good to you.
The first example is "Installing Minikube on an RHEL server." To generate a playbook (Figure 5), make sure you have the format of the playbook as shown below, like defining hosts, permissions of become value, and so on:
- name: Lightspeed_demo
hosts: rhel
become: true
tasks:
- name: Install apache httpd server and enable it
ansible.builtin.package:
name: httpd
state: present
- name: Install minikube on rhel server
ansible.builtin.package:
name:
https://storage.googleapis.com/minikube/releases/latest/minikube-latest.x86_64.rpm
state: present
The second example is installing the kubectl
and oc
command-line interface (CLI) tools on a server. As in the previous example, we are defining topics about requirements in the name field, Ansible Lightspeed is generating and suggesting to us the context of Ansible playbook. If you are fine with the suggestion, press the Tab key to accept.
- name: Lightspeed_demo
hosts: rhel
become: true
tasks:
- name: Include redhar.rhel_system_roles.cockpit
ansible.builtin.include_role:
name: redhar.rhel_system_roles.cockpit
- name: Install apache httpd server and enable it
ansible.builtin.package:
name: httpd
state: present
- name: Install minikube on rhel server
ansible.builtin.package:
name:
https://storage.googleapis.com/minikube/releases/latest/minikube-latest.x86_64.rpm
state: present
- name: Install oc & kubectl
ansible.builtin.package:
name: "{{ item }}"
state: present
loop:
- https://mirror.openshift.com/pub/openshift-v4/clients/ocp/latest/openshift-client-linux.rpm
- kubectl
- atomic-openshift-utils
- name: Install and start the openshift CRC cluster on my rhel machine
ansible.builtin.command:
cmd: /usr/local/bin/oc adm ctnplc -n openshift-cluster-api -l k8s-app=openshift-cluster-api
chdir: /home/ano-user/openshift-cluster-api
Lightspeed also provides source code recommendations in the debugging window beside the terminal. It shows more information if you extend it, as shown in Figure 7.
Continue your automation journey with Ansible Automation Platform
Get started with Ansible Automation Platform by exploring interactive hands-on labs. Download Ansible Automation Platform at no cost and begin your automation journey today.
Last updated: September 28, 2023