From 43024a264e91f80f2733d093d01c974fa3e49d49 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Thu, 19 Sep 2019 12:13:07 +0200
Subject: [PATCH] Add conda_path variable

Avoid hard-coding /opt/conda
---
 handlers/main.yml                   |  2 +-
 molecule/default/side_effect.yml    |  2 ++
 molecule/default/test_conda.yml     | 27 ++++++++++++++++++++++-----
 molecule/default/test_conda_env.yml |  8 ++++----
 tasks/create_conda_env.yml          |  2 +-
 tasks/install.yml                   | 14 +++++++-------
 vars/main.yml                       |  1 +
 7 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/handlers/main.yml b/handlers/main.yml
index c674071..ff3e9a2 100644
--- a/handlers/main.yml
+++ b/handlers/main.yml
@@ -1,3 +1,3 @@
 ---
 - name: create conda environment
-  command: "/opt/conda/bin/conda env create --force -f {{ env_file_path }}"
+  command: "{{ conda_path }}/bin/conda env create --force -f {{ env_file_path }}"
diff --git a/molecule/default/side_effect.yml b/molecule/default/side_effect.yml
index bac1d0c..ad896e8 100644
--- a/molecule/default/side_effect.yml
+++ b/molecule/default/side_effect.yml
@@ -1,6 +1,8 @@
 ---
 - name: Side Effect
   hosts: ics-ans-role-conda-default
+  vars:
+    conda_path: /opt/conda
   become: true
   tasks:
     - name: test conda module
diff --git a/molecule/default/test_conda.yml b/molecule/default/test_conda.yml
index b7f53e7..f189fde 100644
--- a/molecule/default/test_conda.yml
+++ b/molecule/default/test_conda.yml
@@ -1,11 +1,28 @@
 ---
+- name: display conda version
+  command: "{{ conda_path }}/bin/conda --version"
+  register: conda_version
+
 - name: make sure conda environments don't exist
   file:
-    path: "/opt/conda/envs/{{ item }}"
+    path: "{{ conda_path }}/envs/{{ item }}"
     state: absent
   loop:
     - myapp
     - python3
+    - condaforge
+
+- name: try to install a package that doesn't exist
+  conda:
+    name: foo
+    state: present
+  register: foo_install
+  ignore_errors: true
+
+- name: verify that the command failed
+  assert:
+    that:
+      - foo_install.failed
 
 - name: install flask in myapp
   conda:
@@ -21,7 +38,7 @@
       - flask_install.changed
 
 - name: verify that flask is installed
-  command: /opt/conda/envs/myapp/bin/flask --version
+  command: "{{ conda_path }}/envs/myapp/bin/flask --version"
 
 - name: update flask in myapp
   conda:
@@ -48,7 +65,7 @@
       - flask_remove.changed
 
 - name: verify that flask was removed
-  command: /opt/conda/envs/myapp/bin/flask --version
+  command: "{{ conda_path }}/envs/myapp/bin/flask --version"
   register: flask_cmd
   failed_when: "flask_cmd.rc == 0"
 
@@ -76,7 +93,7 @@
       - python_install.changed
 
 - name: check Python version
-  command: /opt/conda/envs/python3/bin/python --version
+  command: "{{ conda_path }}/envs/python3/bin/python --version"
   register: python_version
 
 - name: verify Python 3.7.1 was installed
@@ -97,7 +114,7 @@
       - python_update.changed
 
 - name: check Python version
-  command: /opt/conda/envs/python3/bin/python --version
+  command: "{{ conda_path }}/envs/python3/bin/python --version"
   register: python_version
 
 - name: verify Python was updated
diff --git a/molecule/default/test_conda_env.yml b/molecule/default/test_conda_env.yml
index 48f3367..837cf7c 100644
--- a/molecule/default/test_conda_env.yml
+++ b/molecule/default/test_conda_env.yml
@@ -1,7 +1,7 @@
 ---
 - name: make sure conda environments don't exist
   file:
