From 63fd53eb77c2048311c99d8266c9775d412b4a60 Mon Sep 17 00:00:00 2001
From: Thomas Woerner <twoerner@redhat.com>
Date: Tue, 10 Oct 2017 17:20:59 +0200
Subject: [PATCH] Use Python2/3 to detect ansible_python_interpreter

With the test it is not needed to pin down the python interpreter for ansible
modules. It is therefore possible to use a Python2 version on Fedora-27 and
a Python3 version on Fedora-26.
---
 roles/ipaclient/tasks/install.yml         |  4 ++++
 roles/ipaclient/tasks/python_2_3_test.yml | 15 +++++++++++++++
 roles/ipaclient/tasks/uninstall.yml       |  4 ++++
 roles/ipaclient/vars/Fedora-25.yml        |  2 +-
 roles/ipaclient/vars/Fedora-26.yml        |  2 +-
 roles/ipaclient/vars/RedHat-7.3.yml       |  2 +-
 roles/ipaclient/vars/RedHat-7.yml         |  2 +-
 roles/ipaclient/vars/default.yml          |  2 +-
 8 files changed, 28 insertions(+), 5 deletions(-)
 create mode 100644 roles/ipaclient/tasks/python_2_3_test.yml

diff --git a/roles/ipaclient/tasks/install.yml b/roles/ipaclient/tasks/install.yml
index 48f85f19..297102d1 100644
--- a/roles/ipaclient/tasks/install.yml
+++ b/roles/ipaclient/tasks/install.yml
@@ -7,6 +7,10 @@
     state: present
   with_items: "{{ ipaclient_packages }}"
 
+- name: Install - Include Python2/3 import test
+  include: "{{role_path}}/tasks/python_2_3_test.yml"
+  static: yes
+
 - name: Install - IPA discovery
   ipadiscovery:
     domain: "{{ ipaclient_domain | default(omit) }}"
diff --git a/roles/ipaclient/tasks/python_2_3_test.yml b/roles/ipaclient/tasks/python_2_3_test.yml
new file mode 100644
index 00000000..04270496
--- /dev/null
+++ b/roles/ipaclient/tasks/python_2_3_test.yml
@@ -0,0 +1,15 @@
+- block:
+  - name: Verify Python3 import
+    command: python3 -c "from ipapython.version import NUM_VERSION"
+    register: py3test
+    failed_when: False
+
+  - name: Set python interpreter to 3
+    set_fact:
+      ansible_python_interpreter: "/usr/bin/python3"
+    when: py3test.rc == 0
+
+  - name: Set python interpreter to 2
+    set_fact:
+      ansible_python_interpreter: "/usr/bin/python2"
+    when: py3test.failed or py3test.rc != 0
diff --git a/roles/ipaclient/tasks/uninstall.yml b/roles/ipaclient/tasks/uninstall.yml
index acd0e543..ba644158 100644
--- a/roles/ipaclient/tasks/uninstall.yml
+++ b/roles/ipaclient/tasks/uninstall.yml
@@ -1,6 +1,10 @@
 ---
 # tasks to uninstall IPA client
 
+- name: Uninstall - Include Python2/3 import test
+  include: "{{role_path}}/tasks/python_2_3_test.yml"
+  static: yes
+
 - name: Uninstall - Uninstall IPA client
   ipaclient:
     state: absent
diff --git a/roles/ipaclient/vars/Fedora-25.yml b/roles/ipaclient/vars/Fedora-25.yml
index 064ca8bf..1a62ffa2 100644
--- a/roles/ipaclient/vars/Fedora-25.yml
+++ b/roles/ipaclient/vars/Fedora-25.yml
@@ -1,2 +1,2 @@
 ipaclient_packages: [ "ipa-client", "libselinux-python" ]
-ansible_python_interpreter: '/usr/bin/python2'
\ No newline at end of file
+#ansible_python_interpreter: '/usr/bin/python2'
\ No newline at end of file
diff --git a/roles/ipaclient/vars/Fedora-26.yml b/roles/ipaclient/vars/Fedora-26.yml
index 064ca8bf..1a62ffa2 100644
--- a/roles/ipaclient/vars/Fedora-26.yml
+++ b/roles/ipaclient/vars/Fedora-26.yml
@@ -1,2 +1,2 @@
 ipaclient_packages: [ "ipa-client", "libselinux-python" ]
-ansible_python_interpreter: '/usr/bin/python2'
\ No newline at end of file
+#ansible_python_interpreter: '/usr/bin/python2'
\ No newline at end of file
diff --git a/roles/ipaclient/vars/RedHat-7.3.yml b/roles/ipaclient/vars/RedHat-7.3.yml
index 888aceae..2501eab5 100644
--- a/roles/ipaclient/vars/RedHat-7.3.yml
+++ b/roles/ipaclient/vars/RedHat-7.3.yml
@@ -1,4 +1,4 @@
 # defaults file for ipaclient
 # vars/rhel.yml
 ipaclient_packages: [ "ipa-client", "ipa-admintools", "libselinux-python" ]
-ansible_python_interpreter: '/usr/bin/python2'
+#ansible_python_interpreter: '/usr/bin/python2'
diff --git a/roles/ipaclient/vars/RedHat-7.yml b/roles/ipaclient/vars/RedHat-7.yml
index 3f32a1bd..51ab7bfc 100644
--- a/roles/ipaclient/vars/RedHat-7.yml
+++ b/roles/ipaclient/vars/RedHat-7.yml
@@ -1,4 +1,4 @@
 # defaults file for ipaclient
 # vars/rhel.yml
 ipaclient_packages: [ "ipa-client", "libselinux-python" ]
-ansible_python_interpreter: '/usr/bin/python2'
+#ansible_python_interpreter: '/usr/bin/python2'
diff --git a/roles/ipaclient/vars/default.yml b/roles/ipaclient/vars/default.yml
index 81c13a98..54c64d1c 100644
--- a/roles/ipaclient/vars/default.yml
+++ b/roles/ipaclient/vars/default.yml
@@ -1,4 +1,4 @@
 # defaults file for ipaclient
 # vars/default.yml
 ipaclient_packages: [ "ipa-client", "libselinux-python" ]
-ansible_python_interpreter: '/usr/bin/python3'
+#ansible_python_interpreter: '/usr/bin/python3'
-- 
GitLab