LoginSignup
9
9

More than 5 years have passed since last update.

`pyenv`をインストール設定するAnsible playbook

Last updated at Posted at 2016-06-14

はじめに

CentOS6のpythonのバージョン2.6.6?では、pipでインストールしたモジュールが
いくつか動作しないなどありましたのでpyenvをインストールしてpythonのバージョンを上げる
CentOS6用のAnsibleのplaybookを作成しました。
rbenvはこちらhttp://qiita.com/tbuchi888/items/fd2e8d7e1790d398b281

構築の主な手順

CentOS6に対して以下をrootで実施します

1. pyenvのインストール
2. pythonバージョン2.7.8のインストール

playbook や Vagrantfile(Ansible provisoner)等

gitsはこちらとなります。
git clone https://gist.github.com/tbuchi888/03f2dccdd7726770cb0522260141ca41

site.yml
---
 - include: install_pyenv_without_proxy.yml

以下playbookのvars: python_ver: 2.7.8でpythonのバージョン指定を行っていますので適宜変更して利用してください。

install_pyenv_without_proxy.yml
---
- hosts: all
  gather_facts: no
  become: yes
  vars:
    python_ver: 2.7.8
  tasks:
    - name: which pyenv
      shell:  source ~/.bash_profile; which pyenv
      register: pyenv_install
      failed_when: pyenv_install.rc == 0
    - debug: var=pyenv_install

    - block:
        - debug: msg="---------- block start ----------"
        - name: yum install with_items
          yum:
            name:  '{{ item.name }}'
            state: latest
          register: RESULT
          with_items:
            - name: gcc
            - name: gcc-c++
            - name: make
            - name: git
            - name: openssl-devel
            - name: bzip2-devel
            - name: zlib-devel
            - name: readline-devel
            - name: sqlite-devel
            - name: bzip2
            - name: sqlite
            - name: patch
        - name: results
          debug: var=RESULT.results

        - name: git clone pyenv , pyenv-virtualenv
          git:
            repo: '{{ item.repo }}'
            dest: '{{ item.dest }}'
          register: RESULT
          with_items:
            - repo: git://github.com/yyuu/pyenv.git
              dest: ~/.pyenv
            - repo: git://github.com/yyuu/pyenv-virtualenv.git
              dest: ~/.pyenv/plugins/pyenv-virtualenv
        - name: results
          debug: var=RESULT.results

        - name: "install pyenv and python ver.{{ python_ver }}"
          shell: '{{ item.cmd }}'
          register: RESULT
          with_items:
            - cmd: echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ~/.bash_profile
            - cmd: echo 'if [ -d "${PYENV_ROOT}" ]; then' >> ~/.bash_profile
            - cmd: echo '    export PATH=${PYENV_ROOT}/bin:$PATH' >> ~/.bash_profile
            - cmd: echo '    eval "$(pyenv init -)"' >> ~/.bash_profile
            - cmd: echo 'fi' >> ~/.bash_profile
            - cmd: source ~/.bash_profile; pyenv -v
            - cmd: source ~/.bash_profile; python -V
            - cmd: source ~/.bash_profile; pyenv install '{{ python_ver }}'
            - cmd: source ~/.bash_profile; pyenv global '{{ python_ver }}'
            - cmd: source ~/.bash_profile; python -V
        - name: results
          debug: var=RESULT.results

        - debug: msg="---------- block end   ----------"

      when: pyenv_install.rc == 1

以下はVagrant環境の場合
以下はvagrant upにてCentOS6のServer起動とAnsible provisonerを利用してpyenv環境の構築を行います。

なお、boxファイル名については、自環境のCentOS6のboxファイル名に置き換えて利用してください。特に32bitマシン環境の場合は、自作の32bit版CentOS6のboxファイル名への置き換えをお願いします。

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  # Please replace it with a BOX name of CentOS6 for one's own 32bit when your host is 32bit machine.
  config.vm.box = "geerlingguy/centos6"

  # Add ansible provisioner
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "site.yml"
  end
end

 使い方

- OSX等でAnsible provisonerが利用できるVagrant環境の場合
  - 上記playbook等資材を用意した上でvagrant upしてください。
- Ansible環境のみの場合
  - インベントリホストファイルを各自用意してCentOS6サーバに対してPlaybookを実行してください。ansible-playbook -i インベントリファイル site.yml

上記プレイブック等の実行環境

Ansible provisioner and Vagrant,Virtulbox Host

 - OS:OSX yosemite
 - Virtulbox:バージョン 5.0.10 r104061
 - Vagrant: Vagrant 1.8.1
 - Ansible:

$ansible --version
ansible 2.1.0 (devel c600ab81ee) last updated 2016/04/20 11:11:25 (GMT +900)
  lib/ansible/modules/core: (detached HEAD 98322a777f) last updated 2016/04/20 11:11:54 (GMT +900)
  lib/ansible/modules/extras: (detached HEAD 1aecfc1e19) last updated 2016/04/20 11:11:55 (GMT +900)
  config file = 
  configured module search path = Default w/o overrides

pyenv install Server

- OS: CentOS6.8

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9