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 [code] name=Visual Studio Code baseurl=https://packages.microsoft.com/yumrepos/vscode enabled=1 gpgcheck=1 gpgkey=https://packages.microsoft.com/keys/microsoft.asc
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
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.
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 File → New 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 tasks: - 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'"
Output from the execution. So our playbook has successfully executed.
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.
The extension in the article is long deprecated. The official one now is https://marketplace.visualstudio.com/items?itemName=redhat.ansible
Yep. This page should be updated with the new extension from Redhat.
Updated, thanks for highlighting this!