-    path: "/opt/conda/envs/{{ item }}"
+    path: "{{ conda_path }}/envs/{{ item }}"
     state: absent
   loop:
     - myenv
@@ -28,7 +28,7 @@
       - myenv_creation.changed
 
 - name: check Python version in the created environment
-  command: /opt/conda/envs/myenv/bin/python --version
+  command: "{{ conda_path }}/envs/myenv/bin/python --version"
   register: python_version
 
 - name: verify Python 3.7.1 was installed
@@ -59,7 +59,7 @@
       - myenv_update2.changed
 
 - name: check Python version in the updated environment
-  command: /opt/conda/envs/myenv/bin/python --version
+  command: "{{ conda_path }}/envs/myenv/bin/python --version"
   register: python_version
 
 - name: verify Python 3.7.3 was installed
@@ -80,7 +80,7 @@
 
 - name: check if myenv exists
   stat:
-    path: /opt/conda/envs/myenv
+    path: "{{ conda_path }}/envs/myenv"
   register: myenv_stat
 
 - name: verify that myenv was removed
diff --git a/tasks/create_conda_env.yml b/tasks/create_conda_env.yml
index bcc8990..807398c 100644
--- a/tasks/create_conda_env.yml
+++ b/tasks/create_conda_env.yml
@@ -2,7 +2,7 @@
 - block:
     - name: set env_file_path
       set_fact:
-        env_file_path: "/opt/conda/{{ env_file | basename }}"
+        env_file_path: "{{ conda_path }}/{{ env_file | basename }}"
 
     - name: copy local conda environment file
       copy:
diff --git a/tasks/install.yml b/tasks/install.yml
index dafcb48..4fb5d1e 100644
--- a/tasks/install.yml
+++ b/tasks/install.yml
@@ -7,7 +7,7 @@
     state: present
 
 - name: check if conda is installed
-  command: /opt/conda/bin/conda --version 2>&1
+  command: "{{ conda_path }}/bin/conda --version 2>&1"
   check_mode: false
   failed_when: false
   changed_when: false
@@ -15,7 +15,7 @@
 
 - name: create conda directory
   file:
-    path: /opt/conda
+    path: "{{ conda_path }}"
     state: directory
     mode: 0755
     owner: root
@@ -25,26 +25,26 @@
   get_url:
     url: "{{ conda_installer_url }}"
     checksum: md5:{{ conda_installer_md5 }}
-    dest: /opt/conda
+    dest: "{{ conda_path }}"
     mode: 0644
   when: run_conda_version.rc != 0
 
 - name: install conda
   command: >
-    bash /opt/conda/{{ conda_installer }} -p /opt/conda -b -f
+    bash {{ conda_path }}/{{ conda_installer }} -p {{ conda_path }} -b -f
   when: run_conda_version.rc != 0
 
 - name: create condarc file
   template:
     src: system_condarc.j2
-    dest: /opt/conda/.condarc
+    dest: "{{ conda_path }}/.condarc"
     owner: root
     group: root
     mode: 0644
 
 - name: delete miniconda installer
   file:
-    path: /opt/conda/{{ conda_installer }}
+    path: "{{ conda_path }}/{{ conda_installer }}"
     state: absent
 
 # Always install conda from conda-forge
@@ -59,7 +59,7 @@
 
 - name: create link to conda.sh profile
   file:
-    src: /opt/conda/etc/profile.d/conda.sh
+    src: "{{ conda_path }}/etc/profile.d/conda.sh"
     dest: /etc/profile.d/conda.sh
     state: link
 
diff --git a/vars/main.yml b/vars/main.yml
index 32229e3..3f66c0a 100644
--- a/vars/main.yml
+++ b/vars/main.yml
@@ -2,3 +2,4 @@
 conda_installer: Miniconda3-4.6.14-Linux-x86_64.sh
 conda_installer_md5: 718259965f234088d785cad1fbd7de03
 conda_installer_url: "https://artifactory.esss.lu.se/artifactory/swi-pkg/anaconda/{{ conda_installer }}"
+conda_path: /opt/conda
-- 
GitLab