From 63ab53195c63f24006ed82c81d06bfdb9dbe1a4c Mon Sep 17 00:00:00 2001
From: Dincer Celik <hello@dincercelik.com>
Date: Tue, 17 Dec 2019 07:23:04 +0300
Subject: [PATCH] Adds maximum supported version check for Ansible

Maximum supported version is set to 2.9

Updated the minimum supported version to 2.8

Implements: blueprint ansible-max-version

Change-Id: I97cc95e37f49886e6d74f2d5a789b923b14b5a2d
---
 ansible/roles/prechecks/tasks/package_checks.yml   |  9 +++++++--
 ansible/roles/prechecks/vars/main.yml              |  3 ++-
 doc/source/user/quickstart.rst                     |  8 ++++----
 ...sible-max-version-support-d74258eec27d3549.yaml |  8 ++++++++
 tests/run.yml                                      |  4 ++--
 tools/kolla-ansible                                | 14 ++++++++++++++
 6 files changed, 37 insertions(+), 9 deletions(-)
 create mode 100644 releasenotes/notes/ansible-max-version-support-d74258eec27d3549.yaml

diff --git a/ansible/roles/prechecks/tasks/package_checks.yml b/ansible/roles/prechecks/tasks/package_checks.yml
index 7919b540e..f8f65136e 100644
--- a/ansible/roles/prechecks/tasks/package_checks.yml
+++ b/ansible/roles/prechecks/tasks/package_checks.yml
@@ -6,8 +6,13 @@
   when: inventory_hostname in groups['baremetal']
   failed_when: result is failed or result.stdout is version(docker_py_version_min, '<')
 
+# NOTE(osmanlicilegi): ansible_version.full includes patch number that's useless
+# to check. as ansible_version does not provide major.minor in dict, we need to
+# set it as variable.
 - name: Checking Ansible version
+  vars:
+    ansible_version_host: "{{ ansible_version.major }}.{{ ansible_version.minor }}"
   fail:
-    msg: "Current Ansible version {{ ansible_version.full }} is less than {{ ansible_version_min }}"
+    msg: "Ansible version should be between {{ ansible_version_min }} and {{ ansible_version_max }}. Current version is {{ ansible_version.full }} which is not supported."
   run_once: true
-  when: ansible_version.full is version(ansible_version_min, '<')
+  when: ansible_version_host is version(ansible_version_min, '<') or ansible_version_host is version(ansible_version_max, '>')
diff --git a/ansible/roles/prechecks/vars/main.yml b/ansible/roles/prechecks/vars/main.yml
index 8d673d536..4f1616589 100644
--- a/ansible/roles/prechecks/vars/main.yml
+++ b/ansible/roles/prechecks/vars/main.yml
@@ -1,4 +1,5 @@
 ---
 docker_version_min: '1.10.0'
 docker_py_version_min: '2.0.0'
-ansible_version_min: '2.6.0'
+ansible_version_min: '2.8'
+ansible_version_max: '2.9'
diff --git a/doc/source/user/quickstart.rst b/doc/source/user/quickstart.rst
index 051e6d1e8..d4bd1cd9a 100644
--- a/doc/source/user/quickstart.rst
+++ b/doc/source/user/quickstart.rst
@@ -89,8 +89,8 @@ If not installing Kolla Ansible in a virtual environment, skip this section.
 
       pip install -U pip
 
-#. Install `Ansible <http://www.ansible.com>`__. Currently, Kolla Ansible
-   requires Ansible 2.6+.
+#. Install `Ansible <http://www.ansible.com>`__. Kolla Ansible requires at least
+   Ansible ``2.8`` and supports up to ``2.9``.
 
    .. code-block:: console
 
@@ -121,8 +121,8 @@ If installing Kolla Ansible in a virtual environment, skip this section.
 
       sudo pip install -U pip
 
-#. Install `Ansible <http://www.ansible.com>`__. Currently, Kolla Ansible
-   requires Ansible 2.6+.
+#. Install `Ansible <http://www.ansible.com>`__. Kolla Ansible requires at least
+   Ansible ``2.8`` and supports up to ``2.9``.
 
    For CentOS or RHEL, run:
 
