(ansible)roleの基本構成

投稿者: | 2016年10月24日

みなさんこんにちは。ヒロウミです。

ansibleでroleを使ってみたので、roleのディレクトリ構成をメモっておきます。

site.yml      (roleを実行するplaybook)
roles
|--apache     
  |--hanndlers(イベントに対応したtaskを記述)
  |   - main.yml
  |--tasks    (メインの実行ファイル)
  |   - main.yml
  |--templates(変数埋め込み可能なテンプレートテキスト)
       - example.com.conf.j2

 

- hosts: all
  vars:
    server_name: hello.apach.example
  roles:
    - apache
- name: install apache
  yum: name=httpd state=present
- name: start httpd service
  service: name=httpd state=started enabled=yes
- name: setup conf file
  template: src=example.com.conf.j2 dest=/etc/httpd/conf.d/example.com.conf
  notify: restart apache service
<VirtualHost *:80>
  DocumentRoot /www/public/html
  ServerName {{ server_name }}
</VirtualHost>
- name: restart apache service
  service: name=httpd state=restarted

 

あとは[ansible-playbook site.yml]で実行します。なお、ansibleでは変数は全てグローバル扱いになるらしいので、その辺を注意してプレフィックスを付けたりする必要がありそうです。

色んなロールを追加して自分用のマシンの初期セットアップのroleを作っていきたいですね。

 

参考サイト:

Role を使ったAnsibleのチュートリアル