Auto-incrementing Integer Assignment to hostnames

1,023 views
Skip to first unread message

ra...@leaninfotech.net

unread,
May 22, 2015, 11:57:44 AM5/22/15
to ansible...@googlegroups.com
Hi ,

  I am trying to provision my aws infrastructure using ansible and i wanted to see if there is a way to include an Auto-incrementing Integer Assignment to Hostvars. For ex:


lets say if i have something like this

- ec2:
    key_name: mykey
    instance_type: t2.micro
    image: ami-123456
    wait: yes
    group: webserver
    count: 3
    vpc_subnet_id: subnet-29e63245
    assign_public_ip: yes



can it create instances with below names ?

dev-web-01
dev-web-02
dev-web-03


Thanks in advance for any help.

Barry Kaplan

unread,
May 24, 2015, 10:15:33 PM5/24/15
to ansible...@googlegroups.com
I too wanted this. In the end I had to do without. It would be awesome if a count variable was exposed for use.

benno joy

unread,
May 24, 2015, 11:04:08 PM5/24/15
to ansible...@googlegroups.com
one way to do this would be:

- ec2:

    key_name: mykey

    instance_type: t2.micro

    image: ami-123456

    wait: yes

    group: webserver

    exact_count: 1

    count_tags:

      Name: "{{ 'dev-web-' + item }}" 

    instance_tags:

      Name: "{{ 'dev-web-' + item }}" 

    vpc_subnet_id: subnet-29e63245

    assign_public_ip: yes

  with_sequence: count=3
    



On Mon, May 25, 2015 at 7:45 AM, Barry Kaplan <mem...@gmail.com> wrote:
I too wanted this. In the end I had to do without. It would be awesome if a count variable was exposed for use.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6c5bf7dd-4b9c-4ff4-90e5-cf1cee5c609a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Barry Kaplan

unread,
May 26, 2015, 3:45:24 AM5/26/15
to ansible...@googlegroups.com
Hmm, that might work. I need to spread the instances over the availability zones so I have been setting up something like:

    instances:
     
- {subnet: "{{vpc_subnet.private.a}}", count: 3 }
     
- {subnet: "{{vpc_subnet.private.b}}", count: 3 }
     
- {subnet: "{{vpc_subnet.private.c}}", count: 3 }
     
- {subnet: "{{vpc_subnet.private.d}}", count: 1 }


   
- name: Create instances
      ec2
:
        image
: "{{ubuntu_image.ami}}"
        region
: "{{ec2_region}}"
        vpc_subnet_id
: "{{item.subnet.id}}"
        instance_type
: "{{api_proxy_instance_type}}"
        assign_public_ip
: false
       
group: "{{security_groups}}"
        instance_tags
:
         
Name: "{{env_prefix}}{{instance_name}}"
          api_proxy
: group
          subnet
: "{{item.subnet.id}}"
          env
: "{{env}}"
          managed_by
: ansible
        exact_count
: "{{item.count}}"
        count_tag
:
          api_proxy
: group
          subnet
: "{{item.subnet.id}}"
        key_name
: "{{aws_key_name}}"
        wait
: yes
     
register: ec2_proxy
      with_items
: instances



I looks like using with sequence and (item % 4) to select the availability zone would then leave the {{item}} to be used a suffix.

I will have to try this. Thanks Benno!
Reply all
Reply to author
Forward
0 new messages