tasks:
- name: Launch EC2 instance
ec2:
key_name: "{{keypair}}"
group: default
count: "{{count}}"
instance_type: "{{instance_type}}"
instance_tags: '{"Name": "{{item.names}}","Type":"{{tag_Type}}","Environment":"{{tag_Environment}}"}'
image: "{{image}}"
wait: true
region: "{{region}}"
aws_access_key: *****
aws_secret_key: *****
register: ec2
with_items:
- names: Ansible-1
- names: Ansible-2
- name: One Add the newly created EC2 instance(s) to the local host group (located inside the directory)
local_action: lineinfile dest="/etc/hosts" regexp={{ item.public_ip }} insertafter="[launched]" line="{{ item.public_ip }} ansible_ssh_private_key_file=~/.ssh/{{ keypair }}.pem"
with_items: "{{ec2.instances}}"