こんにちは。最近Ansibleを書いてhttpdやphpをソースからコンパイルしてインストールしています。yumやdnfといったリポジトリに依存しないインストール方法なので、
- リポジトリの更新に関わらず同じ環境が再現できる
- ライブラリを依存関係を最低限にしつつインストールができる
などメリットもあるかと思います。そんな中、今回利用するライブラリのほとんどで ./configureや makeコマンドを実行してインストールしますが、これをAnsibleでやった時にとんでもない時間ハマったのでメモがてら残しておきます。
改善前のコード(抜粋)
- 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を実行しています。
改善後のコード(抜粋)
- 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からコードを書くと気づきが多くて楽しいですね。
あと、ドキュメントちゃんと読もう。