Speed Up Your Ansible Deployment with … VIM?

Speed Up Your Ansible Deployment with … VIM?


A perfect DevOps tool for anyone who wants to become more efficient with YAML

Intro

Do you know this feeling when your infrastructure is on fire? Your angry users ping you every 30 sec whether you’ve already fixed some broken functionality that blocks their work. Like:

“DNS is down! We cannot build our product!” or “I cannot connect to production, while we have a customer demo today!”. Yep, I’ve been there. Probably more times than I’d like to admit.

In those moments, every second counts. Your ability to act fast — and act precisely — is what separates a successful recovery from a costly disaster.

Now, if you’re working with Ansible, you’re already embracing automation and infrastructure as code. But here’s the twist — your efficiency still depends heavily on how fast and effectively you can work with the files themselves.

That’s where an old-school, blazing-fast, muscle-memory-based editor comes in.

So in this article — we can see how we can speed up your Linux file editing … VIM! With my top-4 commands that I use while fine-tuning my Ansible’s playbooks.

🤔 Why Use VIM Anyway?

Well… why not? Okay, jokes aside, I’d say that apart from the pace achieved by automation… it’s simply universal. It is supported by every Linux distro, and there are tons of materials and tutorials to learn from.

Sure, you can use VSCode — and it’s great for many things. But when you’re working on a headless server or jumping into a container, firing up an X server or dealing with remote GUI lag isn’t practical.

For simple configuration edits, quick fixes to Ansible playbooks, or navigating YAML files like a ninja — VIM is your best friend. It keeps you focused, fast, and fully in control.

It’s fast, stable, scriptable, and endlessly configurable — and once it becomes second nature, you’ll wonder how you ever worked without it.

Ok, so let’s review — the top 4 VIM use cases in real-world production.

1️⃣ Comment Out a Full Task Block (Visually)

Let’s say you have the following block of code:

- name: Move Helm binary to /usr/local/bin  become: yes  copy:    src: /tmp/linux-amd64/helm    dest: /usr/local/bin/helm    mode: '0755'    remote_src: yes- name: Ensure Helm is working  command: helm version  register: helm_output  changed_when: false  become: yes
- name: Move Helm binary to /usr/local/bin  become: yes  copy:    src: /tmp/linux-amd64/helm    dest: /usr/local/bin/helm    mode: '0755'    remote_src: yes- name: Ensure Helm is working  command: helm version  register: helm_output  changed_when: false  become: yes
- name: Move Helm binary to /usr/local/bin  become: yes  copy:    src: /tmp/linux-amd64/helm    dest: /usr/local/bin/helm    mode: '0755'    remote_src: yes- name: Ensure Helm is working  command: helm version  register: helm_output  changed_when: false  become: yes

Now, we need to test our playbook without name: Ensure Helm is working task. But we don’t want to remove it either. One option is to comment the whole block. How to do it in VIM?

Firstly, with the V character, we enter Visual mode and highlight the block we want to comment on:

Press enter or click to view image in full size


Then you press : (this will auto-fill the range like :'<,'>) and type s/^/# / and hit Enter.

What this combination s/^/# /means:

  • s — means substitute

  • ^ — matches the beginning of each line

  • # — this is what we want to insert at the beginning of each line (a hash followed by a space)

This allows us to quickly test the specific tasks, part of the code etc.

2️⃣ New settings with .vimrc

What is .vimrc ? Understanding .vimrc configuration file helps a lot. In a nutshell, this file enables you to specify relevant settings, shortcuts, Vim behavior, and more through plugins.

Some of my settings in ~/.vimrc:

set number                     " -->show line numberssyntax on                      " --> enable syntax highlightingset expandtab                  " -->use spaces instead of tabsset shiftwidth=2               " --> 2 spaces per indent (YAML standard)set tabstop=2                  " -->tab key inserts 2 spacesmatch ErrorMsg '\s\+$'         " --> highlight trailing whitespace
set number                     " -->show line numberssyntax on                      " --> enable syntax highlightingset expandtab                  " -->use spaces instead of tabsset shiftwidth=2               " --> 2 spaces per indent (YAML standard)set tabstop=2                  " -->tab key inserts 2 spacesmatch ErrorMsg '\s\+$'         " --> highlight trailing whitespace
set number                     " -->show line numberssyntax on                      " --> enable syntax highlightingset expandtab                  " -->use spaces instead of tabsset shiftwidth=2               " --> 2 spaces per indent (YAML standard)set tabstop=2                  " -->tab key inserts 2 spacesmatch ErrorMsg '\s\+$'         " --> highlight trailing whitespace

Frankly, I often had issues with trailing whitespace at the end of my YAML files. Now, when I have these settings, I can immediately see line numbers, spot whitespace errors, and stay better oriented within the file.

This helps me build better playbooks and be more oriented in the file.

Press Enter or click to view image in full size


3️⃣ Quickly Jump to line 12

One of the most common errors while we develop code in Ansible (or any software that uses YAML format such as Terraform or Kubernetes) is a YAML syntax error. For instance, when Ansible reports an issue like the following:

Press enter or click to view image in full size


Instead of scrolling manually, we can just run:

vi +7 playbooks/1_web_db.yml
vi +7 playbooks/1_web_db.yml
vi +7 playbooks/1_web_db.yml

This command will open our playbook directly at line 7 (where we have syntax issue). In this specific case, the yaml parser complains about the incorrect indentation at apt module.

Quick command + <number of line> allows to quickly get rid of an extra space.

4️⃣ Comparing Files with Diff Mode

While I code roles, playbooks, templates etc. very quickly I am getting a lot of files in my Ansible project. Of course, Ansible can still support me with pre-made modules or roles that are responsible for most of my functionalities in my infrastructure.

One vim option that I use is diff. For example, I have a separate inventory for my WebServer nodes and a separate inventory for my Kubernetes nodes. To quickly compare IP, used keys, etc. I and run:

 vim -d inventory inventory_k8s_only
 vim -d inventory inventory_k8s_only
 vim -d inventory inventory_k8s_only

Press enter or click to view image in full size


VIM diff of two inventory files

VIM diff of two inventory files


Outro

To sum up, if you know VIM well enough, it can really heavily support and accelerate your development (even under stress). Just a few commands + well-configured .vimrc can streamline your troubleshooting and improve your YAML structuring. This software not only saves time but also reduces the risk of errors during critical moments in your infrastructure management.


Test us in action. Book a free
consultation for your team

We don't do hard sales pitches. Let's spend 30 minutes discussing your current DevOps bottlenecks, and we'll help you map out the most efficient upskilling path for your team.

Test us in action. Book a free consultation for your team

We don't do hard sales pitches. Let's spend 30 minutes discussing your current DevOps bottlenecks, and we'll help you map out the most efficient upskilling path for your team.