LoginSignup
1
2

More than 3 years have passed since last update.

Pythonインストール(Ansible)

Last updated at Posted at 2019-10-23

はじめに

Ansibleを使用してPythonインストールを行う際の手順を記述します
サンプルアプリケーションはgithubになります

前提事項

Ansibleセットアップ手順はREADMEを参照ください
アプリケーション環境構築イメージはアプリケーション環境構築(Ansible)を参照ください

Pythonインストール(Ansible)

Pythonインストールを行うplaybookを定義します

ファイル 内容
変数ファイル グループやユーザ名やPythonのインストールバージョン等の変数を定義します
タスクファイル Python関連のパッケージやPythonのインストール等を定義します

Python関連のパッケージインストール

python関連のパッケージをインストールします

- name: Python関連のパッケージインストール
  apt:
    force_apt_get: yes
    state: latest
    name:
      - python-pip
      - libffi-dev
      - libmysqlclient-dev
      - gettext

各項目の説明は以下のとおり

項目 内容
force_apt_get 警告を回避するためにapt-get使用を指定します
詳細は#56832を参照ください
state latestを指定して最新バージョンをインストールします
name pip等の必要なパッケージ名を指定します

Python及びモジュールインストール

Python及び必要な各種モジュールをインストールします

- block:
    - name: pyenvの存在チェック
      command: test -x {{ home_dir }}/.pyenv/bin/pyenv
      register: pyenv_present
      ignore_errors: yes

    - name: pyenvリポジトリ更新
      git:
        repo: https://github.com/pyenv/pyenv.git
        dest: "{{ home_dir }}/.pyenv"
      when: pyenv_present is failed

    - name: pyenv設定追加
      lineinfile:
        dest: "{{ home_dir }}/.bash_profile"
        state: present
        create: yes
        line: "{{ item }}"
      with_items:
        - 'export PYENV_ROOT="$HOME/.pyenv"'
        - 'export PATH="$PYENV_ROOT/bin:$PATH"'
        - 'if command -v pyenv 1>/dev/null 2>&1; then'
        - '  eval "$(pyenv init -)"'
        - 'fi'
      when: pyenv_present is failed

    - name: bash_profile読込み
      shell: /bin/bash -lc "source ~/.bash_profile"

    - name: Python{{ install_python_version }} インストール
      shell: /bin/bash -lc "pyenv install {{ install_python_version }}"
      when: pyenv_present is failed

    - name: shim更新
      shell: /bin/bash -lc "pyenv rehash"
      when: pyenv_present is failed

    - name: Pythonインターセプタ変更
      set_fact: ansible_python_interpreter={{ home_dir }}/.pyenv/shims/python

    - name: Python{{ install_python_version }}のglobal指定
      shell: /bin/bash -lc "pyenv global {{ install_python_version }}"

    - name: Python{{ install_python_version }}のpipアップグレード
      pip:
        name: pip
        state: latest

    - name: Python{{ install_python_version }}のモジュールインストール
      pip:
        name:
          - beautifulsoup4==4.6.0
          - Django==2.1.5
          - django-admin-tools==0.8.1
          - django-bootstrap-form==3.4
          - lxml==4.3.0
          - mysqlclient==1.4.2
          - pytz==2018.9
          - PyYAML==3.12
          - pytest==4.1.1
          - pytest-django==3.4.5

    - name: Pythonインターセプタ変更
      set_fact: ansible_python_interpreter=/usr/bin/python

  become: yes
  become_user: "{{ user_name }}"

blockディレクティブ

指定のユーザにスイッチしてPythonをインストールするためblockディレクティブを使用します

- block:
 ・
 ~ 各タスクを記述する ~
 ・
  become: yes
  become_user: "{{ user_name }}"

各項目の説明は以下のとおり

項目 内容
become yesを指定してsuコマンドを実行します
become_user スイッチするユーザを指定します

pyenvの存在チェック

pyenvの存在チェックを行います

- name: pyenvの存在チェック
    command: test -x {{ home_dir }}/.pyenv/bin/pyenv
    register: pyenv_present
    ignore_errors: yes

