Hello team,
I want to
-
Create AWS ec2 instance using one "with_items: ec2"
-
After that I want to insert the IP address of those instances into /etc/ansible/hosts file. For that I am using local_action: lineinfile . But after that I want to use again with_items: “{{ec2.instances}}”
At that time it shows error, That Item is undefined.
Are we use those with_items twice in a playbook. Any example please suggest me.
Thank you in advance.
A variable does not disappear after being used in an item loop, so I suspect you aren’t referring to the variable correctly in the second task. Please post an example playbook and the full -vvvv output with the error.
if you debug that you will get the real result name, it may be something like ec2.results.instances or something like that. can you post the debug output of ec2 variable?
Thanks for your reply,
Will you please explain more. Thank you.
tasks:
- name: Launch EC2 instance
ec2:
key_name: “{{keypair}}”
group: default
count: “{{count}}”
instance_type: “{{instance_type}}”
instance_tags: ‘{“Name”: “{{item.names}}”,“Type”:“{{tag_Type}}”,“Environment”:“{{tag_Environment}}”}’
image: “{{image}}”
wait: true
region: “{{region}}”
aws_access_key: *****
aws_secret_key: *****
register: ec2
with_items:
- names: Ansible-1
- names: Ansible-2
debug: var=ec2
add the above bold line in your playbook, it will throw an output , paste that here
Thanks for your quick reply. Already entered those bold lines.
Got the same issue.
can you please show the output of it…i dont see that in the screenshot