From ee6d99ca92cc10778d174c442bc35f4d1ae1b1e2 Mon Sep 17 00:00:00 2001 From: lmfaber Date: Tue, 29 Apr 2025 16:01:22 +0200 Subject: [PATCH 1/9] Add possibility to create environments. --- .ansible-lint | 0 .pre-commit-config.yaml | 0 LICENSE | 0 README.md | 0 defaults/main.yml | 2 +- meta/main.yml | 2 +- molecule/default/conda-linux-64.lock | 0 molecule/default/converge.yml | 0 molecule/default/environmentyaml | 0 molecule/default/molecule.yml | 0 molecule/default/tasks/test-prefix.yml | 0 molecule/default/verify.yml | 0 tasks/create_envs.yml | 21 +++++++++++ tasks/download-and-extract.yml | 0 tasks/main.yml | 51 ++------------------------ vars/main.yml | 0 16 files changed, 26 insertions(+), 50 deletions(-) mode change 100644 => 100755 .ansible-lint mode change 100644 => 100755 .pre-commit-config.yaml mode change 100644 => 100755 LICENSE mode change 100644 => 100755 README.md mode change 100644 => 100755 defaults/main.yml mode change 100644 => 100755 meta/main.yml mode change 100644 => 100755 molecule/default/conda-linux-64.lock mode change 100644 => 100755 molecule/default/converge.yml mode change 100644 => 100755 molecule/default/environmentyaml mode change 100644 => 100755 molecule/default/molecule.yml mode change 100644 => 100755 molecule/default/tasks/test-prefix.yml mode change 100644 => 100755 molecule/default/verify.yml create mode 100755 tasks/create_envs.yml mode change 100644 => 100755 tasks/download-and-extract.yml mode change 100644 => 100755 tasks/main.yml mode change 100644 => 100755 vars/main.yml diff --git a/.ansible-lint b/.ansible-lint old mode 100644 new mode 100755 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/defaults/main.yml b/defaults/main.yml old mode 100644 new mode 100755 index 5e0f75d..bde0675 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -3,4 +3,4 @@ version: latest dest: /usr/local/bin/micromamba channels: - conda-forge -packages: [] +packages: [] \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml old mode 100644 new mode 100755 index 8f3cb20..421de31 --- a/meta/main.yml +++ b/meta/main.yml @@ -24,4 +24,4 @@ galaxy_info: - mamba - micromamba -dependencies: [] +dependencies: [] \ No newline at end of file diff --git a/molecule/default/conda-linux-64.lock b/molecule/default/conda-linux-64.lock old mode 100644 new mode 100755 diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml old mode 100644 new mode 100755 diff --git a/molecule/default/environmentyaml b/molecule/default/environmentyaml old mode 100644 new mode 100755 diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml old mode 100644 new mode 100755 diff --git a/molecule/default/tasks/test-prefix.yml b/molecule/default/tasks/test-prefix.yml old mode 100644 new mode 100755 diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml old mode 100644 new mode 100755 diff --git a/tasks/create_envs.yml b/tasks/create_envs.yml new file mode 100755 index 0000000..313e009 --- /dev/null +++ b/tasks/create_envs.yml @@ -0,0 +1,21 @@ +- name: Check if environment exists + stat: + path: "{{ root_prefix }}/{{ item.key }}" + register: env_stat + loop: "{{ envs | dict2items }}" + loop_control: + loop_var: item + +- name: Create environments with micromamba + command: > + {{ dest }} create + --name={{ item.key }} + --root-prefix={{ root_prefix }} + {{ item.value.channels | map('regex_replace', '^', '--channel=') | join(' ') }} + {{ item.value.packages | join(' ') }} + --yes + when: env_stat.results[ansible_loop.index0].stat.exists == false + loop: "{{ envs | dict2items }}" + loop_control: + loop_var: item + extended: true diff --git a/tasks/download-and-extract.yml b/tasks/download-and-extract.yml old mode 100644 new mode 100755 diff --git a/tasks/main.yml b/tasks/main.yml old mode 100644 new mode 100755 index 1ee9f26..fc4835b --- a/tasks/main.yml +++ b/tasks/main.yml @@ -7,41 +7,6 @@ ansible.builtin.import_tasks: download-and-extract.yml when: not dest_stat.stat.exists -- name: Check if root_prefix is defined but doesn't exist - ansible.builtin.stat: - path: "{{ root_prefix }}" - register: root_prefix_stat - when: root_prefix is defined - -- name: Build first part of arguments - ansible.builtin.set_fact: - first_args: - - micromamba - - install - - --yes - - "--root-prefix={{ root_prefix }}" - - "{{ ('--name=' + name) if name is defined else '' }}" - - "{{ ('--file=' + file) if file is defined else '' }}" - - "{{ ('--prefix=' + (prefix | default(root_prefix))) if name is not defined else '' }}" - when: (root_prefix_stat.stat is defined) and not root_prefix_stat.stat.exists - -# Note: at this point, 'first_args' is defined by the previous task exactly when -# 1. the root_prefix has been specified, and -# 2. the corresponding directory does not yet exist. -# This coincides with the condition that we want to proceed with the -# 'micromamba install' command. Thus it's convenient to checking 'first_args is defined' -# in what follows. - -- name: Build channel part of arguments - ansible.builtin.set_fact: - channel_args: "{{ ['--channel='] | product(channels) | map('join') | list }}" - when: first_args is defined - -- name: Create a new conda environment - ansible.builtin.command: - argv: "{{ (first_args + channel_args + packages) | reject('equalto', '') | list }}" - when: first_args is defined - - name: Create .condarc file ansible.builtin.copy: dest: "{{ root_prefix }}/.condarc" @@ -54,19 +19,9 @@ path: "/etc/bash.bashrc" register: file_stat_bash_bashrc -- name: Enable micromamba in bash (ubuntu/debian) - ansible.builtin.lineinfile: - path: "/etc/bash.bashrc" - regexp: '^eval "\$\(micromamba shell hook --shell=bash\)"$' - line: eval "$(micromamba shell hook --shell=bash)" - insertafter: EOF - state: present - when: file_stat_bash_bashrc.stat.exists and file_stat_bash_bashrc.stat.isreg - -- name: Check if /etc/bashrc is present (redhat-like) - ansible.builtin.stat: - path: "/etc/bashrc" - register: file_stat_bashrc +- name: Create environments + include_tasks: create_envs.yml + when: envs is defined - name: Enable micromamba in bash (redhat-like) ansible.builtin.lineinfile: diff --git a/vars/main.yml b/vars/main.yml old mode 100644 new mode 100755 From 556c71b01a17265a0940d997c2f58d80d9934132 Mon Sep 17 00:00:00 2001 From: lmfaber Date: Tue, 29 Apr 2025 17:10:34 +0200 Subject: [PATCH 2/9] Fix env creation. --- tasks/create_envs.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tasks/create_envs.yml b/tasks/create_envs.yml index 313e009..32d32dd 100755 --- a/tasks/create_envs.yml +++ b/tasks/create_envs.yml @@ -1,13 +1,13 @@ - name: Check if environment exists - stat: - path: "{{ root_prefix }}/{{ item.key }}" + ansible.builtin.stat: + path: "{{ prefix }}/{{ item.key }}" register: env_stat loop: "{{ envs | dict2items }}" loop_control: loop_var: item - name: Create environments with micromamba - command: > + ansible.builtin.command: > {{ dest }} create --name={{ item.key }} --root-prefix={{ root_prefix }} From e8f87d4a5616e41f8822e491588eff39b93b96a9 Mon Sep 17 00:00:00 2001 From: lmfaber Date: Tue, 29 Apr 2025 17:10:47 +0200 Subject: [PATCH 3/9] Fix shell init. --- tasks/main.yml | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/tasks/main.yml b/tasks/main.yml index fc4835b..b5adee0 100755 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -14,20 +14,34 @@ mode: "a+r,u+w" when: (first_args is defined) and (root_prefix_condarc is defined) +- name: Create environments + ansible.builtin.import_tasks: create_envs.yml + when: envs is defined + - name: Check if /etc/bash.bashrc is present (ubuntu/debian) ansible.builtin.stat: path: "/etc/bash.bashrc" register: file_stat_bash_bashrc -- name: Create environments - include_tasks: create_envs.yml - when: envs is defined +- name: Enable micromamba in bash (ubuntu/debian) + ansible.builtin.lineinfile: + path: "/etc/bash.bashrc" + regexp: '^eval "\$\({{ dest }} shell hook --shell=bash\)"$' + line: eval "$({{ dest }} shell hook --shell=bash)" + insertafter: EOF + state: present + when: file_stat_bash_bashrc.stat.exists and file_stat_bash_bashrc.stat.isreg + +- name: Check if /etc/bashrc is present (redhat-like) + ansible.builtin.stat: + path: "/etc/bashrc" + register: file_stat_bashrc - name: Enable micromamba in bash (redhat-like) ansible.builtin.lineinfile: path: "/etc/bashrc" - regexp: '^eval "\$\(micromamba shell hook --shell=bash\)"$' - line: eval "$(micromamba shell hook --shell=bash)" + regexp: '^eval "\$\({{ dest }} shell hook --shell=bash\)"$' + line: eval "$({{ dest }} shell hook --shell=bash)" insertafter: EOF state: present when: file_stat_bashrc.stat.exists and file_stat_bashrc.stat.isreg @@ -40,8 +54,8 @@ - name: Enable micromamba in zsh if present ansible.builtin.lineinfile: path: "/etc/zsh/zshrc" - regexp: '^eval "\$\(micromamba shell hook --shell=zsh\)"$' - line: eval "$(micromamba shell hook --shell=zsh)" + regexp: '^eval "\$\({{ dest }} shell hook --shell=zsh\)"$' + line: eval "$({{ dest }} shell hook --shell=zsh)" insertafter: EOF state: present when: file_stat_zshrc.stat.exists and file_stat_zshrc.stat.isreg From 861806cc8730ff16ea771f1b5107de6c1b403d06 Mon Sep 17 00:00:00 2001 From: lmfaber Date: Tue, 29 Apr 2025 17:11:24 +0200 Subject: [PATCH 4/9] Add example in README.md. --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 5f62c5b..f452ae8 100755 --- a/README.md +++ b/README.md @@ -118,6 +118,34 @@ This downloads `micromamba` into `/tmp/micromamba` and creates a new root prefix This creates a new root prefix in `/home/conda-user/micromamba` and creates a conda environment without Python. It also places a `.condarc` file in the root prefix to configure packages to be installed by default from the `conda-forge` channel. +--- + +```yaml +- hosts: servers + become: yes + become_user: condauser + roles: + - mambaorg.micromamba + vars: + dest: "~/micromamba/micromamba" # Executable location + root_prefix: "~/micromamba" # Installation location + prefix: "{{ galaxy_root }}/micromamba/envs" # Environment locations + envs: + env_one: + channels: + - conda-forge + packages: + - python=3.12 + env_two: + channels: + - conda-forge + - bioconda + packages: + - python=3.11 +``` + +This creates a new root prefix in `/home/condauser/micromamba` and creates two conda environments with different channels and packages. + ## Subsequent Usage In order run any commands from a conda environment, it must first be *activated*. Activation involves altering the `PATH` and other environment variables in the active shell (usually Bash). This can be accomplished in various ways. From e10e4b7ba0130962295f718b8c0e0f5fb5844092 Mon Sep 17 00:00:00 2001 From: lmfaber Date: Wed, 30 Apr 2025 10:43:41 +0200 Subject: [PATCH 5/9] Revert executable permissions and removal of new lines. --- .ansible-lint | 0 .pre-commit-config.yaml | 0 LICENSE | 0 README.md | 0 defaults/main.yml | 2 +- meta/main.yml | 2 +- molecule/default/conda-linux-64.lock | 0 molecule/default/converge.yml | 0 molecule/default/environmentyaml | 0 molecule/default/molecule.yml | 0 molecule/default/tasks/test-prefix.yml | 0 molecule/default/verify.yml | 0 tasks/create_envs.yml | 0 tasks/download-and-extract.yml | 0 tasks/main.yml | 0 vars/main.yml | 0 16 files changed, 2 insertions(+), 2 deletions(-) mode change 100755 => 100644 .ansible-lint mode change 100755 => 100644 .pre-commit-config.yaml mode change 100755 => 100644 LICENSE mode change 100755 => 100644 README.md mode change 100755 => 100644 defaults/main.yml mode change 100755 => 100644 meta/main.yml mode change 100755 => 100644 molecule/default/conda-linux-64.lock mode change 100755 => 100644 molecule/default/converge.yml mode change 100755 => 100644 molecule/default/environmentyaml mode change 100755 => 100644 molecule/default/molecule.yml mode change 100755 => 100644 molecule/default/tasks/test-prefix.yml mode change 100755 => 100644 molecule/default/verify.yml mode change 100755 => 100644 tasks/create_envs.yml mode change 100755 => 100644 tasks/download-and-extract.yml mode change 100755 => 100644 tasks/main.yml mode change 100755 => 100644 vars/main.yml diff --git a/.ansible-lint b/.ansible-lint old mode 100755 new mode 100644 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml old mode 100755 new mode 100644 diff --git a/LICENSE b/LICENSE old mode 100755 new mode 100644 diff --git a/README.md b/README.md old mode 100755 new mode 100644 diff --git a/defaults/main.yml b/defaults/main.yml old mode 100755 new mode 100644 index bde0675..5e0f75d --- a/defaults/main.yml +++ b/defaults/main.yml @@ -3,4 +3,4 @@ version: latest dest: /usr/local/bin/micromamba channels: - conda-forge -packages: [] \ No newline at end of file +packages: [] diff --git a/meta/main.yml b/meta/main.yml old mode 100755 new mode 100644 index 421de31..8f3cb20 --- a/meta/main.yml +++ b/meta/main.yml @@ -24,4 +24,4 @@ galaxy_info: - mamba - micromamba -dependencies: [] \ No newline at end of file +dependencies: [] diff --git a/molecule/default/conda-linux-64.lock b/molecule/default/conda-linux-64.lock old mode 100755 new mode 100644 diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml old mode 100755 new mode 100644 diff --git a/molecule/default/environmentyaml b/molecule/default/environmentyaml old mode 100755 new mode 100644 diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml old mode 100755 new mode 100644 diff --git a/molecule/default/tasks/test-prefix.yml b/molecule/default/tasks/test-prefix.yml old mode 100755 new mode 100644 diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml old mode 100755 new mode 100644 diff --git a/tasks/create_envs.yml b/tasks/create_envs.yml old mode 100755 new mode 100644 diff --git a/tasks/download-and-extract.yml b/tasks/download-and-extract.yml old mode 100755 new mode 100644 diff --git a/tasks/main.yml b/tasks/main.yml old mode 100755 new mode 100644 diff --git a/vars/main.yml b/vars/main.yml old mode 100755 new mode 100644 From edad8ea6b76338f9a9afb6326678bd9030e0325d Mon Sep 17 00:00:00 2001 From: lmfaber Date: Wed, 30 Apr 2025 10:48:18 +0200 Subject: [PATCH 6/9] Update README.md to reflect preferred definition of versions. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f452ae8..3889d21 100644 --- a/README.md +++ b/README.md @@ -135,13 +135,13 @@ This creates a new root prefix in `/home/conda-user/micromamba` and creates a co channels: - conda-forge packages: - - python=3.12 + - python=3.12.* env_two: channels: - conda-forge - bioconda packages: - - python=3.11 + - python=3.11.* ``` This creates a new root prefix in `/home/condauser/micromamba` and creates two conda environments with different channels and packages. From ff18e625d277f0d05cfe081debc16c6455718595 Mon Sep 17 00:00:00 2001 From: lmfaber Date: Wed, 30 Apr 2025 11:02:02 +0200 Subject: [PATCH 7/9] Remove unused variable in example. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3889d21..e67f3fb 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ This creates a new root prefix in `/home/conda-user/micromamba` and creates a co vars: dest: "~/micromamba/micromamba" # Executable location root_prefix: "~/micromamba" # Installation location - prefix: "{{ galaxy_root }}/micromamba/envs" # Environment locations + prefix: "~/micromamba/envs" # Environment locations envs: env_one: channels: From 55e56454bff7e81e2bbfa64539e8aa42a836d5f0 Mon Sep 17 00:00:00 2001 From: lmfaber Date: Wed, 30 Apr 2025 11:29:04 +0200 Subject: [PATCH 8/9] Follow existing naming for yml files. --- tasks/{create_envs.yml => create-envs.yml} | 0 tasks/main.yml | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename tasks/{create_envs.yml => create-envs.yml} (100%) diff --git a/tasks/create_envs.yml b/tasks/create-envs.yml similarity index 100% rename from tasks/create_envs.yml rename to tasks/create-envs.yml diff --git a/tasks/main.yml b/tasks/main.yml index b5adee0..579ee98 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -15,7 +15,7 @@ when: (first_args is defined) and (root_prefix_condarc is defined) - name: Create environments - ansible.builtin.import_tasks: create_envs.yml + ansible.builtin.import_tasks: create-envs.yml when: envs is defined - name: Check if /etc/bash.bashrc is present (ubuntu/debian) From ae8b5b4ced844aaac338d9c3209bfe3d00944d53 Mon Sep 17 00:00:00 2001 From: lmfaber Date: Wed, 30 Apr 2025 14:32:06 +0200 Subject: [PATCH 9/9] Separate bashrc tasks. Remove old micromamba statement from shell rc files if present. --- tasks/main.yml | 43 ++-------------------------- tasks/setup-bashrc.yml | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 tasks/setup-bashrc.yml diff --git a/tasks/main.yml b/tasks/main.yml index 579ee98..e6fc7d2 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -18,44 +18,5 @@ ansible.builtin.import_tasks: create-envs.yml when: envs is defined -- name: Check if /etc/bash.bashrc is present (ubuntu/debian) - ansible.builtin.stat: - path: "/etc/bash.bashrc" - register: file_stat_bash_bashrc - -- name: Enable micromamba in bash (ubuntu/debian) - ansible.builtin.lineinfile: - path: "/etc/bash.bashrc" - regexp: '^eval "\$\({{ dest }} shell hook --shell=bash\)"$' - line: eval "$({{ dest }} shell hook --shell=bash)" - insertafter: EOF - state: present - when: file_stat_bash_bashrc.stat.exists and file_stat_bash_bashrc.stat.isreg - -- name: Check if /etc/bashrc is present (redhat-like) - ansible.builtin.stat: - path: "/etc/bashrc" - register: file_stat_bashrc - -- name: Enable micromamba in bash (redhat-like) - ansible.builtin.lineinfile: - path: "/etc/bashrc" - regexp: '^eval "\$\({{ dest }} shell hook --shell=bash\)"$' - line: eval "$({{ dest }} shell hook --shell=bash)" - insertafter: EOF - state: present - when: file_stat_bashrc.stat.exists and file_stat_bashrc.stat.isreg - -- name: Check if zshrc is present - ansible.builtin.stat: - path: "/etc/zsh/zshrc" - register: file_stat_zshrc - -- name: Enable micromamba in zsh if present - ansible.builtin.lineinfile: - path: "/etc/zsh/zshrc" - regexp: '^eval "\$\({{ dest }} shell hook --shell=zsh\)"$' - line: eval "$({{ dest }} shell hook --shell=zsh)" - insertafter: EOF - state: present - when: file_stat_zshrc.stat.exists and file_stat_zshrc.stat.isreg +- name: Setup bashrc + ansible.builtin.import_tasks: setup-bashrc.yml diff --git a/tasks/setup-bashrc.yml b/tasks/setup-bashrc.yml new file mode 100644 index 0000000..14c2734 --- /dev/null +++ b/tasks/setup-bashrc.yml @@ -0,0 +1,64 @@ +- name: Check if /etc/bash.bashrc is present (ubuntu/debian) + ansible.builtin.stat: + path: "/etc/bash.bashrc" + register: file_stat_bash_bashrc + +- name: Enable micromamba in bash (ubuntu/debian) + ansible.builtin.lineinfile: + path: "/etc/bash.bashrc" + regexp: '^eval "\$\({{ dest }} shell hook --shell=bash\)"$' + line: eval "$({{ dest }} shell hook --shell=bash)" + insertafter: EOF + state: present + when: file_stat_bash_bashrc.stat.exists and file_stat_bash_bashrc.stat.isreg + +- name: Replace old micromamba line if present (ubuntu/debian) + ansible.builtin.lineinfile: + path: "/etc/bashrc" + regexp: '^eval "\$\(micromamba shell hook --shell=bash\)"$' + state: absent + when: file_stat_bash_bashrc.stat.exists and file_stat_bash_bashrc.stat.isreg + + +- name: Check if /etc/bashrc is present (redhat-like) + ansible.builtin.stat: + path: "/etc/bashrc" + register: file_stat_bashrc + +- name: Enable micromamba in bash (redhat-like) + ansible.builtin.lineinfile: + path: "/etc/bashrc" + regexp: '^eval "\$\({{ dest }} shell hook --shell=bash\)"$' + line: eval "$({{ dest }} shell hook --shell=bash)" + insertafter: EOF + state: present + when: file_stat_bashrc.stat.exists and file_stat_bashrc.stat.isreg + +- name: Replace old micromamba line if present (redhat-like) + ansible.builtin.lineinfile: + path: "/etc/bashrc" + regexp: '^eval "\$\(micromamba shell hook --shell=bash\)"$' + state: absent + when: file_stat_bashrc.stat.exists and file_stat_bashrc.stat.isreg + + +- name: Check if zshrc is present + ansible.builtin.stat: + path: "/etc/zsh/zshrc" + register: file_stat_zshrc + +- name: Enable micromamba in zsh if present + ansible.builtin.lineinfile: + path: "/etc/zsh/zshrc" + regexp: '^eval "\$\({{ dest }} shell hook --shell=zsh\)"$' + line: eval "$({{ dest }} shell hook --shell=zsh)" + insertafter: EOF + state: present + when: file_stat_zshrc.stat.exists and file_stat_zshrc.stat.isreg + +- name: Replace old micromamba line if present (zsh) + ansible.builtin.lineinfile: + path: "/etc/zsh/zshrc" + regexp: '^eval "\$\(micromamba shell hook --shell=zsh\)"$' + state: absent + when: file_stat_zshrc.stat.exists and file_stat_zshrc.stat.isreg