Visual Studio Code with Ansible

Manage Windows with Ansible – Part 2: Connect Visual Studio Code with Ansible

I hope you read my first artikel “Part 1 – Manage Windows with Ansible” about Ansible. If not take a look, otherwise go ahead 😉 In this article I explain how you can connect Visual Studio Code with Ansible server. So you can easily create your Playbooks on your desktop and simply transfer them to the Ansible server and run them. The great thing is, you can also see the state of the script and its output, so you don’t have to connect to the Ansible server. Since I have been dealing with data center automation for some time, I try to learn as fast as possible and would like to share my experiences with you. But after some changes in my Playbooks I found it quite annoying to edit everything about the CLI Editors like VI or nano. This can be very exhausting.

In addition, the scripts should be managed and versioned later via azure Devop Repos or via GitHub. But more about that in another article, that would go beyond the scope of this article.

What is needed? Set up Visual Studio Code environment.

First you download the setups. Of course we need Microsoft Visual Studio code. You can get it under the following link:

https://code.visualstudio.com/

Visual Studio Code Download

Now we have to download Node.js. This is necessary, otherwise we will not be able to establish a connection to the Ansible Server via SSH. You can find the download here:

https://nodejs.org/en/

Screenshot https://nodejs.org/en/

Installing Visual Studio Code

I don’t describe the installation steps of Visual Studio Code in detail now. You can simply go through the setup. I think the steps are self-explanatory.

If you have installed Visual Studio Code, you can install Node.js directly. Then we already have all the requirements to continue.
If you have any problems or if you want me to include the steps in the article, just let me know. I’ll add them then.

The folder structure

Before we start our Visual Studio code for the first time, we create the necessary folder structure. For this we create the same folder structure as used in our Ansible installation. You can find the blog article here.

The folder .vscode and the inventory file will be created later automatically by Visual Studio code.

Here you can see what the folder structure should look like. You can change the name Lab as you like. If you do this, you only have to change the ansible.conf as well.

Create Folder - Setup Visual Studio Code with Ansible

Lets start Visual Studio Code

Now that we have everything ready, we can start Visual Studio Code. First we can open our created folder. If we connect Visual Studio with Ansible Server later, the folder will be automatically copied to our Ansible Server via SSH. I think this is pretty handy because it’s annoying to copy the files by hand every time you make a lot of changes at the beginning. And I made a lot of changes at the beginning until everything worked as I imagined it would. To open the folder, you only have to open the created folder via File and “open Folder”. The structure will be displayed in the Visual Studio code. I’ve attached a picture of how the structure will look when some Playbooks are present.

Ansible Folder in Visual Studio

Extension to Connect Visual Studio Code with Ansible

In order to connect Visual Studio Code with Ansible, we still need an extension. Extensions for Visual Studio Code can be found under the button “Extensions” on the left menu bar. Here you can install the extensions, find information about them or delete them if necessary. Simply select the extension and click Install.

Connect Visual Stuido with Ansible ExtensionConnect Visual Stuido with Ansible Extension

Lets play! PUSH THE BUTTON!

Now that we’ve got it all set up, I’d say FIRE FREE! Choose a Playbook you want to start and right-click it. Now you can directly select “run ansible playbook remotely via SSH”. Then you will be asked at the top of the screen either to select the Ansible server, if you already have one, or to enter a new server + port + user and password. The first time you are asked if you always want to copy the files when saving them. I selected “Always”.

Execute Playbook via SSH on Ansible ServerExecute Playbook via SSH on Ansible Server

How to remove SSH / Ansible servers, that not exist anymore?

This may sound a bit stupid, but since I have my lab running on azure, the IP address of my Ansible server changed a few times at the beginning. And yes, as a little Monk it bothered me quite fast that I had SSH servers to choose from, which no longer exist. You can remove the servers in your user profile under .ssh -> servers.json.

… And yes, you see right. The ssh passwords are there in plain text. There is still room for improvement!

See if it works…

Once the Playbook has started, you can see the progress directly at the bottom of the terminal area. I think it’s really cool! I have attached a Playbook to you. It’s just a simple one, but I think you can test it quite well with it. It helped me anyway. You can download the file here : https://github.com/thomaspreischl/ansibleplaybooks just select netstat-e.yml

playbook netstat -eplaybook netstat -e

Ich hoffe der Artikel hat euch gefallen. Die nächsten sind schon in der Pipeline! Ich bin gerade dabei Step by Step eine ganze Umgebung automatisch mit VMs inkl. Active Directory, Fileserver, DNS, DHCP usw. zu erstellen.

Wenn ihr wollt, liked mich auf Facebook oder Twitter.

Leave a Comment