Auto-incrementing Integer Assignment to hostnames

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.

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

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
    


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!