Creating n AWS ec2 instances with n different names to each ec2 instance

89 views
Skip to first unread message

balubhai555

unread,
Mar 22, 2017, 4:53:03 PM3/22/17
to Ansible Project
Hello Team, 

I got a requirement to create 'n' number of AWS ec2 instances and with different name to each ec2 instance. I have tried different ways but got failures.

So will you please suggest me how to create different ec2 instances with different names using "Loops"  or any other modules in Ansible.

Thank you. 

Dick Visser

unread,
Mar 23, 2017, 12:38:49 AM3/23/17
to ansible...@googlegroups.com
On Wed, 22 Mar 2017 at 21:53, balubhai555 <balub...@gmail.com> wrote:
Hello Team, 

I got a requirement to create 'n' number of AWS ec2 instances and with different name to each ec2 instance. I have tried different ways but got failures.

What were those ways exactly, and what were the failures you got with them?


Dick
--
Dick Visser
Sr. System & Network Engineer
GÉANT

Want to join us? We're hiring: https://www.geant.org/jobs
Message has been deleted

balubhai555

unread,
Mar 28, 2017, 12:20:36 PM3/28/17
to Ansible Project
Hello  Dick Visser, 

I created 4 instances using Ansible EC2 module. I want to give names as A,B,C,D. I want to give those names as "with_items: " loops. So what is the possible ways to modify the below code. I created 4 instances using "count:4".  

---
- name: Create a AWS  instance
  hosts: all
  gather_facts: true
  vars:
    keypair: *************
    instance_type: t2.micro
    image: ami-b63769a1
    region: us-east-1
    tag_Type: "webserver"
    tag_Environment: "production"

   tag_name: {"A", "B", "C", "D"}
   count: 4

  tasks:
  - name: Install python-setuptools using yum
    yum: pkg=python-setuptools

  - name: Launch EC2 instance
    ec2:
       key_name: "{{keypair}}"
       group: default
       count: "{{count}}"
       instance_type: "{{instance_type}}"
       instance_tags: '{"Name": "{{tag_name}}","Type":"{{tag_Type}}","Environment":"{{tag_Environment}}"}'
       image: "{{image}}"
       wait: true
       region: "{{region}}"
    #  state: "stopped"
       aws_access_key: *****************
       aws_secret_key: ****************
    register: ec2
    with_items: tag_name


please help me to do it using with_item or any other way. Or suggest me similar playbooks for creating n instances.
 

Sudhanshu Sharma

unread,
Apr 17, 2017, 3:05:31 PM4/17/17
to Ansible Project
Hey,

You can use ec2_tag module, Go thru the link below for detailed explanation -

balubhai555

unread,
Apr 25, 2017, 11:47:35 AM4/25/17
to Ansible Project
Yes, Thanks for quick answer.

But for ec2_tag requires "resource: vol-xxx". But when the resource created at the time only resource id generated.
As i am creating different instances at a time, how can i use the ec2_tag module with "resource: "  

Please suggest me.

Thank you in advance.
Reply all
Reply to author
Forward
0 new messages