各項目の説明は以下のとおり

項目 内容
command testコマンドを実行してファイル有無を取得します
register 後続タスクの実行を判定するためファイル有無結果を変数に保持します
ignore_errors ファイルが存在しない場合も処理を継続します

pyenvリポジトリ更新

gitコマンドでpyenvのリポジトリを取得します

- name: pyenvリポジトリ更新
  git:
    repo: https://github.com/pyenv/pyenv.git
    dest: "{{ home_dir }}/.pyenv"
  when: pyenv_present is failed

各項目の説明は以下のとおり

項目 内容
repo リポジトリのURLパスを指定します
dest リポジトリのダウンロード先を指定します
when 変数を参照してpyenvインストール済の場合はスキップします

bash_profile設定

bash_profileに設定を追加して読み込みます

- name: pyenv設定追加
  lineinfile:
    dest: "{{ home_dir }}/.bash_profile"
    state: present
    create: yes
    line: "{{ item }}"
  with_items:
    - 'export PYENV_ROOT="$HOME/.pyenv"'
    - 'export PATH="$PYENV_ROOT/bin:$PATH"'
    - 'if command -v pyenv 1>/dev/null 2>&1; then'
    - '  eval "$(pyenv init -)"'
    - 'fi'
  when: pyenv_present is failed

- name: bash_profile読込み
  shell: /bin/bash -lc "source ~/.bash_profile"

各項目の説明は以下のとおり

項目 内容
dest bash_profileのパスを指定します
state 設定が追加済の場合はスキップします
create ファイルが存在しない場合はファイルを作成します
line with_itemsの定義内容をファイルに追記します
when 変数を参照してpyenvインストール済の場合はスキップします
shell sourceコマンドを実行します

Pythonインストール

Pythonをインストールします

- name: Python{{ install_python_version }} インストール
  shell: /bin/bash -lc "pyenv install {{ install_python_version }}"
  when: pyenv_present is failed

- name: shim更新
  shell: /bin/bash -lc "pyenv rehash"
  when: pyenv_present is failed

各項目の説明は以下のとおり

項目 内容
shell pyenvにインストールするPythonのバージョンを指定します
when 変数を参照してpyenvインストール済の場合はスキップします

Pythonインターセプタ変更

pyenvにモジュールをインストールするためPythonインターセプタを変更します

- name: Pythonインターセプタ変更
  set_fact: ansible_python_interpreter={{ home_dir }}/.pyenv/shims/python

各項目の説明は以下のとおり

項目 内容
set_fact pyenvのPythonパスを指定します

Ansible実行中はPythonインターセプタ変更は保持されるため
モジュールのインストールは以下の流れで行っています

No 内容
1 Pythonパスをシステム→ユーザの参照先に変更する
2 スイッチしたユーザでpipを実行する
3 Pythonパスをユーザ→システムの参照先に変更する

Pythonのglobal指定

pyenvのPythonバージョンを変更します

- name: Python{{ install_python_version }}のglobal指定
  shell: /bin/bash -lc "pyenv global {{ install_python_version }}"

各項目の説明は以下のとおり

項目 内容
shell pyenvのPythonバージョンを指定します

pipアップグレード

pipを最新化します

- name: Python{{ install_python_version }}のpipアップグレード
  pip:
    name: pip
    state: latest

各項目の説明は以下のとおり

項目 内容
pip モジュール名を指定します
state latestを指定してモジュールを最新化します

モジュールインストール

pipコマンドを使用してモジュールをインストールします

- name: Python{{ install_python_version }}のモジュールインストール
  pip:
    name:
      - beautifulsoup4==4.6.0
      - Django==2.1.5
      - django-admin-tools==0.8.1
      - django-bootstrap-form==3.4
      - lxml==4.3.0
      - mysqlclient==1.4.2
      - pytz==2018.9
      - PyYAML==3.12
      - pytest==4.1.1
      - pytest-django==3.4.5

各項目の説明は以下のとおり

項目 内容
pip アプリケーションに必要なモジュール名を指定します

参考情報

1
2
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
1
2