如何使用 Ansible 安装 python3.4.3

How to Install python3.4.3 with Ansible

我想通过使用 pyenv 和 ansible 安装 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?

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 用户的密码。

如果这不起作用,您可能需要 post 整个剧本供我们查看:)

所以这对我来说很有效,可以让任何版本的 python 安装 ansible 并使其成为替代安装。我首先 运行 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。我不建议这样做,因为它确实会占用资源,而且您不想在产品中这样做。以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