From 01d29f93a0ed8302181898789664c914c8d83f77 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Tue, 26 Nov 2019 11:01:00 +0100
Subject: [PATCH] Add force option to conda_env module

JIRA INFRA-1563 #action In Progress
---
 README.md                           |  6 ++++++
 library/conda_env.py                | 24 +++++++++++++++++++++++-
 molecule/default/test_conda_env.yml | 12 ++++++++++++
 3 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 4a8997f..45b9ab5 100644
--- a/README.md
+++ b/README.md
@@ -103,6 +103,12 @@ The `conda_env` module allows to create, update or remove conda environments.
     prune: true
     file: /tmp/environment.yml
 
+- name: force the environment creation (remove previous one first)
+  conda_env:
+    state: present
+    force: true
+    file: /tmp/environment.yml
+
 - name: remove myenv environment
   conda_env:
     name: myenv
diff --git a/library/conda_env.py b/library/conda_env.py
index ce595de..d017ee2 100644
--- a/library/conda_env.py
+++ b/library/conda_env.py
@@ -56,6 +56,11 @@ options:
         description:
             - Remove installed packages not defined in environment.yml. Can only be used with state c(present).
         type: bool
+    force:
+        description:
+            - Force the creation of an environment by first removing a previously existing environment of the same name.
+              Can only be used with state c(present).
+        type: bool
 
 requirements:
   - conda >= 4.7.12
@@ -83,6 +88,12 @@ EXAMPLES = """
     prune: true
     file: /tmp/environment.yml
 
+- name: force the environment creation (remove previous one first)
+  conda_env:
+    state: present
+    force: true
+    file: /tmp/environment.yml
+
 - name: remove myenv environment
   conda_env:
     name: myenv
@@ -264,6 +275,13 @@ class Conda:
             args.append("--prune")
         return self.run_conda("update", subcmd="env", *args)
 
+    def env_create(self, file, force=False):
+        """Create the environment based on file"""
+        args = ["-f", file] + self.env_args
+        if force:
+            args.append("--force")
+        return self.run_conda("create", subcmd="env", *args)
+
     def env_remove(self):
         """Remove the given environment"""
         return self.run_conda("remove", "--all", *self.env_args)
@@ -276,6 +294,7 @@ def run_module():
         executable=dict(type="path"),
         file=dict(type="path"),
         prune=dict(type="bool", default=False),
+        force=dict(type="bool", default=False),
     )
 
     module = AnsibleModule(
@@ -293,7 +312,10 @@ def run_module():
     )
 
     if state == "present":
-        result = conda.env_update(module.params["file"], module.params["prune"])
+        if module.params["force"]:
+            result = conda.env_create(module.params["file"], module.params["force"])
+        else:
+            result = conda.env_update(module.params["file"], module.params["prune"])
     elif state == "latest":
         result = conda.update_all()
     elif state == "absent":
diff --git a/molecule/default/test_conda_env.yml b/molecule/default/test_conda_env.yml
index 837cf7c..83debc2 100644
--- a/molecule/default/test_conda_env.yml
+++ b/molecule/default/test_conda_env.yml
@@ -47,6 +47,18 @@
     that:
       - not myenv_update1.changed
 
+- name: force myenv creation with the same environment.yml file
+  conda_env:
+    name: myenv
+    force: true
+    file: /tmp/environment_python371.yml
+  register: myenv_force1
+
+- name: verify we recorded a change
+  assert:
+    that:
+      - myenv_force1.changed
+
 - name: update myenv with a new environment.yml file
   conda_env:
     name: myenv
-- 
GitLab