Ansibleで Raspberry Pi に Python 3.7 をインストールしてみる

急に思い立って、Ansibleを使ってみることにした。

こんなかんじになった。

### raspberry pi に Python3.7.2をインストール
---
- hosts: all
  vars:
    apt_for_python_build_names: [build-essential,zlib1g-dev,zlib1g-dev,libncurses5-dev,libgdbm-dev,libnss3-dev,libssl-dev,libreadline-dev,libffi-dev,wget]
    python_build_version: "3.7.2"
  tasks:
  - name: apt Update and Upgrade
    become: true
    apt:
      update_cache: "yes"
      force_apt_get: "yes"
      autoclean: "yes"
      autoremove: "yes"
      upgrade: "dist"
  - name: Python3.7のダウンロードと展開
    unarchive:
      src: "https://www.python.org/ftp/python/{{python_build_version}}/Python-{{python_build_version}}.tar.xz"
      dest: .
      remote_src: yes
  - name: apt for Python build.
    become: true
    apt:
      autoclean: "yes"
      autoremove: "yes"
      name: "{{apt_for_python_build_names}}"
  - name: Running ./configure for Python build.
    # command: ./configure --enable-optimizations
    command: ./configure #上はビルドにすごく時間がかかる testも全て実行されてしまう
    args:
      chdir: "./Python-{{python_build_version}}/"
  - name: make Python
    make:   # makeモジュールはmakeの引数が渡せない。make -j -l 4
      chdir: "./Python-{{python_build_version}}"
  - name: install Python
    become: true
    make:
      chdir: "./Python-{{python_build_version}}/"
      target: altinstall  #既存を上書きしないインストール
  - name: pip3をpip3.7に
    become: true
    command: python3.7 -m pip install --upgrade pip 
  # ビルドのためのaptインストールを削除
  - name: apt Remove for Python build.
    become: true
    apt:
      state: "absent"
      name: "{{apt_for_python_build_names}}"
      force_apt_get: "yes"
      purge: "yes"
      autoclean: "yes"
      autoremove: "yes"

Ansibleをほぼ初めて使ったら、なんかひどく苦労してしまった。インベントリって何だ?から始まって、YAMLもよく知らなかったのでインデントとハイフンの位置ですったもんだ。あと、unarchiveモジュールがremote_src: yesでネット上のsrcを指定できるの知らなかったのであれこれ遠回りした、などなど。ふぅ。疲れた。