Writing Ansible playbook with Visual Studio Code

Ansible Tutorial

Now that you are familiar with writing ansible playbooks, in this section we will use Visual Studio Code Editor to write playbooks instead of CLI. You will need GUI based Linux environment for this part of tutorial. If your CentOS or Red Hat is not installed with GUI Desktop then you can install GNOM Desktop or Workstation using DNF or YUM.


Download Visual Studio Code Repo

You can get the steps to install Microsoft Visual Studio Code Editor from the official visualstudio page

[root@controller ~]# rpm --import https://packages.microsoft.com/keys/microsoft.asc

Next execute this script which will create your /etc/yum.repos.d/vscode.repo repository file

[root@controller ~]# sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'

Verify your new repo content

[root@controller ~]# cat /etc/yum.repos.d/vscode.repo
name=Visual Studio Code

Now you can go ahead and install Visual Studio Code using dnf or yum.

[root@controller ~]# dnf install code -y


Access Visual Code Studio

To open the Visual Studio Code Editor, login to the graphical console of your controller node as ansible user and execute "code ." on the terminal at the home folder or you can using any of the Projects directory

Writing Ansible playbook using Visual Studio Code Editor


The Visual Studio Editor will come up and you can see all the YAML files which we have created are also visible in the LEFT TAB.


Install Ansible Extension

Since we have to work with Ansible we must install the Ansible Extension/Plugin. To install this click on "Extensions" from the Left Menu and search for "Ansible" string. Click on "install" to install the respective extension.

Red Hat Ansible Extension Visual Studio Code


Create playbook using Visual Studio

Now we can create a new file and start by creating a new playbook using the visual editor. Click on FileNew File which will create a new file. Press Ctrl+s to save it with a different name, I will save it as "playbook_vsc.yaml"

Now I have created a very simple playbook using the Visual Code Editor. Press Ctrl+s to save the playbook before executing.

 - name: Using Visual Code Editor
   hosts: localhost
     - debug:
         msg: "Hello World"


To execute the playbook using Visual Studio press Ctrl + Shift + P which will present you a bunch of options for execution. Since we are executing the script on localhost i.e. the controller node, I will use "Run Ansible Playbook via 'ansible-playbook'"

Execute Ansible On Visual Studio Code


Output from the execution. So our playbook has successfully executed.

Writing Ansible playbook with Visual Studio Code


Further Reading

Ansible VS Code Extension by Red Hat


What's Next

Next in our Ansible Tutorial we will start using different types of operators such as arithmetic, comparison, test operator, ... in Ansible.


Views: 465

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

3 thoughts on “Writing Ansible playbook with Visual Studio Code”

Leave a Comment