Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Ansible version from inside of a playbook

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?

like image 965
usterk Avatar asked Jan 15 '16 11:01

usterk


People also ask

What is the command to check the Ansible version?

After the installation, run the ansible --version command to check the version of Ansible installed.

How do you check Ansible syntax?

Use this command to check the playbook for syntax errors: $ ansible-playbook <playbook. yml> --syntax-check.


2 Answers

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"
like image 89
Clarence Mills Avatar answered Oct 25 '22 07:10

Clarence Mills


You can use the assert module:

- assert:
    that: ansible_version.major < 2
like image 25
udondan Avatar answered Oct 25 '22 08:10

udondan