Can't get returned IPs from EC2

246 views
Skip to first unread message

Matthew Morgan

unread,
Aug 15, 2014, 6:35:14 PM8/15/14
to ansible...@googlegroups.com
So, I'm super stuck on this problem.  I created a pastie of all the info.


What am I doing wrong in the playbook that I can't get those IPs into my hosts file?  

Any help would be greatly appreciated!


Tomasz Kontusz

unread,
Aug 15, 2014, 6:56:39 PM8/15/14
to ansible...@googlegroups.com
add_host is for adding a host to current run's in-memory inventory, not the one on disk. It's used to provision a newly-created VMs.

You want to either use dynamic inventory and pull data from EC2, or somehow add the host to static inventory (with lineinfile maybe?)


--
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/8d21cfef-aa4f-4009-abc4-f9e2c235b8be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthew Morgan

unread,
Aug 15, 2014, 10:37:28 PM8/15/14
to ansible...@googlegroups.com
Okay, I understand that it only adds to the in-memory inventory, but that's not really the problem I'm having.

The problem I'm having is I can't get the IP.  What I plan to do with the IP after I get it involves provisioning those hosts.  I can't provision those hosts unless I get the IP from the current run which creates the host.

The step where I should be able to get the IP to add it to a group (in memory or otherwise) is failing.

TASK: [Add new instance to host group] **************************************** 
fatal: [localhost -> 127.0.0.1] => One or more undefined variables: 'str object' has no attribute 'tagged_instances'

FATAL: all hosts have already failed -- aborting


---
- name: Create EC2 Instances in VPC
  gather_facts: False
  hosts: local
  vars:
    security_group: my-sg
    instance_type: t2.medium
    image: ami-000111222
    region: us-east-1
    key_name: my-key
    attr:
      - { zone: us-east-1a, az: a, subnet: subnet-12345678 }
      - { zone: us-east-1b, az: b, subnet: subnet-23456789 }
      - { zone: us-east-1d, az: d, subnet: subnet-34567890 }
  tasks:
    - name: Create Nodes in VPC
      local_action: 
        module: ec2 
        group: "{{ security_group }}" 
        instance_type: "{{ instance_type }}" 
        image: "{{ image }}" 
        wait: "yes"
        vpc_subnet_id: "{{ item.subnet }}"
        region: "{{ region }}" 
        zone: "{{ item.zone }}" 
        key_name: "{{ key_name }}"
        instance_tags: 
          Name: my-cool-tag-{{item.az}}-vpc
        exact_count: 1
        count_tag:
          Name: my-cool-tag-{{item.az}}-vpc
      with_items: attr
      register: ec2

    - name: dump output of ec2.results
      debug: var=ec2.results

    - name: Add new instance to host group
      local_action: add_host hostname={{item.tagged_instances.private_ip}} groupname=launched
      with_items: 
        - ec2.results

    - name: Wait for SSH to come up
      local_action: wait_for host={{item.instances.private_ip}} port=22 delay=60 timeout=320 state=started
      with_items: 
        - ec2.results

- name: Configure instances
  hosts: launched
  gather_facts: True
  sudo: True
  roles:
    - my-role

Tomasz Kontusz

unread,
Aug 16, 2014, 3:21:14 AM8/16/14
to ansible...@googlegroups.com
On 16.08.2014 04:37, Matthew Morgan wrote:
Okay, I understand that it only adds to the in-memory inventory, but that's not really the problem I'm having.

The problem I'm having is I can't get the IP.  What I plan to do with the IP after I get it involves provisioning those hosts.  I can't provision those hosts unless I get the IP from the current run which creates the host.

The step where I should be able to get the IP to add it to a group (in memory or otherwise) is failing.

TASK: [Add new instance to host group] **************************************** 
fatal: [localhost -> 127.0.0.1] => One or more undefined variables: 'str object' has no attribute 'tagged_instances'

FATAL: all hosts have already failed -- aborting
Sorry, I only skimmed that paste and missed the actual problem.
For the record: it's easier if you paste examples in your mail, and it's better for archiving too :-)
I think what you meant is "run this task for all of ec2.results", but what you wrote is "run this task for all of ['ec2.results']" (that's why it's complaining about a string).
You want:
      with_items: ec2.results

Dash at the beginning of a line creates a list element in YAML (that's why all tasks start with it :-))

Matthew Morgan

unread,
Aug 16, 2014, 11:28:26 PM8/16/14
to ansible...@googlegroups.com
That was exactly my issue.  Thank you very much for your help!
Reply all
Reply to author
Forward
0 new messages