LoginSignup
4

More than 5 years have passed since last update.

ansibleを使ってec2 instanceにnginxをインストールする

Posted at

IAMの作成

ここでユーザを作成してaccess_keyとsecret_access_keyを作成。
環境変数に定義しておく。

~/.bashrc
  export AWS_ACCESS_KEY_ID=XXX
  export AWS_SECRET_ACCESS_KEY=XXX
  export AWS_REGION=ap-northeast-1

Key Pairの作成

ここでKey Pairを作る。
名前は何でもいいが、ansibleというkeyを作りました。

ダウンロードしたansible.pemを~/.ssh/にコピーしてアクセス権をかえておく

$ cd ~/Download
$ chmod 600 ansible.pem
$ mv ansible.pem ~/.ssh

EC2インスタンスの作成

  • Amazon Linuxを選択
  • ansibleというNameをつける(違うNameをつける場合は後で指定するinstance_filtersで同じ名前を付ける)
  • 自分のPCからsshでアクセスできるようにしておく
  • 先ほど作ったkey pair(ansible)を選択

他は何でもOK

python環境の作成

$ virtualenv -p `which python2.7` venv
$ source venv/bin/activate

requirements.txtというファイルを作成して必要なライブラリを記載

requirements.txt
ansible==2.2.1.0
passlib==1.7.1
boto==2.46.1

インストールする

$ pip install -r requirements.txt

EC2 Dynamic Inventoryの準備

$ mkdir -p inventories/dev/
$ cd inventories/dev/
$ wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
$ wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
$ chmod +x ec2.py

ec2.iniの170行目あたりのサンプルを見ながらinstance_filtersを定義

ec2.ini
instance_filters = tag:Name=ansible(他のNameにした場合は修正)

同じ場所にhostsファイルを作っておく

hosts
[all:vars]
ansible_ssh_port=22
ansible_ssh_private_key_file=~/.ssh/ansible.pem
ansible_ssh_user=ec2-user

こんな感じ

- inventories
  - dev
    - ec2.ini
    - ec2.py
    - hosts

動作確認

pingしてみる

% ansible -i inventories/dev tag_Name_ansible -m ping                                                                                                                
54.64.184.xxx | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

nginxのinstall

geerlingguy.nginxを使う

galaxy-requirements.yml
- src: geerlingguy.nginx

インストール

$ ansible-galaxy install -p ./roles/galaxy -r galaxy-requirements.yml

playbookの作成

playbook.yml
---
- name: provisining for a sample java application.
  hosts: all
  become: true

  roles:
    - { role: galaxy/geerlingguy.nginx }

このまま実行するとこんなエラーになるので設定を修正

TASK [galaxy/geerlingguy.nginx : Ensure nginx is installed.] *******************
fatal: [54.64.184.xxx]: FAILED! => {"changed": false, "failed": true, "msg": "Failure talking to yum: failure: repodata/repomd.xml from nginx: [Errno 256] No more mirrors to try.\nhttp://nginx.org/packages/centos/NA/x86_64/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found"}

ついでにnginx.confもテンプレートから読むようにしておく

vars/main.yml
---
nginx_yum_repo_enabled: false
nginx_conf_template: "{{ playbook_dir }}/templates/nginx.conf.j2"
templates/nginx.conf.j2
events {
    worker_connections  1024;
}

http {
  server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    location / {
      root   html;
      index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   html;
    }
  }
}

main.ymlを読むようにplaybook.ymlを修正

playbook.yml
---
- name: provisining for a sample java application.
  hosts: all
  become: true

  vars_files:
    - vars/main.yml

  roles:
    - { role: galaxy/geerlingguy.nginx }

実行

$ ansible-playbook -i inventories/dev playbook.yml

Inbound 80を許可していればnginxのテストページが表示されます。
やった

完成品

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4