create multiple ec2 instances assign statis IP and tags

209 views
Skip to first unread message

Tony Wong

unread,
Jan 21, 2022, 9:28:46 AM1/21/22
to Ansible Project
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 }}"

---------------------------

how do i put the names in my var file? 

deploy_env:
  instance_type: t2.micro
  image: ami-02d03ce209db75523
  sg_group:
    - "sg-057771872265bfda6"
  region: us-west-1
  assign_ip: 10.10.1.10
  vpc_subnet_id: subnet-0d9d37440a2265163
  instance_tags:
    name: test1


----------------


I need to apply ip address 10.10.1.11 - 10.10.1.20 and different tags to the ec2 instances






Tony Wong

unread,
Jan 22, 2022, 11:43:19 AM1/22/22
to ansible...@googlegroups.com
any idea? where do i put my ec2 tag names and assign to the instances

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/fsQdjZ1Ica8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7a17e070-baa9-4dae-9429-4dde024caa25n%40googlegroups.com.

Dick Visser

unread,
Jan 22, 2022, 12:19:33 PM1/22/22
to ansible...@googlegroups.com
Depends on how many more requirements you come up with in every new mail 



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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALmkhkpnU7r3cQ4gjsEoLzNKGVs1w2QWsYeXfXJAOoJPLqwAjA%40mail.gmail.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Evan Hisey

unread,
Jan 22, 2022, 12:44:29 PM1/22/22
to Ansible Project
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.

Tony Wong

unread,
Jan 23, 2022, 10:01:52 AM1/23/22
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/fsQdjZ1Ica8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

Dick Visser

unread,
Jan 24, 2022, 3:51:24 AM1/24/22
to ansible...@googlegroups.com
Hii


On Sun, 23 Jan 2022 at 16:01, Tony Wong <tdub...@gmail.com> wrote:
>
> 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



Tony Wong

unread,
Jan 25, 2022, 12:45:41 PM1/25/22
to Ansible Project

Thank you. This works great. 

Tony Wong

unread,
Feb 8, 2022, 10:05:56 AM2/8/22
to ansible...@googlegroups.com
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
}


---------------------------------------------------------------------------

---
- 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: yes
wait_timeout: 600
count: 1
region: "{{ deploy_env.region }}"
vpc_subnet_id: "{{ deploy_env.vpc_subnet_id}}"
private_ip: "{{ item.ip }}"
register: ec2
with_items: "{{ instances }}"
loop: "{{ instances }}"
async: 60
poll: 0

- name: wait for instance creation to complete
async_status:
jid: ec2.ansible_job_id
register: ec2_jobs
until: ec2_jobs.finished
retries: 300

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/fsQdjZ1Ica8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

Dick Visser

unread,
Feb 8, 2022, 10:11:46 AM2/8/22
to ansible...@googlegroups.com
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 }}"



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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALmkhkroS%2BrO8yCizkvXCjF3XFACJyz%2BaW%3DNMRzjg2ZsEC%3DtfQ%40mail.gmail.com.


--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Tony Wong

unread,
Feb 8, 2022, 10:56:31 AM2/8/22
to ansible...@googlegroups.com
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 

Tony Wong

unread,
Feb 8, 2022, 10:59:58 AM2/8/22
to ansible...@googlegroups.com
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?

Dick Visser

unread,
Feb 8, 2022, 11:05:09 AM2/8/22
to ansible...@googlegroups.com
On Tue, 8 Feb 2022 at 16:56, Tony Wong <tdub...@gmail.com> wrote:
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?

Just create another file and include that

--

Dick Visser

unread,
Feb 8, 2022, 11:08:36 AM2/8/22
to ansible...@googlegroups.com
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 

Reply all
Reply to author
Forward
0 new messages