Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: "~/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.
Expand Down
21 changes: 21 additions & 0 deletions tasks/create-envs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
- name: Check if environment exists
ansible.builtin.stat:
path: "{{ prefix }}/{{ item.key }}"
register: env_stat
loop: "{{ envs | dict2items }}"
loop_control:
loop_var: item

- name: Create environments with micromamba
ansible.builtin.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
80 changes: 5 additions & 75 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,16 @@
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"
content: "{{ root_prefix_condarc | to_nice_yaml }}"
mode: "a+r,u+w"
when: (first_args is defined) and (root_prefix_condarc 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 "\$\(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: 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)"
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: Create environments
ansible.builtin.import_tasks: create-envs.yml
when: envs is defined

- 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)"
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
64 changes: 64 additions & 0 deletions tasks/setup-bashrc.yml
Original file line number Diff line number Diff line change
@@ -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