I have a playbook that is running in different way in Ansible 1.9.x and 2.0. I would like to check currently running ansible version in my playbook to avoid someone running it with old one.
I don't think that this is the best solution:
- local_action: command ansible --version
register: version
What would you suggest?
After the installation, run the ansible --version command to check the version of Ansible installed.
Use this command to check the playbook for syntax errors: $ ansible-playbook <playbook. yml> --syntax-check.
Ansible provides a global dict called ansible_version, dict contains the following
"ansible_version": {
"full": "2.7.4",
"major": 2,
"minor": 7,
"revision": 4,
"string": "2.7.4"
}
you can use any of the following ansible_version.full, ansible_version.major or any other combination in creating conditional statements to check the version of ansible that's installed.
example playbook: using this dict and a when statement.
---
- hosts: localhost
tasks:
- name: Print message if ansible version is greater than 2.7.0
debug:
msg: "Ansible version is {{ ansible_version.full }}"
when: ansible_version.full >= "2.7.4"
You can use the assert module:
- assert:
that: ansible_version.major < 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With