diff --git a/releasenotes/notes/ansible-max-version-support-d74258eec27d3549.yaml b/releasenotes/notes/ansible-max-version-support-d74258eec27d3549.yaml
new file mode 100644
index 000000000..152f483f5
--- /dev/null
+++ b/releasenotes/notes/ansible-max-version-support-d74258eec27d3549.yaml
@@ -0,0 +1,8 @@
+---
+upgrade:
+  - |
+    Adds maximum supported version check for Ansible. Kolla Ansible now
+    requires at least Ansible ``2.8`` and supports up to ``2.9``. See
+    `blueprint
+    <https://blueprints.launchpad.net/kolla-ansible/+spec/ansible-max-version>`__
+    for details.
diff --git a/tests/run.yml b/tests/run.yml
index c1f5a13e6..5a8322264 100644
--- a/tests/run.yml
+++ b/tests/run.yml
@@ -148,7 +148,7 @@
     - name: install kolla-ansible and dependencies
       vars:
         # Test latest ansible version on Ubuntu, minimum supported on others.
-        ansible_version_constraint: "{{ '>=2.6' if base_distro == 'ubuntu' else '<2.7' }}"
+        ansible_version_constraint: "{{ '==2.9.*' if base_distro == 'ubuntu' else '==2.8.*' }}"
       pip:
         name:
           - "{{ kolla_ansible_src_dir }}"
@@ -373,7 +373,7 @@
             - name: install ansible and ARA for python 3
               vars:
                 # Test latest ansible version on Ubuntu, minimum supported on others.
-                ansible_version_constraint: "{{ '>=2.6' if base_distro == 'ubuntu' else '<2.7' }}"
+                ansible_version_constraint: "{{ '==2.9.*' if base_distro == 'ubuntu' else '==2.8.*' }}"
               pip:
                 name:
                   - "ansible{{ ansible_version_constraint }}"
diff --git a/tools/kolla-ansible b/tools/kolla-ansible
index 2bc0ada17..733c69c89 100755
--- a/tools/kolla-ansible
+++ b/tools/kolla-ansible
@@ -2,6 +2,18 @@
 #
 # This script can be used to interact with kolla via ansible.
 
+function check_ansible_compatibility {
+    ANSIBLE_VERSION_MIN=2.8
+    ANSIBLE_VERSION_MAX=2.9
+    ANSIBLE_VERSION_HOST=$(ansible --version | head -n1 | egrep -o '[0-9]\.[0-9]+')
+
+    if [[ $(printf "%s\n" "$ANSIBLE_VERSION_MIN" "$ANSIBLE_VERSION_MAX" "$ANSIBLE_VERSION_HOST" | sort -V | head -n1) != "$ANSIBLE_VERSION_MIN" ]] ||
+       [[ $(printf "%s\n" "$ANSIBLE_VERSION_MIN" "$ANSIBLE_VERSION_MAX" "$ANSIBLE_VERSION_HOST" | sort -V | tail -1)  != "$ANSIBLE_VERSION_MAX" ]]; then
+        echo "ERROR: Ansible version should be between $ANSIBLE_VERSION_MIN and $ANSIBLE_VERSION_MAX. Current version is $ANSIBLE_VERSION_HOST which is not supported."
+        exit 1
+    fi
+}
+
 function find_base_dir {
     # $1: Python interpreter
     local python
@@ -117,6 +129,8 @@ genconfig
 EOF
 }
 
+check_ansible_compatibility
+
 SHORT_OPTS="hi:p:t:k:e:v"
 LONG_OPTS="help,inventory:,playbook:,skip-tags:,tags:,key:,extra:,verbose,configdir:,passwords:,limit:,forks:,vault-id:,ask-vault-pass,vault-password-file:,yes-i-really-really-mean-it,include-images,include-dev:,full,incremental"
 
-- 
GitLab