今回学ぶこと †
ディレクトリ構成 †
[~/sourcetree/AnsibleExam/nginx]$ tree
.
├── Vagrantfile
├── ansible.cfg
├── files
│ └── nginx
│ └── site-ansible.conf
├── hosts
├── nginx.retry
├── nginx.yml
└── templates
└── index.html.j2
3 directories, 7 files
- 慣例的な設定ファイルの置き場所
- files/{app} に設定ファイルを置く
- templates/ にテンプレートを置く
Playbook †
- name: Configure nginx
hosts: testserver
become: True
tasks:
- name: Add repository
yum_repository:
name: epel
description: EPEL YUM repo
baseurl: http://download.fedoraproject.org/pub/epel/$releasever/$basearch/
- name: Add gpg key of EPEL
rpm_key: key=https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7 state=present
- name: Install nginx
yum: name=nginx state=latest
- name: copy nginx config file
copy: src=files/nginx/site-ansible.conf dest=/etc/nginx/default.d/
- name: Creates /ansible site directory
file: path=/var/www/ansible state=directory owner=nginx group=nginx mode=0775
- name: copy index.html
template: src=templates/index.html.j2 dest=/var/www/ansible/index.html
- name: start nginx
service: name=nginx enabled=Yes state=started
- 設定ファイルのコピー
- name: copy nginx config file
copy: src=files/nginx/site-ansible.conf dest=/etc/nginx/default.d/
- ディレクトリの作成
- name: Creates /ansible site directory
file: path=/var/www/ansible state=directory owner=nginx group=nginx mode=0775
- HTMLをテンプレート化
- name: copy index.html
template: src=templates/index.html.j2 dest=/var/www/ansible/index.html
- とりあえず、"ansible_managed" オブジェクトを表示
<html>
<head>
<title>Welcome to ansible</title>
</head>
<body>
<h1>nginx, configured by ansible</h1>
<p>If you can see this, Ansible successfully installed nginx.</p>
<p>{{ ansible_managed }}</p>
</body>
</html>
- どんなオブジェクトを使えるかは、⇒ Ansible Jinja2 template
- これだけ分かれば、単一サーバを相手にするなら一通りのことが出来そう
実行結果 †
Ansible