· 9 years ago · Jan 03, 2017, 02:17 PM
1---
2- hosts: localhost
3 connection: local
4 gather_facts: true
5 become: true
6
7 vars_files:
8 - vars.yml
9
10 tasks:
11 - name: Launch ec2 on aws
12 ec2:
13 access_key="{{ ec2_access_key }}"
14 secret_key="{{ ec2_secret_key }}"
15 keypair="{{ ec2_keypair }}"
16 image="{{ ec2_image }}"
17 type="{{ ec2_type }}"
18 region={{ ec2_region }}
19 instance_tags="{'Name':'{{ ec2_tag }}'}"
20 count_tag="{'Name':'{{ ec2_tag }}'}"
21 exact_count="{{ ec2_instance_count }}"
22 wait=true
23 register: ec2
24 - name: Add new instance to the local host group
25 local_action: lineinfile dest=hosts regexp="{{ item.public_dns_name }}" insertafter="[launched]" line="{{ item.public_dns_name }} ansible_ssh_private_key_file=/Users/$USER/.ssh/"{{ ssh_key }}""
26 with_items: "{{ ec2.instances }}"
27 - name: Wait for instance to come up
28 local_action: wait_for host={{ item.public_dns_name }}
29 port={{ wait_for_port }} delay=180 timeout=320 state=started
30 with_items: "{{ ec2.instances }}"
31
32hosts file
33[local]
34localhost ansible_python_interpreter=/usr/local/bin/python
35
36[launched]