Writing Ansible playbook with Visual Studio Code

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.


Writing Ansible playbook using Visual Studio Code Editor


Configure Visual Studio to use Ansible

On the same session scroll the page and look out for "Usage" section which will show following information:

Writing Ansible playbook using Visual Studio Code Editor


So you have to copy this setting as we have to use this to associate our Visual Studio with this JSON configuration. After you have copied the content, click on FilePreferences → Settings

Writing Ansible playbook using Visual Studio Code Editor


Search for "associations" in the search bar which will bring you "settings.json". Click on "Edit in settings.json" and paste the content which we had copied and press Ctrl+s to save the file and close the file.

Writing Ansible playbook using Visual Studio Code Editor


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.yml"

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 in Local Ansible"

Writing Ansible playbook using Visual Studio Code Editor


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

Writing Ansible playbook using Visual Studio Code Editor


What’s Next

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



Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

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 either use the comments section or contact me form.

Thank You for your support!!

1 thought on “Writing Ansible playbook with Visual Studio Code”

Leave a Comment