amazon web services - Best way to launch aws ec2 instances with ansible -
i'm trying create small webapp infrastructure ansible on amazon aws , want process: launch instance, configure services, etc. can't find proper tool or module deal ansible. ec2 launch.
thanks lot.
this short answer of question, if want detail , automated role, please let me know. thanks
prerequisite:
ansible
python boto library
set aws access , secret keys in environment settings
(best inside ~./boto)
to create ec2 instance(s):
in order create ec2 instance, please modified these parameters can find inside "ec2_launch.yml" file under "vars":
- region # want launch instance(s), usa, australia, ireland etc
count # number of instance(s), want create
once, have mentioned these parameter, please run following command:
ansible-playbook -i hosts ec2_launch.yml
contents of hosts file:
[local] localhost [webserver]
contents of ec2_launch.yml file:
--- - name: provision ec2 instance hosts: local connection: local gather_facts: false tags: provisioning # necessary variables creating/provisioning ec2 instance vars: instance_type: t1.micro security_group: webserver # change security group name here image: ami-98aa1cf0 # change ami, want launch server region: us-east-1 # change region keypair: ansible # change keypair name count: 1 # task used launch/create ec2 instance tasks: - name: create security group local_action: module: ec2_group name: "{{ security_group }}" description: security group webserver servers region: "{{ region }}" rules: - proto: tcp type: ssh 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 rules_egress: - proto: type: cidr_ip: 0.0.0.0/0 - name: launch new ec2 instance local_action: ec2 group={{ security_group }} instance_type={{ instance_type}} image={{ image }} wait=true region={{ region }} keypair={{ keypair }} count={{count}} register: ec2 - name: add newly created ec2 instance(s) local host group (located inside directory) local_action: lineinfile dest="./hosts" regexp={{ item.public_ip }} insertafter="[webserver]" line={{ item.public_ip }} with_items: "{{ ec2.instances }}" - name: wait ssh come local_action: wait_for host={{ item.public_ip }} port=22 state=started with_items: "{{ ec2.instances }}" - name: add tag instance(s) local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present with_items: "{{ ec2.instances }}" args: tags: name: webserver
Comments
Post a Comment