edwinkamal
edwinkamal

Reputation: 61

How to create the Count tags name with sequential numbers using ansible script

I create 2 windows aws machine using exact_count tag as 2.

It creates the both of 2 AWS machine with same name.

For example:

1) itg-Web-windows

2) itg-web-windows

I want to create the machine Name as instance_tags:

1)itg-windows-web-1

2)itg-windows-web-2

Below are my code:

name: ensure instances are running
        ec2:
          region: "{{ region }}"
          image: "{{ image_id }}"
          group_id: sg-1234
          vpc_subnet_id: subnet-5678
          instance_tags:
             Name: "itg-windows-web"
          exact_count: 2
          count_tag:
             Name: "itg-windows-web"`
          register: ec2_result

Upvotes: 2

Views: 1686

Answers (2)

Christina A
Christina A

Reputation: 430

This will create servers with name tags web_server_1, web_server_3 and web_server_5:

- name: create instances
  ec2:
    - image: <your_ami>
      instance_type: t2.micro
      key_name: <your_ssh_key>
      region: us-east-1
      vpc_subnet_id: <your_subnet_id>
      count_tag:
        Name: "web_server_{{ item }}"
      exact_count: 1
      instance_tags: 
        Name: "web_server_{{ item }}"
  with_items: ['1', '3', '5']

Upvotes: 2

Madhukar Mohanraju
Madhukar Mohanraju

Reputation: 2863

Use the below ansible template:

---
- name: A sample template
  hosts: local
  connection: local
  gather_facts: False
  tasks:
    - name: create instance
      ec2:
        keypair: test-ssh-key
        instance_type: t2.micro
        image: ami-abcd1234
        wait: yes
        instance_tags:
            ec2type: web
        exact_count: "{{ count }}"
        count_tag:
            ec2type: web
        region: us-east-1
        vpc_subnet_id: subnet-1234abcd
      register: ec2

    - name: generate sequence id for tagging
      debug: msg="{{ item }}"
      no_log: true
      with_sequence: start="{{ startindex }}" end="{{ count }}" format=%02d
      register: sequence

    - name: tag instances
      no_log: true
      ec2_tag:
        region: us-east-1
        resource: "{{ item.0.id }}"
        tags:
            Name: "itg-windows-web-{{ item.1.msg }}"
      with_together:
        - "{{ ec2.instances }}"
        - "{{ sequence.results }}"

command:

ansible-playbook -i ./hosts ec2-basic.yml --extra-vars "startindex=1 count=2"

Invocation-1:

ansible-playbook -i ./hosts ec2-basic.yml --extra-vars "startindex=1  count=2"

This will create 2 instances and attach name tag itg-windows-web-01 and itg-windows-web-02 to it.

Invocation 2:

ansible-playbook -i ./hosts ec2-basic.yml --extra-vars "startindex=3  count=4"

This will add 2 more instances and attach name tag itg-windows-web-03 and itg-windows-web-04 to it.

All these instances are grouped by ec2type tag.

How it works:

Use a custom tag other than Name tag for attribute count_tag. If you use Name tag, then the same tag-value is assigned for all the instances that are created(which defeats your purpose). In the above script, I have used ec2type: web as my instance_tags and count_tag. So ansible will use this tag to determine how many nodes should run based on the specific tag criteria.

The count value which you pass is assigned to exact_count in the template. Also you can have further control by passing startindex which controls the start of sequence.

with_sequence generates a sequence based on your input. Click here to read more about it.

with_together loops over parallel set of data. Click here to read more about it.

Using the above ansible loops, we append 01, 02 ... and so on to itg-windows-web text and add it to the instance Name tag.

Upvotes: 1

Related Questions