概要
AnsibleでNginxをソースからインストールする場合、冪等性はどうなるんだろう?と思って作ってみました。
実際作ってみたら結構色んなモジュールを使って勉強になったのでそのメモとして。
今回は「NginxでRTMPストリーミングをする」をAnsibleでできるようにしています。
環境
- Ubuntu 14.04
- Ansible 1.8.2
- Nginx 1.7.10
ソースコード
jun06t/ansible-examplesにありますが簡単に説明を。
フォルダ構成
├── inventory │ └── local ├── nginx.yml └── roles ├── common │ └── tasks │ └── main.yml └── nginx ├── defaults │ └── main.yml ├── files │ └── nginx.init ├── handlers │ └── main.yml ├── tasks │ ├── main.yml │ └── modules.yml └── templates ├── nginx.conf.j2 └── rtmp.conf.j2
基本的にAnsible Best Practicesに則っています。
フォルダ構成の説明
フォルダ名 | 用途 |
---|---|
tasks | タスクを実行する |
defaults | 変数を設定する。優先度は最も低いのでvars などがあるとそちらが優先 |
handlers | イベントを受けて実行する処理。configに変更があったら再起動〜など。notify で呼び出す |
files | ファイルを転送したいときに使用。SSHキーや値が不変なファイル |
templates | configなど、変数を反映させて転送した時に使用 |
tasks
の中身
順次説明していきますがまずは軽く目を通してみてください。
--- - name: Install dependencies apt: name={{ item }} state=latest update_cache=yes with_items: - build-essential - libpcre3 - libpcre3-dev - libssl-dev - name: Download nginx src get_url: url: "{{ nginx_source_url }}" dest: /usr/local/src/nginx-{{ nginx_version }}.tar.gz - include: "modules.yml" - name: Unpack source command: tar xvzf /usr/local/src/nginx-{{ nginx_version }}.tar.gz -C /usr/local/src creates=/usr/local/src/nginx-{{ nginx_version }} - name: Compile the Nginx source shell: > cd /usr/local/src/nginx-{{ nginx_version }} && ./configure {{ nginx_configure_flags | join(" ") }} && make && make install - name: Send init script copy: src: nginx.init dest: /etc/init.d/nginx mode: 0755 notify: restart nginx - name: Send conf script template: src: nginx.conf.j2 dest: "{{ nginx_conf_path }}/nginx.conf" mode: 0755 notify: restart nginx - name: Register Nginx as a service service: name=nginx state=started enabled=yes
各モジュールの説明
apt
apt-get install
するときに使います。
apt: name={{ item }} state=latest update_cache=yes with_items: - build-essential - libpcre3 - libpcre3-dev - libssl-dev
{{ item }}
変数を使用することで複数を同時にインストールできます。
パラメータ | 選択肢 | 説明 |
---|---|---|
name | インストールしたいパッケージ名 | |
state | latest present absent build-dep |
インストールパッケージのstate。デフォルトpresent |
update_cache | 実行前にapt-get update をやってくれる |
get_url
ファイルをダウンロードするときに使います。wget
に近いです。
- name: Download nginx src get_url: url: "{{ nginx_source_url }}" dest: /usr/local/src/nginx-{{ nginx_version }}.tar.gz
パラメータ | 選択肢 | 説明 |
---|---|---|
url | ダウンロードしたいファイルのURL | |
dest | 保存先。ファイル名まできちんと指定すれば ファイルの有無でダウンロードするかどうか自動で判断してくれる |
|
force | yes no |
必ず毎回ダウンロードする。デフォルトno |
include
別のyamlファイルを読み込みます。ファイル分割したいときに使います。
- include: "modules.yml"
command, shell
シェルコマンドを実行するときに使います。<, >, |
を使用する場合は、shell
の方を使います。
- name: Unpack source command: tar xvzf /usr/local/src/nginx-{{ nginx_version }}.tar.gz -C /usr/local/src creates=/usr/local/src/nginx-{{ nginx_version }} - name: Compile the Nginx source shell: > cd /usr/local/src/nginx-{{ nginx_version }} && ./configure {{ nginx_configure_flags | join(" ") }} && make && make install
パラメータ | 選択肢 | 説明 |
---|---|---|
creates | 指定のファイル名が存在するなら、commandの実行をスキップします | |
removes | 指定のファイル名が存在しないなら、commandの実行をスキップします |
copy
ファイルを転送するときに使います。
- name: Send init script copy: src: nginx.init dest: /etc/init.d/nginx mode: 0755 notify: restart nginx
src
のファイルはfiles
ディレクトリに置きます。dest
はリモートのパスです。
dest
にはちゃんとファイル名(xxx.confとか)まで書いてください。
パラメータ | 選択肢 | 説明 |
---|---|---|
src | コピー元(ローカル) | |
dest | コピー先(リモート) | |
owner | 所有者の指定 | |
group | 所有グループの指定 | |
mode | 権限の設定 |
template
ファイルに変数を設定して転送するときに使います。
- name: Send conf script template: src: nginx.conf.j2 dest: "{{ nginx_conf_path }}/nginx.conf" mode: 0755
パラメータ | 選択肢 | 説明 |
---|---|---|
src | 転送元(ローカル) | |
dest | 転送先(リモート) | |
owner | 所有者の指定 | |
group | 所有グループの指定 | |
mode | 権限の設定 |
notify
変更があったとき(statusがchangedのとき)にイベントを発火させます。
notify: restart nginx
handlers
ディレクトリにrestart nginx
という名前のタスクを登録しておけばそれを実行してくれます。
service
サービスの設定をするときに使います。
- name: Register Nginx as a service service: name=nginx state=started enabled=yes
パラメータ | 選択肢 | 説明 |
---|---|---|
name | サービス名 | |
state | started stopped restarted reloaded |
サービスの動作 |
enabled | yes no |
システム起動時に自動起動するかどうか。デフォルトno |
インストール
$ ansible-playbook -i inventory/local nginx.yml
を実行するとインストールしてくれます。