In the stable version of ansible, even if I set the profile used by awscli in ec2 module, it failed. By the way, ec2_group module works fine. So I tried the development version and it worked, but it worked fine, but [hacking / env-setup](https: /) in Installation Procedure on the official website. /github.com/ansible/ansible/blob/devel/hacking/env-setup) adds environment variables to the fullest and it's a little difficult to handle, so I created a virtualenv environment and installed it using setup.py. .. The execution environment is OS X Yosemite + MacPorts. You can replace the virtualenv-2.7 command with virtualenv.
virtualenv-2.7 env
source env/bin/activate
pip install --upgrade pip
pip install paramiko PyYAML Jinja2 httplib2
pip install boto awscli
git clone git://github.com/ansible/ansible.git --recursive
python setup.py install
cd ..
rehash
which ansible
It's just a hobby to put awscli at this timing. If you come out to virtualenv with which ansible, it's OK
/Users/hoge/dev/unkoproject/server/infra/env/bin/ansible
Next, create a profile with awscli.
aws configure --profile toilet
After that, make an appropriate use of local_action and execute it.
echo "localhost ansible_python_interpreter=`which python`" > hosts
vi unko.yml
ansible-playbook -i hosts unko.yml
unko.yml has the following contents.
- name: Unko
hosts: 127.0.0.1
connection: local
tasks:
- name: Create Security Group
local_action:
module: ec2_group
name: unko_group
description: Security Group for Unko
region: ap-northeast-1
profile: toilet
rules:
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 25
to_port: 25
cidr_ip: 0.0.0.0/0
- name: Unko Web Server Instance
local_action:
module: ec2
region: ap-northeast-1
keypair: ToiletKey
group: unko_group
instance_type: t2.micro
image: ami-936d9d93
count: 1
wait: yes
profile: toilet
register: ec2
- name: Add Web Server instance to host group
local_action: add_host hostname={{ item.public_ip }} groupname=unko
with_items: ec2.instances
- name: Add tag to instances
local_action: ec2_tag resource={{ item.id }} region=ap-northeast-1 state=present profile=toilet
with_items: ec2.instances
args:
tags:
Name: Unko
- name: Wait for SSH to become available
pause: minutes=1
I wish I had a little more naming sense.
Recommended Posts