Problem has no attribute 'public_ip when I execute a playbook AWS

Hello guys,
I am provisioning instances in AWS, however, when adding a snapshot to the “temp” inventory, this error appears below:

- name: Adding a temp inventory
  ansible.builtin.add_host:
      name: "{{ item.public_ip }}"
       groups: giropops-new
   with_items: "{{ ec2.instances }}"

fatal: [localhost]: FAILED! => {“msg”: "The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘public_ip’. ‘dict object’ has no attribute ‘public_ip’

I’ve already searched the internet and read the Ansible manual, but so far I haven’t found any solution to this problem.

Thanks in advance.

Hello @dedeboy

What’s values does ec2.instances include?
Could you try to check the debug module to see if the public_ip are included in ec2.instances list items?

FYI, for provisioned instances, another approach is to use dynamic inventory instead of add_host module.

2 Likes

In addition to a debug task to see what keys are available, modules should document the return structure (assuming you’re registering the ec2 variable). If you’re using the ec2_instance module, for example the variable is documented here as public_ip_address, not public_ip.

3 Likes

Hello guys,
Sorry for delay in write post. First all I want to thank you, @shertel and @akira6592 to me help about this issue.
I got solved it :slight_smile: I noticed that when I went to provisioning my AWS instance I forgot mentioned assign an IP public for it, then when I was running my playbook in that part “add_host” the IP address couldn’t find the IP to store the information, then error message occurred.

@shertel thank you, I fixed up it.

Thank in advance.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.