1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansibleのmakeで詰まった話

1
Last updated at Posted at 2021-01-13

こんにちは。最近Ansibleを書いてhttpdやphpをソースからコンパイルしてインストールしています。yumやdnfといったリポジトリに依存しないインストール方法なので、

  • リポジトリの更新に関わらず同じ環境が再現できる
  • ライブラリを依存関係を最低限にしつつインストールができる

などメリットもあるかと思います。そんな中、今回利用するライブラリのほとんどで ./configuremakeコマンドを実行してインストールしますが、これをAnsibleでやった時にとんでもない時間ハマったのでメモがてら残しておきます。

改善前のコード(抜粋)

httpd.yaml
- name: get httpd
  get_url:
    url:
    dest: /tmp/
    state: present

- name: open httpd repository
  unarchive:
    src: /tmp/httpd-2.4.45.tar.gz
    dest: /opt/httpd-2.4.45
    remote: yes

# ....apr, apr-util, pcreなど必要なパッケージを取得....

- name: configure httpd
  command: './configure --prefix=/usr/local/apache2'
  args:
    src: /opt/httpd-2.4.45

- name: execute make command
  command: 'make'
  args:
    chdir: /opt/httpd-2.4.45

- name: execute make install
  make:
    chdir: /opt/httpd-2.4.45

手動実行とAnsible実行の翻訳のために

commandモジュール

はじめにcommandモジュールです。リモートで実行させたいコマンド、特にAnsibleののモジュールが用意されていないパターンだと使う必要が出てくるかと思います。今回の場合は ./configureのシェルを叩くために使っています。特定のファイルを実行するためにはよく使うのではないでしょうか。

makeモジュール

makeコマンドを実行するためにモジュールが公式で提供されています。
https://docs.ansible.com/ansible/2.8/modules/make_module.html
ここで大事だったのが、makeコマンドで渡すサブコマンドを指定するところです。これを見落としていたこともあり、だいぶハマりました。

手動からYAMLへ

手動で行うと以下になります。(httpdのインストール部分)

# cd /opt/httpd-2.4.45
# ./configure --prefix=/usr/local/apache2
# make
# make install

これをAnsible化させると、

- name: configure httpd
  command: './configure --prefix=/usr/local/apache2'
  args:
    src: /opt/httpd-2.4.45

- name: execute make command # make モジュールに変更
  make:
    chdir: /opt/httpd-2.4.45

- name: execute make install
  make:
    target: install          # targetを追加
    chdir: /opt/httpd-2.4.45

makeモジュールを2回使って、makeのみとmake installを実行しています。

改善後のコード(抜粋)

httpd.yaml
- name: get httpd
  get_url:
    url:
    dest: /tmp/
    state: present

- name: open httpd repository
  unarchive:
    src: /tmp/httpd-2.4.45.tar.gz
    dest: /opt/httpd-2.4.45
    remote: yes

# ....apr, apr-util, pcreなど必要なパッケージを取得....

- name: configure httpd
  command: './configure --prefix=/usr/local/apache2'
  args:
    src: /opt/httpd-2.4.45

- name: execute make command # make モジュールに変更
  make:
    chdir: /opt/httpd-2.4.45

- name: execute make install
  make:
    target: install          # targetを追加
    chdir: /opt/httpd-2.4.45

終わりに

なんでmakeモジュールのtargetが必須じゃないんだろうと思ったら、makeコマンドそのものも実行できる様にしているのか、、、と後になって気づきました。1からコードを書くと気づきが多くて楽しいですね。

あと、ドキュメントちゃんと読もう。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?