How to detect an ansible version

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 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?

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.

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.

​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:
     that:
         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

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