Ansible: register: ec2 / with_items: ec2.instances - results in "str object has no attribute"

Created on 18 Jun 2015  ·  3Comments  ·  Source: ansible/ansible

I have a playbook that's very similar to examples online:


tasks:
- name: create an EC2 instance
ec2:
[-snip-]
register: ec2
- name: print it
debug: msg="item has - {{ item.public_ip }}"
with_items: ec2.instances


This gives the error:


fatal: [localhost] => One or more undefined variables: 'str object' has no attribute 'public_ip'

If I change it to {{ item }} I see:

TASK: [print it] ********************
ok: [localhost] => (item=ec2.instances) => {
"item": "ec2.instances",
"msg": "item has - ec2.instances"
}


It seems that 'ec2.instances' is merely a string? Lastly, if I use "with_items: ec2.results", I can iterate through it but I have to reference it as item.instances.0.public_ip

Any idea why ec2_instances appears to be a string as opposed to a dictionary?

Edit:
: arp_kiki/pts/55:0.08:new; ansible --version ansible 1.5.4

after upgrading:

ok: [localhost] => (item=ec2.instances) => {
"item": "ec2.instances",
"msg": "item has - ec2.instances"
}

PLAY RECAP **********************
localhost : ok=2 changed=1 unreachable=0 failed=0

: arp_kiki/pts/55:0.31:new; ansible --version
ansible 1.9.1

Most helpful comment

Understood. I assumed it was a bug because I was following the documentation and getting different results.

All 3 comments

Hi!

Thanks very much for your interest in Ansible. It sincerely means a lot to us.

This appears to be a user question, and we'd like to direct these kinds of things to either the mailing list or the IRC channel.

also the reason is because with_items is used hence there woudl be multiple instances and the results of each instance created is stored in the ec2.results dict. if there is only one single instance create avoild with_items then you shoudl get what you need or if you have multiple instances to create then to get public ip's use with_items: ec2.results and item.public_ip

IRC: #ansible on irc.freenode.net
mailing list: https://groups.google.com/forum/#!forum/ansible-project
If you can stop by there, we'd appreciate it. This allows us to keep the issue tracker for bugs, pull requests, RFEs and the like.

Thank you once again and we look forward to seeing you on the list or IRC. Thanks!

Understood. I assumed it was a bug because I was following the documentation and getting different results.

I just came to this thread due to making the same "user error", which @bennojoy described the cause of very succinctly. Finding this to be a highly "non-obvious" situation, it'd be great to have reference to this, at least in the examples of the ec2 module documentation, as with_items is going to be a very common pattern managing systems in set availability zones.

Was this page helpful?
0 / 5 - 0 ratings