create multiple ec2 instances assign statis IP and tags

hi

any idea how I can create multiple ec2 instances and tag it

main.yml

  • include_vars: vars/main.yml
  • name: create the ec2 instance
    ec2:
    assign_public_ip: no
    group_id: ‘{{ deploy_env.sg_group }}’
    instance_type: “{{ deploy_env.instance_type }}”
    image: “{{ deploy_env.image }}”
    wait: true
    wait_timeout: 600
    count: 10
    region: “{{ deploy_env.region }}”
    vpc_subnet_id: “{{ deploy_env.vpc_subnet_id}}”
    private_ip: “{{ deploy_env.assign_ip }}”
    instance_tags:
    Name: “{{ deploy_env.instance_tags.name }}”

any idea? where do i put my ec2 tag names and assign to the instances

Depends on how many more requirements you come up with in every new mail

I would say you are 95% done. You are just missing a loop and list of lists. Looking at the snippet, I think you may be making life a touch more difficult than needed. Are you writing a role or a playbook? What is your programing/ansible experience level? I see a similar mistakes in other emails you have posted, so trying to gauge answer level, as what you seem to be doing is while not advanced, also not trivial as it requires a decent understanding of aws, lists, ansible and the limits of loops.

I am just trying to create 2 or more ec2 instances. I need to assign them static ips and unique tag names. the playbook I created can only do one at a time and I am not sure how to modify it to do multiple

Hii

I am just trying to create 2 or more ec2 instances.

Here you say 2

  • name: create the ec2 instance
    ec2:
    assign_public_ip: no
    group_id: ‘{{ deploy_env.sg_group }}’
    instance_type: “{{ deploy_env.instance_type }}”
    image: “{{ deploy_env.image }}”
    wait: true
    wait_timeout: 600
    count: 10

But here it says 10 are created.
https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_module.html#parameter-count

Assuming this is an oversight, this should work:

  • include_vars: vars/main.yml

  • name: create the ec2 instance

ec2:

assign_public_ip: no

group_id: ‘{{ deploy_env.sg_group }}’

instance_type: “{{ deploy_env.instance_type }}”

image: “{{ deploy_env.image }}”

wait: true

wait_timeout: 600

count: 1

region: “{{ deploy_env.region }}”

vpc_subnet_id: “{{ deploy_env.vpc_subnet_id}}”

private_ip: “{{ item.ip }}”

instance_tags:

Name: “{{ item.name }}”

loop: “{{ instances }}”

And then in your vars file:

deploy_env:

instance_type: t2.micro

image: ami-02d03ce209db75523

sg_group:

  • “sg-057771872265bfda6”

region: us-west-1

vpc_subnet_id: subnet-0d9d37440a2265163

instances:

  • name: test1

ip: 10.10.1.10

  • name: test2

ip: 10.10.1.12

  • name: test43

ip: 10.10.1.77

Thank you. This works great.

hi,

any idea why I keep getting this error when i run this asyn job?

fatal: [localhost]: FAILED! => {
“ansible_job_id”: “ec2.ansible_job_id”,
“attempts”: 1,
“changed”: false,
“finished”: 1,
“invocation”: {
“module_args”: {
“_async_dir”: “/Users/tonywong/.ansible_async”,
“jid”: “ec2.ansible_job_id”,
“mode”: “status”
}
},
“msg”: “could not find job”,
“started”: 1
}

yes

You loop over the ec2_instance task.
So, you will also need to loop over the result.

I think this should work:

  • name: wait for instance creation to complete
    async_status:
    jid: “{{ item.ansible_job_id }}”
    register: ec2_jobs
    until: ec2_jobs.finished
    retries: 300
    loop: “{{ ec2.results }}”

awesome. Thanks.

so is there a way to have my inventory file in its own separate text file.?

I have it all in the vars.yml file now?

deploy_env:
instance_type: t2.micro
image: ami-02d03ce209db75523
sg_group:

  • “sg-057771872265bfda6”
    region: us-west-1
    vpc_subnet_id: subnet-0d9d37440a2265163
    instance_tags:
    date: 1.20.2022

instances:

  • name: test1
    ip: 10.10.1.10
  • name: test2
    ip: 10.10.1.11

also what if I got 100s servers to deploy but from different instance types/image_ids/SGs/Subnets ?

would i have to create separate env variables for these?

Just create another file and include that

You define what you need.
Settle on a data structure that suits your use case.
It all depends on how different each instance is.
Is there no common pattern?
You can supply defaults in the task, and then optionally have them in your instances list.

instances:

  • name: foo
    type: t4g.nano
    ip: 10.1.1.1
  • name: bar
    ip: 10.1.2.3

'etc etc