Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Require minimum version of Ansible #908

Closed
grasmash opened this issue Sep 27, 2016 · 9 comments
Closed

Require minimum version of Ansible #908

grasmash opened this issue Sep 27, 2016 · 9 comments
Labels

Comments

@grasmash
Copy link

grasmash commented Sep 27, 2016

It would be great if Drupal VM asserted that a minimum version of Ansible is in use.

You could use the assert module for this.

Something like:

- hosts: all
  tasks:
    - name: Get current Ansible version on local host
      local_action: command ansible --version
      register: local_ansible_version
    - assert:
        that:
          - "'2.1' in local_ansible_version.stdout"
@geerlingguy
Copy link
Owner

+1 to this; There are a number of people who have run into issues like #891, #890, #771, and #713... would help stomp out those errors in the future.

This would need to run prior to anything else.

@smiller171
Copy link

I think I may spend some time later turning this into a module, but here's a python script that will fail if the installed version of Ansible isn't at least 2.1.0:

# -*- encoding:utf-8 -*-
from distutils.version import LooseVersion

import sys

import ansible

def print_red_bold(text):
    print('\x1b[31;1m' + text + '\x1b[0m')

def __init__(self):
    # Can't use `on_X` because this isn't forwards compatible with Ansible 2.0+
    required_version = '2.1.0'
    installed_version = ansible.__version__
    if LooseVersion(installed_version) < LooseVersion(required_version):
        print_red_bold(
            "drupal-vm restriction: only Ansible version greater than {version} is supported.\n"
            .format(version=required_version)
        )
        sys.exit(1)

@geerlingguy
Copy link
Owner

For easier maintenance (e.g. when 2.2+ comes out), to test a minimum version, we can use version_compare... I hope :)

@geerlingguy
Copy link
Owner

Nice... looks like there's already a globally-accessible ansible_version variable that lets us do this more easily than string comparisons.

Test playbook:

---
- hosts: localhost
  pre_tasks:
    - debug: var=ansible_version

Output:

TASK [debug] *******************************************************************
ok: [localhost] => {
    "ansible_version": {
        "full": "2.1.1.0", 
        "major": 2, 
        "minor": 1, 
        "revision": 1, 
        "string": "2.1.1.0"
    }
}

@geerlingguy
Copy link
Owner

I'll be adding a new variable for this and for vagrant min version (was going to hardcode it in playbook, but it's nicer to use a var):

drupalvm_vagrant_version_min: '1.8.5'
drupalvm_ansible_version_min: '2.1'

@smiller171
Copy link

@geerlingguy nice catch. I looked for both a version_compare function and an ansible_version var and couldn't find either. ansible_version wasn't listed when I ran setup ad-hoc

@smiller171
Copy link

One thing to note is that if someone has a really old version of Ansible, it's likely to fail on a syntax check before it ever gets to running your version check task. The only way I know to get around this is to write something as a callback module that is compatible with both pre and post 2.0

@geerlingguy
Copy link
Owner

@smiller171 - True, mentioned in blog post here: http://www.jeffgeerling.com/blog/2016/require-minimum-ansible-version-your-playbook

I'd rather Ansible solves this upstream... everyone adding their own callback plugin for a version check seems a bit crazy.

@smiller171
Copy link

Agreed. I wonder if Ansible would accept a pull request to add in that functionality. Having it as a top-level parameter for a play seems to make sense:

- hosts: all
   minimum_ansible_version: 1.9.2
   tasks:
.....

Of course this isn't helpful for users of old versions, since the old versions wouldn't support the minimum_ansible_version parameter. Perhaps you could also ship a callback plugin with instructions on how to include it with your playbooks.

kekkis pushed a commit to kekkis/drupal-vm that referenced this issue Feb 23, 2017
kekkis pushed a commit to kekkis/drupal-vm that referenced this issue Feb 23, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants