Ansible で HEIF 対応の ImageMagick をソースからインストール

#はじめに
Ansible で HEIF 対応の ImageMagick をソースからインストールするサンプルです。
・HEIC に対応させるために別途 libde265 と libheif のインストールが必要
・HEIC の他、GIF、JPEG、PNG、WebP に対応
・Amazon Linux 2 にて検証

#構成
```
playbook
|-- group_vars
    |-- all.yml
|-- roles
    |-- software_management
        |-- imagemagick
            |-- defaults
                |-- main.yml
            |-- files
                |-- config
                     |-- policy.xml
            |-- tasks
                |-- main.yml
                |-- setup.yml
        |-- libde265
            |-- defaults
                |-- main.yml
            |-- tasks
                |-- main.yml
                |-- setup.yml
        |-- libheif
            |-- defaults
                |-- main.yml
            |-- tasks
                |-- main.yml
                |-- setup.yml
```

#共通変数
```YAML:group_vars/all.yml
---
# 一時ディレクトリ
temp_dir: /var/tmp
```

#libde265 のインストール
```YAML:roles/software_management/libde265/defaults/main.yml
---
# Git リポジトリ
git_repository: https://github.com/strukturag/libde265.git

# 依存モジュール
dependencies:
  - gcc-c++
  - git
  - libtool
```

```YAML:roles/software_management/libde265/tasks/main.yml
---
- import_tasks: setup.yml
```

```YAML:roles/software_management/libde265/tasks/setup.yml
---
- ansible.builtin.set_fact:
    git_clone_dir: "{{ temp_dir }}/libde265"

- name: Install dependences.
  ansible.builtin.yum:
    name: "{{ dependencies }}"
    state: present

- name: Clone remote Git repository.
  ansible.builtin.git:
    repo: "{{ git_repository }}"
    dest: "{{ git_clone_dir }}"

- name: Generate configure script.
  ansible.builtin.command: ./autogen.sh
  args:
    chdir: "{{ git_clone_dir }}"

- name: Configure libde265.
  ansible.builtin.command: ./configure
  args:
    chdir: "{{ git_clone_dir }}"

- name: Make libde265.
  community.general.make:
    chdir: "{{ git_clone_dir }}"

- name: Install libde265.
  community.general.make:
    chdir: "{{ git_clone_dir }}"
    target: install

- name: Create configuration file for local libraries to search with ldconfig.
  ansible.builtin.lineinfile:
    path: /etc/ld.so.conf.d/usr-local-lib.conf
    line: /usr/local/lib
    create: yes

- name: Create links and cache to shared libraries.
  ansible.builtin.command: ldconfig

- name: Delete Git clone directory.
  ansible.builtin.file:
    path: "{{ git_clone_dir }}"
    state: absent
```

#libheif のインストール
```YAML:roles/software_management/libheif/defaults/main.yml
---
# Git リポジトリ
git_repository: https://github.com/strukturag/libheif.git

# 依存モジュール
dependencies:
  - gcc-c++
  - git
  - libtool
```

```YAML:roles/software_management/libheif/tasks/main.yml
---
- include_role:
    name: software_management/libde265

- import_tasks: setup.yml
```

```YAML:roles/software_management/libheif/tasks/setup.yml
---
- ansible.builtin.set_fact:
    git_clone_dir: "{{ temp_dir }}/libheif"

- name: Install dependences.
  ansible.builtin.yum:
    name: "{{ dependencies }}"
    state: present

- name: Clone remote Git repository.
  ansible.builtin.git:
    repo: "{{ git_repository }}"
    dest: "{{ git_clone_dir }}"

- name: Generate configure script.
  ansible.builtin.command: ./autogen.sh
  args:
    chdir: "{{ git_clone_dir }}"

- name: Configure libheif.
  ansible.builtin.command: ./configure
  args:
    chdir: "{{ git_clone_dir }}"
  environment:
    PKG_CONFIG_PATH: /usr/local/lib/pkgconfig

- name: Make libheif.
  community.general.make:
    chdir: "{{ git_clone_dir }}"

- name: Install libheif.
  community.general.make:
    chdir: "{{ git_clone_dir }}"
    target: install

- name: Create configuration file for local libraries to search with ldconfig.
  ansible.builtin.lineinfile:
    path: /etc/ld.so.conf.d/usr-local-lib.conf
    line: /usr/local/lib
    create: yes

- name: Create links and cache to shared libraries.
  ansible.builtin.command: ldconfig

- name: Delete Git clone directory.
  ansible.builtin.file:
    path: "{{ git_clone_dir }}"
    state: absent
```

#ImageMagick のインストール
※ 変数 `imagemagick_version` は `--extra-vars` で `imagemagick_version=7.0.8-25` の様に指定

```YAML:roles/software_management/imagemagick/defaults/main.yml
---
# ImageMagick ディストリビューション
imagemagick_distributions:
  7.0.8-25:
    url: https://www.imagemagick.org/download/releases/ImageMagick-7.0.8-25.tar.gz

# 依存モジュール
dependencies:
  - gcc
  - libjpeg-turbo-devel
  - libpng-devel
  - libwebp-devel

# configure 時のオプション引数
coufigure_option_args:
  # OpenMP
  - --disable-openmp
  # 量子化レベル
  - --with-quantum-depth=8

# configure 時のオプション
coufigure_option: "{{ coufigure_option_args | join(' ') }}"

# ImageMagick 設定ファイルのディレクトリ
imagemagick_config_dir: /usr/local/etc/ImageMagick-7

# ImageMagick 設定ファイル
imagemagick_config_files:
  - src: "{{ role_path }}/files/config/"
    mode: u=rw,go=r
```

```XML:software_management/imagemagick/files/config/policy.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policymap [
  <!ELEMENT policymap (policy)+>
  <!ATTLIST policymap xmlns CDATA #FIXED ''>
  <!ELEMENT policy EMPTY>
  <!ATTLIST policy xmlns CDATA #FIXED '' domain NMTOKEN #REQUIRED
    name NMTOKEN #IMPLIED pattern CDATA #IMPLIED rights NMTOKEN #IMPLIED
    stealth NMTOKEN #IMPLIED value CDATA #IMPLIED>
]>
<!--
  Configure ImageMagick policies.

  Domains include system, delegate, coder, filter, path, or resource.

  Rights include none, read, write, execute and all.  Use | to combine them,
  for example: "read | write" to permit read from, or write to, a path.

  Use a glob expression as a pattern.

  Suppose we do not want users to process MPEG video images:

    <policy domain="delegate" rights="none" pattern="mpeg:decode" />

  Here we do not want users reading images from HTTP:

    <policy domain="coder" rights="none" pattern="HTTP" />

  The /repository file system is restricted to read only.  We use a glob
  expression to match all paths that start with /repository:

    <policy domain="path" rights="read" pattern="/repository/*" />

  Lets prevent users from executing any image filters:

    <policy domain="filter" rights="none" pattern="*" />

  Any large image is cached to disk rather than memory:

    <policy domain="resource" name="area" value="1GP" />

  Define arguments for the memory, map, area, width, height and disk resources
  with SI prefixes (.e.g 100MB).  In addition, resource policies are maximums
  for each instance of ImageMagick (e.g. policy memory limit 1GB, -limit 2GB
  exceeds policy maximum so memory limit is 1GB).

  Rules are processed in order.  Here we want to restrict ImageMagick to only
  read or write a small subset of proven web-safe image types:

    <policy domain="delegate" rights="none" pattern="*" />
    <policy domain="filter" rights="none" pattern="*" />
    <policy domain="coder" rights="none" pattern="*" />
    <policy domain="coder" rights="read|write" pattern="{GIF,JPEG,PNG,WEBP}" />
-->
<policymap>
  <!-- <policy domain="system" name="shred" value="2" /> -->
  <!-- <policy domain="system" name="precision" value="6" /> -->
  <!-- <policy domain="system" name="memory-map" value="anonymous" /> -->
  <!-- <policy domain="system" name="max-memory-request" value="256MiB" /> -->
  <!-- <policy domain="resource" name="temporary-path" value="/tmp" /> -->
  <!-- <policy domain="resource" name="memory" value="2GiB" /> -->
  <!-- <policy domain="resource" name="map" value="4GiB" /> -->
  <!-- <policy domain="resource" name="width" value="10KP" /> -->
  <!-- <policy domain="resource" name="height" value="10KP" /> -->
  <!-- <policy domain="resource" name="list-length" value="128" /> -->
  <!-- <policy domain="resource" name="area" value="100MP" /> -->
  <!-- <policy domain="resource" name="disk" value="16EiB" /> -->
  <!-- <policy domain="resource" name="file" value="768" /> -->
  <!-- <policy domain="resource" name="thread" value="4" /> -->
  <!-- <policy domain="resource" name="throttle" value="0" /> -->
  <!-- <policy domain="resource" name="time" value="3600" /> -->
  <!-- <policy domain="coder" rights="none" pattern="MVG" /> -->
  <!-- <policy domain="delegate" rights="none" pattern="HTTPS" /> -->
  <!-- <policy domain="path" rights="none" pattern="@*" /> -->
  <!-- <policy domain="cache" name="memory-map" value="anonymous" /> -->
  <!-- <policy domain="cache" name="synchronize" value="True" /> -->
  <!-- <policy domain="cache" name="shared-secret" value="passphrase" stealth="true" /> -->
  <policy domain="delegate" rights="none" pattern="*" />
  <policy domain="filter" rights="none" pattern="*" />
  <policy domain="coder" rights="none" pattern="*" />
  <policy domain="coder" rights="read|write" pattern="GIF" />
  <policy domain="coder" rights="read|write" pattern="HEIC" />
  <policy domain="coder" rights="read|write" pattern="{JPEG,JPG}" />
  <policy domain="coder" rights="read|write" pattern="PNG" />
  <policy domain="coder" rights="read|write" pattern="WEBP" />
</policymap>

```

```YAML:roles/software_management/imagemagick/tasks/main.yml
---
- include_role:
    name: software_management/libheif

- import_tasks: setup.yml
```

```YAML:roles/software_management/imagemagick/tasks/setup.yml
---
- name: Install dependences.
  ansible.builtin.yum:
    name: "{{ dependencies }}"
    state: present

- name: Download ImageMagick distribution file.
  ansible.builtin.get_url:
    url: "{{ imagemagick_distributions[imagemagick_version].url }}"
    dest: "{{ temp_dir }}"
  register: distribution_file

- name: Unarchive ImageMagick distribution file.
  ansible.builtin.unarchive:
    src: "{{ distribution_file.dest }}"
    dest: "{{ temp_dir }}"
    remote_src: yes
    owner: root
    group: root

- name: Configure ImageMagick.
  ansible.builtin.command: "./configure {{ coufigure_option }}"
  args:
    chdir: "{{ temp_dir }}/ImageMagick-{{ imagemagick_version }}"
  environment:
    PKG_CONFIG_PATH: /usr/local/lib/pkgconfig

- name: Make ImageMagick.
  community.general.make:
    chdir: "{{ temp_dir }}/ImageMagick-{{ imagemagick_version }}"

- name: Install ImageMagick.
  community.general.make:
    chdir: "{{ temp_dir }}/ImageMagick-{{ imagemagick_version }}"
    target: install

- name: Copy ImageMagick configuration files.
  ansible.builtin.copy:
    src: "{{ item.src }}"
    dest: "{{ imagemagick_config_dir }}"
    mode: "{{ item.mode }}"
  with_items:
    - "{{ imagemagick_config_files }}"

- name: Create configuration file for local libraries to search with ldconfig.
  ansible.builtin.lineinfile:
    path: /etc/ld.so.conf.d/usr-local-lib.conf
    line: /usr/local/lib
    create: yes

- name: Create links and cache to shared libraries.
  ansible.builtin.command: ldconfig

- name: Delete ImageMagick distribution file.
  ansible.builtin.file:
    path: "{{ item }}"
    state: absent
  with_items:
    - "{{ distribution_file.dest }}"
    - "{{ temp_dir }}/ImageMagick-{{ imagemagick_version }}"
```

#参照
[kameidの備忘録 - Sharpen the Saw! · heif を jpg に変換してみた](https://d.hatena.ne.jp/kameid/20181002/1538461121)