issue setting tags on ec2 module

60 views
Skip to first unread message

Hernandes Sousa

unread,
Jul 22, 2014, 1:53:52 PM7/22/14
to ansible...@googlegroups.com
I'm having trouble setting up instance_tags in the following playbook:


vars:
    key_name: mykey
    instance_type: t2.micro
    security_group: sg-xxxxxxxx
    image: ami-f5b815e8
    region: sa-east-1
    subnet_id: subnet-5eaxxxxb
    instance_tags:
      foo: bar


tasks:
    - name: Launch instance
      local_action: ec2 
                    key_name={{ key_name }}
                    group_id={{ security_group }}
                    instance_type={{ instance_type }}
                    image={{ image }}
                    wait=yes 
                    region={{ region }}
                    assign_public_ip=yes
                    vpc_subnet_id={{ subnet_id }}
                    instance_tags="{{ instance_tags }}" # not working
      register: ec2


I get "msg: this module requires key=value arguments", does anyone know the correct way to add the dictionary in this case? I've tried a variety of combinations without much success.

Brent Langston

unread,
Jul 22, 2014, 2:02:42 PM7/22/14
to ansible...@googlegroups.com
This might be old'ish style at this point, but this is how I do it:

    - name: tag my launched instances
      local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
      with_items: ec2.instances
      args:
       tags:
        Name: "{{assigned_hostname.stdout}}"
        color: "{{oscar_color.stdout}}"
        environment: "{{oscar_environment}}"
        os: "{{os}}"
        owner: "{{owner}}"
        phi: "{{phi}}"
        pool: "{{pool}}"
        primary_role: "{{primary_role}}"
        branch: "{{branch}}"

Hope that helps.

--------
Brent
--------


--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/99f1a77f-35f6-4920-b260-04ff433f245d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Hernandes Sousa

unread,
Jul 22, 2014, 2:18:58 PM7/22/14
to ansible...@googlegroups.com
That was it Brent thanks a lot! You pointed me to the right direction. I knew the ec2_tags was an option but I'd like to place the tags in the same call. So I followed the same structured as you proposed:

vars:
    key_name: mykey
    instance_type: t2.micro
    security_group: sg-xxxxxxxx
    image: ami-f5b815e8
    region: sa-east-1
    subnet_id: subnet-5eaxxxxb
    instance_tags:
      foo: bar

tasks:
    - name: Launch instance
      local_action: ec2 
                    key_name={{ key_name }}
                    group_id={{ security_group }}
                    instance_type={{ instance_type }}
                    image={{ image }}
                    wait=yes 
                    region={{ region }}
                    assign_public_ip=yes
                    vpc_subnet_id={{ subnet_id }}
      args:
        instance_tags: "{{ instance_tags }}"
      register: ec2

I will raise a PR adding this example to the documentation may anyone need it.

If someone knows of a more elegant way of doing this please let me know.

Thanks
Hernandes

Michael DeHaan

unread,
Jul 22, 2014, 3:13:08 PM7/22/14
to ansible...@googlegroups.com
You can stick absolutely everything under args if you want, might make it feel a bit more consistent.

Also "local_action" is usually not needed in cloud provisioning tasks as you can just have a play that talks to localhost

- hosts: localhost
  tasks:
   - ec2:
       x: 1
       y: 2
   - ...

etc






Brent Langston

unread,
Jul 24, 2014, 5:26:08 PM7/24/14
to ansible...@googlegroups.com

On Tue, Jul 22, 2014 at 3:13 PM, Michael DeHaan <mic...@ansible.com> wrote:
Also "local_action" is usually not needed in cloud provisioning tasks as you can just have a play that talks to localhost

yeah, this was written before localhost was handled the way it is today. I haven't gone back and edited this in quite some time. 



--------
Brent
--------
Reply all
Reply to author
Forward
0 new messages