ec2_tag existing instances

807 views
Skip to first unread message

Kegan Holtzhausen

unread,
Mar 31, 2015, 9:17:53 AM3/31/15
to ansible...@googlegroups.com
Hi,

Sorry for a bit of a noobish question, but I am provisioning some instances in ec2, and I can successfully create and tag the instances in my provision playbook.
However, later I try and add a tag to exiting hosts based on a tag_Name, but I cannot seem to get the resource-id's I need for ec2_tag. 

Example

---
- name: tag something
  vars
:
    tag_name
: ami_test
  hosts
: "tag_Name_{{tag_name}}"
  user
: admin
  tasks
:
   
- name: Add tag to instances
      local_action
: ec2_tag resource="{{item.id}}" region="{{aws_region}}" state=present
      with_items
: ec2.instances
      args
:
        tags
:
         
Name: "{{tag_name}}"


So when I run this with:
bash-3.2$ ansible-playbook tag.yml -e tag_name=dev_test

PLAY [tag something] ****************************************************** 

GATHERING FACTS *************************************************************** 
ok: [52.28.9.65]

TASK: [Add tag to instances] ************************************************** 
fatal: [52.28.9.65 -> 127.0.0.1] => One or more undefined variables: 'unicode object' has no attribute 'id'

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/Users/keghol/tag.retry

52.28.9.65                 : ok=1    changed=0    unreachable=1    failed=0   


I think the problem is that I don't understand how register works, or how to get my instance-id's. And I am pretty sure with_items: ec2.instasnces will freak out next.

Thanks in advance.
/K


Ivan S. Freitas

unread,
Mar 31, 2015, 12:01:53 PM3/31/15
to ansible...@googlegroups.com
On Tue, Mar 31, 2015 at 10:16 AM, Kegan Holtzhausen
<kegan.ho...@kegans.com> wrote:

> Example
>
> ---
> - name: tag something
> vars:
> tag_name: ami_test
> hosts: "tag_Name_{{tag_name}}"
> user: admin
> tasks:
> - name: Add tag to instances
> local_action: ec2_tag resource="{{item.id}}" region="{{aws_region}}"
> state=present
> with_items: ec2.instances
> args:
> tags:
> Name: "{{tag_name}}"

The ec2 (and hence the ec2.instance) variable is not defined in this
scope, it only exists after the task responsible for creating a new
instance is completed successfully.

To tag existing instances with hosts from EC2, you can use the ec2_id
variable defined by the aws dynamic inventory script. For example:

---
- hosts:
- tag_Name_{{tag_name}}
user: ec2-user
tasks:
- name: Tag instances
sudo: False
local_action: >
ec2_tag
resource="{{ ec2_id }}"
region=sa-east-1
state=present
aws_access_key={{ lookup('env', 'AWS_ACCESS_KEY_ID') }}
aws_secret_key={{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}
args:
tags:
Name: "{{ tag_name }}"

--
Ivan Sichmann Freitas

Kegan Holtzhausen

unread,
Apr 1, 2015, 6:55:58 AM4/1/15
to ansible...@googlegroups.com
Thanks! that did the trick!

Are these variables documented anywhere in detail? 

Thanks
/K
Message has been deleted

Ivan S. Freitas

unread,
Apr 1, 2015, 1:25:36 PM4/1/15
to ansible...@googlegroups.com
I think those variables are documented in the ec2 dynamic inventory
script. I don't now if there's a way to only add a tag, but you could
try using a different group in your hosts line, e.g., try selecting
your hosts based in another tag value besides Name. Or you may be able
to limit this task with a "when" clause using variables defined by the
ec2 script, but I never tested this.

On Wed, Apr 1, 2015 at 8:02 AM, Kegan Holtzhausen
<kegan.ho...@kegans.com> wrote:
> The tag command works, but it seems to re-tag all my instances who match the
> filter. Is there something I need to do to "add" tags only?
>
> I am trying to tag nodes which I deploy specific hazelcuts applications to
> in order to setup hz cluster detection.
>
> /K
>
> On Tuesday, March 31, 2015 at 6:01:53 PM UTC+2, Ivan Sichmann wrote:
>>
> --
> 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/ad11a9c6-7cc3-4fae-b0c9-5e306a8cbeaf%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Ivan Sichmann Freitas
Reply all
Reply to author
Forward
0 new messages