LoginSignup
12
12

More than 5 years have passed since last update.

Ansible で EC2 のインスタンスを生成する

Posted at

やりたいこと

Provisioning できる Ansible で、EC2 のインスタンスを生成したかったが、ちょっとはまったのでメモ。
基本的にはページしたの参考そのまま。

とりあえず作成までなので、Console から止める必要があるので注意。

インストール & セットアップ

インストールとかディレクトリ作成とか

brew install ansible
sudo pip install boto
sudo mkdir /etc/ansible/

ファイル作成

  • /etc/ansible に 127.0.0.1 を追加
  • ~/.boto に下記追加
[Credentials]
aws_access_key_id = [your access key id]
aws_secret_access_key = [your secret access key id]

playbook 作成

ec2factory.yml がテンプレ、 values.yml が個別設定

ec2factory.yml

- hosts:
  - 127.0.0.1
  connection: local
  gather_facts: False
  vars_files:
    - values.yml
  tasks:
    - name: Provision a set of instances
      local_action: ec2
          keypair={{keypair}}
          group={{group}}
          instance_type={{instance_type}}
          image={{image}}
          region={{region}}
          wait={{wait}}
          instance_tags='{"Name":"ansibled"}'
      register: ec2

values.yml

ここがハマった。どれがどれかわからなかった。

---
  image: ami-a1124fa0     [Launch Instance で AMIを選ぶときの ID]
  instance_type: t2.micro [Instance Type]
  keypair: mypair         [EC2 -> Key Pair で Key Pair Name]
  group: securitygrp      [EC2 -> Security Gruop の Group Name]
  region: ap-northeast-1  [EC2 のリージョン]
  wait: true

実行

ansible-playbook makeec2.yml -i /etc/ansible/hosts

inventory が見つけてくれなかったので -i で追加

参考

12
12
0

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
12
12