# How to detect an ansible version

**URL:** <https://forum.ansible.com/t/how-to-detect-an-ansible-version/19200>\
**Category:** Ansible Developer\
**Created:** [September 8, 2015, 6:19am UTC](https://forum.ansible.com/t/how-to-detect-an-ansible-version/19200 "2015-09-08T06:19:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valeriy\_Solovyov](https://avatars.discourse-cdn.com/v4/letter/v/8e8cbc/32.png) [@Valeriy\_Solovyov](https://forum.ansible.com/u/Valeriy_Solovyov)\
**Post date:** [September 8, 2015, 6:19am UTC](https://forum.ansible.com/t/how-to-detect-an-ansible-version/19200/1 "2015-09-08T06:19:07Z")

</div>

Hello,

I am a perfectionist. I want write ansible-playbooks that have backward compatibility.  
For example if I use ansible \< 1.5.1 [http://docs.ansible.com/ansible/get\_url\_module.html](http://docs.ansible.com/ansible/get_url_module.html) I use it without validate\_certs but in newer versions I must use with validate\_certs=no.  
But in ansible \< 1.4 there wasn’t this option it was failed because “no module option”.

Also **I wasn’t able to find how to detect an ansible version**. I wanted to do something like this:

`

- name: Include task based on the OS enviroment  
include: “{{item}}”  
with\_first\_found:

| 
- “{{ ansible\_os\_family }}-ansible-{{ ansible\_curr\_full\_version }}.yml”
- “{{ ansible\_os\_family }}-ansible-{{ ansible\_curr\_major\_version }}.yml”
- “{{ ansible\_os\_family }}.yml”

 |
| | |

`

or

`

- name: Include task based on the ansible version  
include: “{{ ansible\_os\_family }}-ansible-{{ ansible\_major\_version }}.yml”  
`

But ansible said that it’s deprecated behavior.

I detected ansible version simply in the playbook, but it’s not good (I think):

`

- name: Detect ansible major version  
shell: ansible-playbook --version 2\> /dev/null|cut -d " " -f2|cut -d “.” -f1  
register: ansible\_version\_check  
ignore\_errors: True

- name: Setting ansible major version variable  
set\_fact:  
ansible\_curr\_major\_version: “{{ ansible\_version\_check[‘stdout’] }}”

- name: Setting ansible major version variable  
set\_fact:  
ansible\_major\_version: “{{ ansible\_version\_check[‘stdout’] }}”

- name: Setting ansible version variable  
set\_fact:  
ansible\_curr\_full\_version: “{{ ansible\_version\_check[‘stdout’] }}”

`

I found more simply way through an enviroment variable:

` ANSIBLE_SHORT_VERSION=`ansible-playbook --version 2\> /dev/null|cut -d " " -f2|cut -d “.” -f1,2` ansible-playbook -i …

`

and in the defaults/main.yml:

`  
ansible\_curr\_short\_version: “{{ lookup(‘env’,‘ANSIBLE\_SHORT\_VERSION’)| default(‘1.5’) }}”  
old\_ansible\_versions: [‘1.4’,‘1.3’, ‘1.2’, ‘1.1’,‘1.0’]

`

in the tasks I use /tasks/main.yml:

`

- include: old.yml  
when: “ansible\_curr\_short\_version in old\_ansible\_versions”

`

PS:  
If there is possibility to use modules that added new option’s in the old version of ansible? Ignore them?

---

<div class="post-metadata">

**Author:** ![Jonathan\_Davila](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@Jonathan\_Davila](https://forum.ansible.com/u/Jonathan_Davila)\
**Post date:** [September 21, 2015, 12:17pm UTC](https://forum.ansible.com/t/how-to-detect-an-ansible-version/19200/2 "2015-09-21T12:17:51Z")

</div>

Personally, I don’t understand why you would want to this to begin with. Short of some regression introduced into a release there isn’t really ever a reason to be using an older version of Ansible (especially \<1.6). Could you explain why you would want to create Ansible projects that are compatible with versions of Ansible that are many major-releases old?

To your last question, yes. From the root of your project directory, create a ‘library/’ directory and inside of that directory you can place any module that you want and then you can use it in your playbook. Modules loaded from library/ take precedence over the system installed modules.

---

<div class="post-metadata">

**Author:** ![Mark\_Chance](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mark\_Chance](https://forum.ansible.com/u/Mark_Chance)\
**Post date:** [November 13, 2015, 5:19pm UTC](https://forum.ansible.com/t/how-to-detect-an-ansible-version/19200/3 "2015-11-13T17:19:02Z")

</div>

The reason to want this is to help with migration to Ansible 2.0. I have found non-backward compatible issues (different path to include files) and in order to commit playbooks that will run in Ansible 1.9 before a complete cutover to Ansible 2.0, I would like the playbook to handle both versions.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [November 13, 2015, 5:28pm UTC](https://forum.ansible.com/t/how-to-detect-an-ansible-version/19200/4 "2015-11-13T17:28:15Z")

</div>

​Euhm. Things have broken often and a lot in the past. That is exactly why  
I don't easily upgrade ansible versions. Getting all my ansible playbooks  
and roles tested is not an quick job.  
Managing versions is the proper way to handle software, IMHO.

That being said, there is an ansible internal variable that helps here:

- assert:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_version.full|version\_compare('1.9.2', '=')

I'm not sure in what version it was introduced though. It won't be  
available in very old versions like 1.6

​

---

<div class="post-metadata">

**Author:** ![Mark\_Chance](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mark\_Chance](https://forum.ansible.com/u/Mark_Chance)\
**Post date:** [November 13, 2015, 7:41pm UTC](https://forum.ansible.com/t/how-to-detect-an-ansible-version/19200/5 "2015-11-13T19:41:20Z")

</div>

Serge:  
Thanks for that - it should work perfectly!  
Mark
