Featured image of post 【Ansible備忘録】Apacheをインストールするplaybook

【Ansible備忘録】Apacheをインストールするplaybook

503文字

概要

前提

Point
  • Ansibleインストール済み
  • 対象サーバーへの公開鍵設定済み
  • Apacheのポートは80

参考

関連記事 【脱魔境インフラ】AWSのEC2にAnsibleをインストールしてみよう 2018/02/26 2612文字 約6分で読めます 関連記事 【失われしベストプラクティスを求めて】Ansibleでの特殊な動きをするディレクトリについて 2018/03/13 1640文字 約4分で読めます

ディレクトリ構造

ディレクトリ構造
.
├── httpd-install.yml
├── handlers
│   └── httpd
│       └── start.yml
├── inventories
│   └── local
│       ├── group_vars
│       │   └── all.yml
│       ├── host_vars
│       │   └── 127.0.0.1.yml
│       └── hosts
└── roles
    └── httpd
        └── install
            └── tasks
                └── main.yml

ファイル群

hosts

inventories/local/hosts
[all-servers:children]
local-servers

[local-servers:children]
local

[local]
127.0.0.1

group_vars

inventories/local/group_vars/all.yml
---
#------------------------------------------
# 環境
#------------------------------------------
ENV: "local"

host_vars

inventories/local/host_vars/127.0.0.1.yml
---
#------------------------------------------
# Apache2.4
#------------------------------------------
HTTPD:
  DOCUMENT_ROOT_DIRECTORY: /var/www/html
  CONF:
    DIRECTORY: /etc/httpd/conf
  SERVICE:
    NAME: httpd
  YUM:
    NAME: httpd24

handers

handlers/http/start.yml
- name: "httpd start"
  action: service name={{ HTTPD.SERVICE.NAME }} state=started

roles

roles/httpd/install/tasks/main.yml
- name: instal
  yum:
    name: "{{ HTTPD.YUM.NAME }}"
    state: latest
  notify: "{{ HTTPD.SERVICE.NAME }} start"

playbook

httpd-install.yml
---
- hosts: local-servers
  remote_user: {実行ユーザー名}
  become: yes
  roles:
    - httpd/install
  handlers:
    - import_tasks: handlers/httpd/start.yml

実行

playbook実行コマンド
ansible-playbook  \
  --private-key={秘密鍵へのパス}  \
  -u {実行ユーザー}  \
  -i inventories/local httpd-install.yml  \
  -K


※実行ユーザーのパスワードの入力が求められます

実行ログ
SUDO password:

PLAY [local-servers] ***************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************
ok: [127.0.0.1]

TASK [httpd/install : install httpd] ***********************************************************************************************************************
changed: [127.0.0.1]

RUNNING HANDLER [httpd start] ******************************************************************************************************************************
changed: [127.0.0.1]

PLAY RECAP *************************************************************************************************************************************************
127.0.0.1                  : ok=3    changed=2    unreachable=0    failed=0

確認

ブラウザで、http://localhost/にアクセスして、以下のような画面が表示されればインストール完了です♪