LoginSignup
0
0

More than 1 year has passed since last update.

Ansible: GitHubリリースからPythonパッケージをインストールする

Last updated at Posted at 2022-05-05

概要

Ansibleを使って、GitHubリリースにアップロードされているWheelファイルをダウンロードしインストールします。

事前準備と前提

community.generalコレクションを使用するので、インストールされていなければ下記のコマンドでインストールして下さい。

ansible-galaxy collection install community.general

また、AnsibleでインストールするPythonパッケージは、下記Jupyter Notebookの例のようにGitHubのリリースに.whlファイルとしてアップロードされているとします。

Jupyter Notebookの例

最新リリースの取得

community.generalコレクションgithub_releaseというモジュールはあるのですが、v4.8.0時点ではアセットの取得ができません。
代わりにansible.builtin.get_urlモジュールを使って直接Get the latest release APIにアクセスします。

- name: Find the latest version
  ansible.builtin.uri:
    url: "https://api.github.com/repos/{{ org }}/{{ repo }}/releases/latest"
    headers:
      Authorization: "Bearer {{ github_token }}"
      Accept: application/vnd.github.v3+json
  register: info
  no_log: true

変数orgにレポジトリのOrganization名(個人アカウントの場合はユーザ名)、repoにリポジトリ名が格納されているとします。

リポジトリがプライベートの場合はAuthorizationヘッダを使った認証が必要です。
変数github_tokenにはrepo権限のあるアクセストークンを格納されているとします。
パブリックレポジトリの場合、Authorizationヘッダは不要です。

APIのレスポンスはJSON形式で返ってくるので、戻り値を格納した変数infojson属性からアクセスできます。

Wheelファイルのダウンロードとインストール

GitHubリリースにアップロードされたファイルは、Get the latest release APIレスポンスのassets属性にリストとして記録されています。

その中から、.whlファイルだけを取得するためにJSONクエリフィルターを使います。
クエリassets[?ends_with(name, `.whl`)].{name: name, url: url}は、assetsの中からname.whlで終わるものだけを抜き出し、nameurlからなるオブジェクトを作成します。
このnameurlは、get_urlモジュールのdesturlで使用しています。

- name: Download the latest package
  ansible.builtin.get_url:
    url: "{{ item.url }}"
    headers:
      Authorization: "Bearer {{ github_token }}"
      Accept: application/octet-stream
    dest: "/tmp/{{ item.name }}"
    mode: "0644"
  loop: "{{ info.json | community.general.json_query(query) }}"
  vars:
    query: "assets[?ends_with(name, `.whl`)].{name: name, url: url}"
  register: download
  no_log: true

今回もパブリックリポジトリの場合、Authorizationヘッダは不要です。

Accept: application/octet-streamの指定を忘れると、.whlファイルではなくメタデータがJSON形式で返ってきてしまうので注意が必要です。

最後に、ダウンロードしてきた.whlファイルをpipを使ってインストールします。

- name: Install the package
  ansible.builtin.pip:
    name: "file://{{ item.dest }}"
    state: present
  loop: "{{ download.results }}"

assetsがリストのため、このタスクもループ処理になります。

プレイブック

以上をまとめたプレイブックは次の通りです。vars以下の変数を適切に設定してください。
github_tokenはvaultで暗号化しておくと良いでしょう。

gh_package.yaml
- hosts: all

  become: true
  gather_facts: false
  vars:
    org: org_name
    repo: repo_name
    github_token: personal_access_token
  tasks:
    - name: Find the latest version
      ansible.builtin.uri:
        url: "https://api.github.com/repos/{{ org }}/{{ repo }}/releases/latest"
        headers:
          Authorization: "Bearer {{ github_token }}"
          Accept: application/vnd.github.v3+json
      register: info
      no_log: true

    - name: Download the latest package
      ansible.builtin.get_url:
        url: "{{ item.url }}"
        headers:
          Authorization: "Bearer {{ github_token }}"
          Accept: application/octet-stream
        dest: "/tmp/{{ item.name }}"
        mode: "0644"
      loop: "{{ info.json | community.general.json_query(query) }}"
      vars:
        query: "assets[?ends_with(name, `.whl`)].{name: name, url: url}"
      register: download
      no_log: true

    - name: Install the package
      ansible.builtin.pip:
        name: "file://{{ item.dest }}"
        state: present
      loop: "{{ download.results }}"

参考

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