Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Install python3.4.3 with Ansible

I want to install python3.x by use pyenv with ansible.

- name: install pyenv
  git: >
    repo=https://github.com/pyenv/pyenv.git
    dest=/home/www/.pyenv
    accept_hostkey=yes
    become: yes
    become_user: www

- name: enable pyenv
  shell: |
    echo 'export PYENV_ROOT="/home/www/.pyenv"' >> /home/www/.bashrc
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> /home/www/.bashrc
    echo 'eval "$(pyenv init -)"' >> /home/www/.bashrc
- name: install python
  shell: pyenv install 3.4.3

How to install python3.x with ansible?

like image 396
Hiro3 Avatar asked Dec 19 '22 00:12

Hiro3


2 Answers

So here is what worked for me well to get any version of python installed with ansible and make it an alternative installation. I first ran configure and make, later compressed the result since this takes a while, then re-distributed the file using a mirror so I can run make altinstall on its own. Here is the recipe:

---
# Check the alt python3 version
- name: check alt python version
  shell: /usr/local/bin/python3.6 --version
  register: python3_version
  ignore_errors: yes  # If not installed
  tags:
    - python-alt

# Stuff I did manually to compile everything first by hand
# Python3 alt-install - steps to create binary:
# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
# tar xf Python-3.6.4.tgz
# mv Python-3.6.4 Python-3.6.4-binary && cd Python-3.6.4-binary
# ./configure --prefix=/usr/local --enable-optimizations
# cd .. && tar -zcvf Python-3.6.4-binary.tar.gz Python-3.6.4-binary (upload to mirror servers)
# make && sudo make altinstall UNINST=1
- name: download and unpack alternative python3
  unarchive:
    src: http://www.yourmirror.com/centos/python/Python-3.6.4-binary.tar.gz dest=/tmp/Python-3.6.4-binary.tar.gz
    dest: /tmp
    remote_src: yes
    keep_newer: yes
  when: python3_version['stderr'] != 'Python 3.6.4'
  tags:
    - python-alt

# Its possible to install (instead of altinstall) python3 here
- name: make install alt python3
  make:
    chdir: /tmp/Python-3.6.4-binary
    target: altinstall
    params:
      UNINST: 1  # Replace
  when: python3_version['stderr'] != 'Python 3.6.4'
  become: yes
  tags:
    - python-alt

- name: download get-pip.py
  get_url:
    url: https://bootstrap.pypa.io/get-pip.py
    dest: /tmp/get-pip.py
    mode: 0664
  tags:
    - python-alt

- name: install pip for python3
  shell: /usr/local/bin/python3.6 /tmp/get-pip.py
  become: yes
  tags:
    - python-alt

# We need virtualenv installed under py3 for the virtualenv command to work
- pip:
    name: virtualenv
    executable: /usr/local/bin/pip3.6
  become: True
  tags:
    - python-alt

If you want to compile everything on your server you could do the following before the altinstall step and also download the source code package instead of the pre-compiled tar. I don't recommend doing it this way because it does take up resources and you don't want to be doing it in prod. Using Python2.7.14 as an example:

---
# Build the default target
- debug:
    var: python2_version
  tags:
    - python_alt
- make:
    chdir: /tmp/Python-2.7.14-binary
  when: python2_version['stderr'] != 'Python 2.7.14'
  tags:
    - python_alt
- name: configure target command
  command: ./configure --prefix=/usr/local --enable-optimizations chdir=/tmp/Python-2.7.14-binary
  when: python2_version['stderr'] != alt_python_version
  tags:
    - python_alt
like image 135
radtek Avatar answered Dec 21 '22 23:12

radtek


Rather than using the shell module to set environment variables on the remote host, Ansible has the environment keyword, which can set per task or even per playbook.

Assuming the www user already exists I managed to get this working with some more specific path setting:

- name: enable pyenv and install python
  shell: /home/www/.pyenv/bin/pyenv init - && /home/www/.pyenv/bin/pyenv install 3.4.3 chdir=/home/www
  environment:
    pyenv_root: /home/www/.pyenv
    path: "{{ pyenv_root }}/bin:$PATH"
  become: yes
  become_user: www

You will need to run the playbook with:

ansible-playbook --ask-become-pass <playbook-name>

and supply the password for the www user on request.

If that doesn't work, you might have to post the whole playbook here for us to look at :)

like image 42
ocean Avatar answered Dec 22 '22 01:12

ocean