Ansible Pilot

Comparing versions - ansible.builtin.version plugin

Using Ansible to Compare Versions and Ensure Consistency Across Your Infrastructure

Using Ansible to compare versions and ensure consistency across your infrastructure with the ansible.builtin.version plugin.
May 7, 2023
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

YouTube Video

As your IT infrastructure grows and evolves, it can become increasingly difficult to ensure consistency across all your systems and applications. One way to address this challenge is using configuration management tools like Ansible to automate tasks such as version comparison and updates. This article will explore how to use Ansible to compare versions and ensure consistency across your infrastructure.

What is Ansible?

Ansible is an open-source tool that automates IT tasks such as configuration management, application deployment, and orchestration. It uses a simple language called YAML to describe configuration and automation tasks, making it easy for developers and system administrators to learn and use.

Comparing Versions with Ansible

One common task in IT infrastructure management is ensuring that all systems and applications run the same software version. This can be time-consuming and error-prone, especially as the number of systems and applications in your infrastructure grows.

Fortunately, Ansible makes it easy to automate version comparison and ensure consistency across your infrastructure. The code snippet provided above demonstrates how to use Ansible to compare the version of Ansible installed on all hosts with the specified version “2.15”.

This code uses the “ansible.builtin.version” filter to compare the installed version with the specified version and then returns a boolean value indicating whether the installed version is greater than or equal to the specified version. If the installed version is less than “2.15”, the debug message will return “False”, and you can use this information to trigger an update to the latest version of Ansible.

Benefits of Using Ansible for Version Comparison

Using Ansible to automate version comparison and updates has several benefits, including:

Demo

This Ansible playbook will compare the version of Ansible installed on all hosts with the specified version “2.15” using the “version” filter and return a boolean value indicating whether the installed version is greater than or equal to the specified version.

If the installed version of Ansible is greater than or equal to “2.15”, the debug message will return “True”. If the installed version is less than “2.15”, the debug message will return “False”.

Note that this playbook will run on all hosts specified in the Ansible inventory. If you only want to run it on a specific host or group of hosts, you will need to modify the “hosts” line accordingly.

Code

---
- name: Versions compare
  hosts: all
  tasks:
    - name: Print Ansible Version
      ansible.builtin.debug:
        var: ansible_version.full

    - name: Compare Versions
      ansible.builtin.debug:
        msg: "Version correct!"
      when: ansible_version.full is version('2.15', '>=')
localhost ansible_connection=local

Execution Ansible 2.14

ansible-playbook -i inventory version.yml 

PLAY [Versions compare] *****************************************************************

TASK [Gathering Facts] ******************************************************************
ok: [localhost]

TASK [Print Ansible Version] ************************************************************
ok: [localhost] => {
    "ansible_version.full": "2.14.4"
}

TASK [Compare versions] *****************************************************************
skipping: [localhost]

PLAY RECAP ******************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

Execution Ansible 2.15

ansible-playbook -i inventory version.yml 

PLAY [Versions compare] *****************************************************************

TASK [Gathering Facts] ******************************************************************
ok: [localhost]

TASK [Print Ansible Version] ************************************************************
ok: [localhost] => {
    "ansible_version.full": "2.15.0b3"
}

TASK [Compare versions] *****************************************************************
ok: [localhost] => {
    "msg": "Version correct!"
}

PLAY RECAP ******************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Recap

Using Ansible to compare versions and ensure consistency across your infrastructure is a powerful way to automate IT tasks and reduce the risk of errors and inconsistencies. By leveraging the power of automation, you can save time and increase accuracy while ensuring that all systems and applications in your infrastructure are running the latest software versions. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases