如何使用Ansible安装python3.4.3

时间:2015-09-13 05:06:32

标签: python-3.x ansible python-3.4 pyenv

我想通过使用带有ansible的pyenv来安装python3.x。

- 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

如何用ansible安装python3.x?

2 个答案:

答案 0 :(得分:3)

Ansible不是使用shell模块在​​远程主机上设置环境变量,而是使用environment关键字,可以设置每个任务甚至每个剧本。

假设 www用户已经存在,我设法通过一些更具体的路径设置来实现:

- 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

您需要运行剧本:

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

并根据请求提供www用户的密码。

如果这不起作用,您可能需要在此发布整本剧本供我们查看:)

答案 1 :(得分:3)

因此,对于我来说,使用ansible安装任何版本的python并使其成为替代安装对我有用。我首先运行了configuremake,后来压缩了结果,因为这需要一段时间,然后使用镜像重新分发文件,这样我就可以自己运行make altinstall。这是食谱:

---
# 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

如果要编译服务器上的所有内容,可以在altinstall步骤之前执行以下操作,并下载源代码包而不是预编译的tar。我不建议这样做,因为它确实占用资源而你不想在prod中这样做。以Python2.7.14为例:

---
# 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