Comparing versions - ansible.builtin.version plugin
Using Ansible to Compare Versions and Ensure Consistency Across Your Infrastructure


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:
- 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.
Links
- https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_tests.html#comparing-versions
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/version_test.html
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
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
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, Website, Twitter, and Substack 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
Donate
Want to keep this project going? Please donate