Skip to content
Snippets Groups Projects
Commit 01d29f93 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Add force option to conda_env module

JIRA INFRA-1563 #action In Progress
parent 18207dc9
No related branches found
No related tags found
No related merge requests found
...@@ -103,6 +103,12 @@ The `conda_env` module allows to create, update or remove conda environments. ...@@ -103,6 +103,12 @@ The `conda_env` module allows to create, update or remove conda environments.
prune: true prune: true
file: /tmp/environment.yml 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 - name: remove myenv environment
conda_env: conda_env:
name: myenv name: myenv
......
...@@ -56,6 +56,11 @@ options: ...@@ -56,6 +56,11 @@ options:
description: description:
- Remove installed packages not defined in environment.yml. Can only be used with state c(present). - Remove installed packages not defined in environment.yml. Can only be used with state c(present).
type: bool 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: requirements:
- conda >= 4.7.12 - conda >= 4.7.12
...@@ -83,6 +88,12 @@ EXAMPLES = """ ...@@ -83,6 +88,12 @@ EXAMPLES = """
prune: true prune: true
file: /tmp/environment.yml 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 - name: remove myenv environment
conda_env: conda_env:
name: myenv name: myenv
...@@ -264,6 +275,13 @@ class Conda: ...@@ -264,6 +275,13 @@ class Conda:
args.append("--prune") args.append("--prune")
return self.run_conda("update", subcmd="env", *args) 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): def env_remove(self):
"""Remove the given environment""" """Remove the given environment"""
return self.run_conda("remove", "--all", *self.env_args) return self.run_conda("remove", "--all", *self.env_args)
...@@ -276,6 +294,7 @@ def run_module(): ...@@ -276,6 +294,7 @@ def run_module():
executable=dict(type="path"), executable=dict(type="path"),
file=dict(type="path"), file=dict(type="path"),
prune=dict(type="bool", default=False), prune=dict(type="bool", default=False),
force=dict(type="bool", default=False),
) )
module = AnsibleModule( module = AnsibleModule(
...@@ -293,7 +312,10 @@ def run_module(): ...@@ -293,7 +312,10 @@ def run_module():
) )
if state == "present": 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": elif state == "latest":
result = conda.update_all() result = conda.update_all()
elif state == "absent": elif state == "absent":
......
...@@ -47,6 +47,18 @@ ...@@ -47,6 +47,18 @@
that: that:
- not myenv_update1.changed - 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 - name: update myenv with a new environment.yml file
conda_env: conda_env:
name: myenv name: myenv
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment