# Error register variable "The task includes an option with an undefined variable"

**URL:** <https://forum.ansible.com/t/error-register-variable-the-task-includes-an-option-with-an-undefined-variable/36647>\
**Category:** Ansible Project\
**Created:** [May 19, 2022, 7:32pm UTC](https://forum.ansible.com/t/error-register-variable-the-task-includes-an-option-with-an-undefined-variable/36647 "2022-05-19T19:32:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [May 19, 2022, 7:32pm UTC](https://forum.ansible.com/t/error-register-variable-the-task-includes-an-option-with-an-undefined-variable/36647/1 "2022-05-19T19:32:09Z")

</div>

Hello, 30 minutes ago I am reviewing my playbook and I do not find the error, I am going crazy, i see this error:

I run the playbook this way

/usr/bin/ansible-playbook -i hosts --extra-vars “CARRIER=xx” tasks/create\_ec2\_stage.yaml

I see this error:

fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: list object has no element 0\n\nThe e  
rror appears to be in ‘/etc/ansible/loyalty/tasks/create\_ec2\_stage.yaml’: line 63, column 7, but may\nbe elsewhere in the file depending on the exac  
t syntax problem.\n\nThe offending line appears to be:\n\n register: ec2\_metadata\n - name: Parse \< JSON \>\n ^ here\n”}

My playbook:

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 19, 2022, 7:40pm UTC](https://forum.ansible.com/t/error-register-variable-the-task-includes-an-option-with-an-undefined-variable/36647/2 "2022-05-19T19:40:00Z")

</div>

The problem is with this line:

ip\_addr: “{{ ec2\_metadata.instances[0].network\_interfaces[0].private\_ip\_address }}”

One of those llist vars is empty. You will probably need to debug them, to see what the data structure is.

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [May 19, 2022, 8:14pm UTC](https://forum.ansible.com/t/error-register-variable-the-task-includes-an-option-with-an-undefined-variable/36647/3 "2022-05-19T20:14:03Z")

</div>

I tried something a little smaller and it works properly:

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [May 20, 2022, 3:04pm UTC](https://forum.ansible.com/t/error-register-variable-the-task-includes-an-option-with-an-undefined-variable/36647/4 "2022-05-20T15:04:58Z")

</div>

A few observations inline:

> Hello, 30 minutes ago I am reviewing my playbook and I do not find the error, I am going crazy, i see this error:
> 
> I run the playbook this way
> 
> /usr/bin/ansible-playbook -i hosts --extra-vars "CARRIER=xx" tasks/create\_ec2\_stage.yaml
> 
> I see this error:
> 
> fatal: [localhost]: FAILED! =\> {"msg": "The task includes an option with an undefined variable. The error was: list object has no element 0\n\nThe e  
> rror appears to be in '/etc/ansible/loyalty/tasks/create\_ec2\_stage.yaml': line 63, column 7, but may\nbe elsewhere in the file depending on the exac  
> t syntax problem.\n\nThe offending line appears to be:\n\n register: ec2\_metadata\n - name: Parse \< JSON \>\n ^ here\n"}
> 
> My playbook:
> 
> ---  
> - name: New EC2 instances  
> &nbsp;&nbsp;hosts: localhost  
> &nbsp;&nbsp;gather\_facts: no  
> &nbsp;&nbsp;vars\_files:  
> &nbsp;&nbsp;&nbsp;&nbsp;- /etc/ansible/loyalty/vars/vars.yaml  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Run EC2 Instances  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amazon.aws.ec2\_instance:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "new-{{ CARRIER }}.test"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aws\_secret\_key: "{{ ec2\_secret\_key }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aws\_access\_key: "{{ ec2\_access\_key }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;region: us-east-1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key\_name: Kiu  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_type: t2.medium  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image\_id: xxxxxxxxxxxxx  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait\_timeout: 500

Here you explicitly decrease the wait\_timeout by 100 seconds (default = 600).  
This may cause the instance(s) to be not ready yet, which in turn  
might cause the error.  
Suggestion: either use the default (i.e. do not define wait\_timeout),  
or use a higher value - to increase the likelihood that all instances  
are in the state that you want them to be.  
While we're on that topic - you didn't define this desired state, so  
the default ('present') is used:  
[https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2\_instance\_module.html#parameter-state](https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_instance_module.html#parameter-state)  
If you spin up new instances, state 'present' means just that - it's  
there. But it might or might not have all of its network interfaces  
added and/or configured.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;volumes:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- device\_name: /dev/xvda  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ebs:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;volume\_type: gp3  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;volume\_size: 20  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete\_on\_termination: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vpc\_subnet\_id: xxxxxxxxxxxx  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;network:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assign\_public\_ip: no  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;security\_groups: ["xxxxxxxxxx", "xxxxxxxxxxx", "xxxxxxxxxxxxxx"]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enviroment: TEST  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count: 1  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Pause Few Seconds  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pause:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seconds: 20  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prompt: "Please wait"

Here you seem to work around the unstable 'present' state that you  
implicitly required in the previous task?

> &nbsp;&nbsp;&nbsp;&nbsp;- name: Get Information for EC2 Instances  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ec2\_instance\_info:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;region: us-east-1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filters:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"tag:Name": new-{{ CARRIER }}.test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: ec2\_metadata  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Parse JSON  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip\_addr: "{{ ec2\_metadata.instances[0].network\_interfaces[0].private\_ip\_address }}"
> 
> Any help, please?

1. Drop the entire pause task  
2. Drop the entire ec2\_instance\_info task  
3. Remove the 'wait\_timeout' parameter from the ec2\_instance task  
4. Add a state parameter with value 'running' to the ec2\_instance task  
5. Register the output of the ec2\_instance task in a variable (say  
'my\_instances')  
6. Add a debug task for the my\_instances variable. This should have  
the information you are looking for. Based on the exact structure of  
that you can select the required keys/indices (or use json\_query etc)

Dick
