# Auto-incrementing Integer Assignment to hostnames

**URL:** <https://forum.ansible.com/t/auto-incrementing-integer-assignment-to-hostnames/18285>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [May 21, 2015, 10:23pm UTC](https://forum.ansible.com/t/auto-incrementing-integer-assignment-to-hostnames/18285 "2015-05-21T22:23:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raja](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@raja](https://forum.ansible.com/u/raja)\
**Post date:** [May 21, 2015, 10:23pm UTC](https://forum.ansible.com/t/auto-incrementing-integer-assignment-to-hostnames/18285/1 "2015-05-21T22:23:40Z")

</div>

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:](http://stackoverflow.com/questions/28789465/ansible-dynamic-inventory-auto-incrementing-integer-assignment-to-hostvars)

lets say if i have something like this

```auto
- 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.

```

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [May 25, 2015, 2:15am UTC](https://forum.ansible.com/t/auto-incrementing-integer-assignment-to-hostnames/18285/2 "2015-05-25T02:15:33Z")

</div>

I too wanted this. In the end I had to do without. It would be awesome if a count variable was exposed for use.

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [May 25, 2015, 3:04am UTC](https://forum.ansible.com/t/auto-incrementing-integer-assignment-to-hostnames/18285/3 "2015-05-25T03:04:04Z")

</div>

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

```auto
  with_sequence: count=3

```

```auto
    

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [May 26, 2015, 7:45am UTC](https://forum.ansible.com/t/auto-incrementing-integer-assignment-to-hostnames/18285/4 "2015-05-26T07:45:24Z")

</div>

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!
