From bc203433f1120373fbe751521eafc6cd3a3ee336 Mon Sep 17 00:00:00 2001
From: Michal Nasiadka <mnasiadka@gmail.com>
Date: Mon, 25 Sep 2023 14:03:34 +0000
Subject: [PATCH] Add support for ansible-core

Collections version taken from official Ansible 7 package [1]
and limited to the same major version.

[1]: https://github.com/ansible-community/ansible-build-data/blob/main/7/ansible-7.7.0.yaml

Change-Id: I0f1ea9717397adc2d2c3215d74ec3759f351a57a
---
 doc/source/user/quickstart.rst                |  5 +++--
 ...support-ansible-core-81ab88ca37b949cc.yaml |  5 +++++
 requirements-core.yml                         | 11 ++++++++++
 setup.cfg                                     |  1 +
 tests/run.yml                                 | 21 ++++++++++++++++++-
 tools/kolla-ansible                           |  8 ++++++-
 6 files changed, 47 insertions(+), 4 deletions(-)
 create mode 100644 releasenotes/notes/support-ansible-core-81ab88ca37b949cc.yaml
 create mode 100644 requirements-core.yml

diff --git a/doc/source/user/quickstart.rst b/doc/source/user/quickstart.rst
index 15dfd261e..14024ca3b 100644
--- a/doc/source/user/quickstart.rst
+++ b/doc/source/user/quickstart.rst
@@ -90,11 +90,12 @@ Install dependencies for the virtual environment
       pip install -U pip
 
 #. Install `Ansible <http://www.ansible.com>`__. Kolla Ansible requires at least
-   Ansible ``6`` and supports up to ``7``.
+   Ansible ``6`` (or ansible-core ``2.14``) and supports up to ``7`` (or
+   ansible-core ``2.15``).
 
    .. code-block:: console
 
-      pip install 'ansible>=6,<8'
+      pip install 'ansible-core>=2.14,<2.16'
 
 
 Install Kolla-ansible
diff --git a/releasenotes/notes/support-ansible-core-81ab88ca37b949cc.yaml b/releasenotes/notes/support-ansible-core-81ab88ca37b949cc.yaml
new file mode 100644
index 000000000..f162511f9
--- /dev/null
+++ b/releasenotes/notes/support-ansible-core-81ab88ca37b949cc.yaml
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    Adds support for ansible-core only installation (use ``kolla-ansible
+    install-deps`` to install required collections).
diff --git a/requirements-core.yml b/requirements-core.yml
new file mode 100644
index 000000000..aec45a20a
--- /dev/null
+++ b/requirements-core.yml
@@ -0,0 +1,11 @@
+---
+collections:
+  - name: ansible.netcommon
+    source: https://galaxy.ansible.com
+    version: <5
+  - name: ansible.posix
+    source: https://galaxy.ansible.com
+    version: <2
+  - name: community.general
+    source: https://galaxy.ansible.com
+    version: <7
diff --git a/setup.cfg b/setup.cfg
index 7ad076fdf..e03675ee4 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -37,6 +37,7 @@ data_files =
     share/kolla-ansible = tools/init-vpn
     share/kolla-ansible = setup.cfg
     share/kolla-ansible = requirements.yml
+    share/kolla-ansible = requirements-core.yml
 
 scripts =
     tools/kolla-ansible
diff --git a/tests/run.yml b/tests/run.yml
index 843f59cf1..92db8985b 100644
--- a/tests/run.yml
+++ b/tests/run.yml
@@ -256,15 +256,34 @@
         virtualenv: "{{ kolla_ansible_venv_path }}"
 
     - name: install kolla-ansible and dependencies
+      vars:
+        ansible_core_version_min: "==2.14.*"
+        ansible_core_version_max: "==2.15.*"
+        # Test latest ansible version on Ubuntu, minimum supported on others.
+        ansible_core_version_constraint: >-
+          {{ ansible_core_version_min if is_upgrade or base_distro != 'ubuntu' else ansible_core_version_max }}
       pip:
         extra_args: "-c {{ upper_constraints_file }}"
         name:
           - "{{ kolla_ansible_src_dir }}"
           - "ansible-core{{ ansible_core_version_constraint }}"
-          - "ansible{{ ansible_version_constraint }}"
           - "ara[server]<1.7"
         virtualenv: "{{ kolla_ansible_venv_path }}"
 
+    # TODO(mnasiadka): Remove in Caracal
+    - name: install Ansible on upgrade only
+      vars:
+        ansible_version_min: "==7.*"
+        ansible_version_max: "==8.*"
+        # Test latest ansible version on Ubuntu, minimum supported on others.
+        ansible_version_constraint: >-
+          {{ ansible_version_min if is_upgrade or base_distro != 'ubuntu' else ansible_version_max }}
+      pip:
+        extra_args: "-c {{ upper_constraints_file }}"
+        name: "ansible{{ ansible_version_constraint }}"
+        virtualenv: "{{ kolla_ansible_venv_path }}"
+      when: is_upgrade
+
     - name: install Ansible collections
       shell: |
         source {{ kolla_ansible_venv_path }}/bin/activate
diff --git a/tools/kolla-ansible b/tools/kolla-ansible
index 0fa247da2..07b43cf6c 100755
--- a/tools/kolla-ansible
+++ b/tools/kolla-ansible
@@ -130,7 +130,13 @@ function find_base_dir {
 
 function install_deps {
     echo "Installing Ansible Galaxy dependencies"
-    ansible-galaxy collection install -r ${BASEDIR}/requirements.yml --force
+    if pip show ansible 2>/dev/null; then
+        ansible-galaxy collection install -r ${BASEDIR}/requirements.yml --force
+    else
+        ansible-galaxy collection install -r ${BASEDIR}/requirements.yml --force
+        ansible-galaxy collection install -r ${BASEDIR}/requirements-core.yml --force
+    fi
+
     if [[ $? -ne 0 ]]; then
         echo "ERROR: Failed to install Ansible Galaxy dependencies" >&2
         exit 1
-- 
GitLab