Ansibleでroleを試してみる
VagrantとAnsibleでCentOSの環境を用意してみる
の続き
Ansibleでroleを試してみる。
今回は、
Apacheをインストールして、
アクセスするとGoogleにリダイレクトされる設定をする
playbookで試してみる。
roleを使わない場合
まずはroleを使わないplaybookを作成してみる。
[構成] ├ playbook.yml └ redirect.conf
playbook.yml
--- - hosts: 127.0.0.1 connection: local sudo: yes vars: redirect_url: http://www.google.co.jp tasks: - name: Apacheをインストールする yum: name=httpd - name: Apacheを起動する service: name=httpd state=started enabled=yes - name: 強制的にリダイレクトさせるconfを設置する template: src=redirect.conf dest=/etc/httpd/conf.d/ notify: - apache restart handlers: - name: apache restart action: service name=httpd state=restarted
redirect.conf
<Directory /> Redirect / {{ redirect_url }} </Directory>
プレイブックを実行し、ブラウザからアクセスすると、Googleに飛ばされる。
roleを使う場合
今度はroleを使ってみる。
[構成] ├ roles │└ apache │ ├ vars │ │└ main.yml │ ├ tasks │ │└ main.yml │ ├ handlers │ │└ main.yml │ └ templetes │ └ redirect.conf └ playbook.yml
playbook.yml
--- - hosts: 127.0.0.1 connection: local sudo: yes roles: - apache
roles/apache/vars/main.yml
--- redirect_url: http://www.google.co.jp
roles/apache/tasks/main.yml
--- - name: Apacheをインストールする yum: name=httpd - name: Apacheを起動する service: name=httpd state=started enabled=yes - name: 強制的にリダイレクトさせるconfを設置する template: src=redirect.conf dest=/etc/httpd/conf.d/ notify: - apache restart
roles/apache/handlers/main.yml
--- - name: apache restart action: service name=httpd state=restarted