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 Playbooknstrates 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:

  • Increased consistency: Automating version comparison and updates ensures that all systems and applications are running the same software version, which can help prevent compatibility issues and reduce the risk of security vulnerabilities.
  • Time savings: Automating version comparison and updates can save time by reducing the amount of manual effort required to ensure consistency across your infrastructure.
  • Improved accuracy: Automating version comparisons and updates can reduce the risk of errors when performing these tasks manually.
  • Scalability: Ansible is designed to be scalable, so you can easily manage version comparison and updates across many systems and applications.
Join 50+ hours of courses in our exclusive community

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

  • versin.yml
---
- 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', '>=')
  • inverntory
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

Conclusion

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 Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

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

Patreon Buy me a Pizza