Browse Source

init from home

Gogs 7 years ago
parent
commit
ae754a5d34
5 changed files with 194 additions and 1 deletions
  1. 64 1
      README.md
  2. 18 0
      defaults/main.yml
  3. 91 0
      tasks/main.yml
  4. 18 0
      tasks/remove.yml
  5. 3 0
      vars/main.yml

+ 64 - 1
README.md

@@ -1,2 +1,65 @@
-# ansible-role-anaconda
+ansible-role-anaconda
+========
+This role installes the [Continuum Anaconda](https://www.continuum.io/anaconda-overview) distribution with all its packages (aka Anaconda, ~500 MB installer, ~3 GB installed) or just the conda package manager and Python (aka Miniconda, ~40 MB installer, ~400 MB installed).
 
 
+You choose your desired version of Python and Anaconda by choosing a propper installer-file. Installer-files follow a schema: `(Ana|Mini)conda<Python-major-version>-<Distribution-version>-<Platform>.sh`
+    
+Examples:  
+`Miniconda3-4.3.21-Linux-x86_64.sh` installs the conda package manager and Python-3.x from the distribution version 3.4.21 on 64-bit GNU/Linux.  
+`Anaconda2-4.4.0-Linux-x86.sh` installs the full distribution version 4.4.0 with Python-2.x on 32-bit GNU/Linux.  
+
+Online resource for Installer-files:
+- Anaconda: https://repo.continuum.io/archive/
+- Miniconda: https://repo.continuum.io/miniconda/
+
+If you do not provide a installer file, you get a Miniconda installation with Python3 for x86_64 systems. See [role defaults](defaults/main.yml) for details.
+
+It is possible to install Miniconda and run `conda install anaconda` afterwards to get the full Ananconda distribution.
+
+Be aware that this role sets the path to the Anaconda distribution at the beginning of `$PATH` in a global way for bash/ksh/zsh/ash (e.g. `/etc/bash.bashrc.local`) by default. Keep that in mind if your system depends on a specific python version. Set `anaconda_set_path` to `False` and handle `$PATH` by your own if you don't like this.
+
+The installer-file will reside in `anaconda_home_dir` to detect version changes. Despite the fact, that you can up/downgrade your Anaconda distribution with the conda package manager, a full reinstall (uninstall first, new install afterwards) will be performed if the installer-file or `anaconda_installer_uri` change. As long as you don't change `anaconda_installer_uri` and the installer-file, your Anaconda installation will not be changed.
+
+
+Requirements
+------------
+This role should be almost OS-agnostic as long as you provide a suitable installer file and the target is a Unix-like system.
+The target hosts need access to `anaconda_installer_uri` resource. But this can be a local resource on the target host with `file:///tmp/foo`. So a full offline installation can be done in a short playbook.
+
+
+Role Variables
+--------------
+| parameter | required | default | choices | comments |
+| :-------- | :------- | :------ | :-----: | :------- |
+| anaconda_state | no | `present` | `present`, `absent` | Desired state of the Anaconda distribution. If `absent`, it tries to remove the Anaconda path from `$PATH`. |
+| anaconda_home_dir | no | `/opt/anaconda` | | Path to Anaconda installation. |
+| anaconda_installer_uri | no | see [defaults](defaults/main.yml) | | URI to the installer file. Use `https://` for remote or `file://` for local files on the target host. The target host requires access to that file. See above for online resources. |
+| anaconda_installer_md5 | no | see [defaults](defaults/main.yml) | | The md5 checksum of the installer file. See above for online resources. |
+| anaconda_force_install | no | False | boolean | Run installer even if the installer-file is unchanged and Anaconda seems to be installed. |
+| anaconda_set_path | no | True | boolean | Adds Anaconda's path to the beginning of `$PATH` for all bash/ksh/zsh/ash login shells. See above for details. |
+
+Example playbooks
+-----------------
+    # installs Miniconda
+    - host: python-dev-host
+      roles:
+        - ansible-role-anaconda
+    
+    # installes full Anaconda with Python 2.x into /usr/local/ but don't touch $PATH
+    - host: python-dev-host
+      roles:
+        - role: ansible-role-anaconda
+          anaconda_installer_uri: https://repo.continuum.io/archive/Anaconda2-4.4.0-Linux-x86_64.sh
+          anaconda_installer_md5: d72add23bc937ccdfc7de4f47deff843
+          anaconda_home_dir: /usr/local
+          anaconda_set_path: False
+          
+    # removes anacona from its default home_dir and removes it from $PATH
+    - host: python-dev-host
+      roles:
+        - { role: ansible-role-anaconda, anaconda_state: absent }
+
+
+Dependencies
+------------
+None.

+ 18 - 0
defaults/main.yml

@@ -0,0 +1,18 @@
+---
+# Choose your version of Anaconda by choosing a propper installer. 
+# - for Miniconda: https://repo.continuum.io/miniconda/
+# - for Anaconda: https://repo.continuum.io/archive/
+anaconda_installer_uri: "https://repo.continuum.io/miniconda/Miniconda3-4.3.21-Linux-x86_64.sh"
+anaconda_installer_md5: "c1c15d3baba15bf50293ae963abef853"
+
+# install per default
+anaconda_state: present
+
+# home folder of the installation
+anaconda_home_dir: "/opt/anaconda"
+
+# run installer even if Anaconda is already installed
+anaconda_force_install: False
+
+# set anaconda binary path at the beginning of $PATH
+anaconda_set_path: True

+ 91 - 0
tasks/main.yml

@@ -0,0 +1,91 @@
+---
+# pre installation tasks
+# ======================
+
+- name: check var anaconda_state
+  fail:
+    msg: "anaconda_state '{{ anaconda_state }}' not supported. Use 'present' or 'absent'."
+  when: anaconda_state != "present" and anaconda_state != "absent"
+
+- block:
+
+  - name: ensure home dir exists
+    file:
+      dest: "{{ anaconda_home_dir }}"
+      state: directory
+
+  - name: download installer file exists
+    get_url:
+      url: "{{ anaconda_installer_uri }}"
+      dest: "{{ anaconda_home_dir }}/installer.sh"
+      checksum: "md5:{{ anaconda_installer_md5 }}"
+    register: installer_file
+
+  - name: write warning into Anaconda home dir
+    copy:
+      dest: "{{ anaconda_home_dir }}/do_not_touch_installer.sh.txt"
+      content: |
+        Do not touch the installer.sh file because ansible uses
+        it to decide if a reinstallation (uninstall/install) is
+        necessary.
+
+  - name: check if Anaconda is already installed
+    stat:
+      path: "{{ anaconda_install_dir }}/bin/conda"
+    register: conda_binary
+
+  when: "anaconda_state == 'present'"
+
+  rescue:
+    - name: failed to prepare installation
+      fail:
+        msg: "leave installed Anaconda (if any) untouched, installer file may be changed"
+
+# uninstall
+# =========
+# if the installer file changes, we assume a new version should be installed
+# (the Anaconda way recommend a 'conda update' or similar instead of a new
+# installation but we won't to that here to kiss)
+- include: remove.yml
+  when: (installer_file|changed and conda_binary.stat.exists) or anaconda_state == 'absent'
+
+
+# install
+# =======
+- block:
+
+  - name: run installer
+    command: "bash installer.sh -b -p {{ anaconda_install_dir }} {{ anaconda_force_install|ternary('-f', '') }}"
+    args:
+      chdir: "{{ anaconda_home_dir }}"
+    failed_when: false
+    register: installer_out
+
+  - name: fail if installer finished unsuccessfully
+    fail:
+      msg: "{{ installer_out.stderr }}"
+    when: "not (('ERROR: File or directory already exists' in installer_out.stderr) or ('installation finished.' in installer_out.stdout))"
+
+  when: "anaconda_state == 'present' and (installer_file|changed or anaconda_force_install)"
+
+  rescue:
+    - name: rollback installation
+      include: remove.yml
+    - fail: msg="Installation failed! Rolled back installation!"
+
+# post install tasks
+# ==================
+- name: set path to anaconda
+  lineinfile:
+    dest: "{{ item }}"
+    create: true
+    state: present
+    insertafter: EOF
+    line: 'export PATH="{{ anaconda_install_dir }}/bin:$PATH"'
+  with_items:
+    - /etc/bash.bashrc.local
+    - /etc/ksh.kshrc.local
+    - /etc/zsh.zshrc.local
+    - /etc/ash.ashrc.local
+  when: "anaconda_state == 'present' and anaconda_set_path|bool"
+

+ 18 - 0
tasks/remove.yml

@@ -0,0 +1,18 @@
+---
+
+- name: remove Anaconda home dir
+  file:
+    dest: "{{ anaconda_home_dir }}"
+    state: absent
+
+- name: remove path to anaconda
+  lineinfile:
+    dest: "{{ item }}"
+    create: true
+    state: absent
+    line: 'export PATH="{{ anaconda_install_dir }}/bin:$PATH"'
+  with_items:
+    - /etc/bash.bashrc.local
+    - /etc/ksh.kshrc.local
+    - /etc/zsh.zshrc.local
+    - /etc/ash.ashrc.local

+ 3 - 0
vars/main.yml

@@ -0,0 +1,3 @@
+---
+
+anaconda_install_dir:    "{{ anaconda_home_dir }}/dist"