On Tue, Mar 31, 2015 at 10:16 AM, Kegan Holtzhausen
<[email protected]> 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

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAG2tFJy-BfwJFOG5ppWJZDRuwszpzFuOWu8Xw7oLXmQYTk2j2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to