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

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:

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.

I tried something a little smaller and it works properly:

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
  hosts: localhost
  gather_facts: no
  vars_files:
    - /etc/ansible/loyalty/vars/vars.yaml
  tasks:
    - name: Run EC2 Instances
      amazon.aws.ec2_instance:
        name: "new-{{ CARRIER }}.test"
        aws_secret_key: "{{ ec2_secret_key }}"
        aws_access_key: "{{ ec2_access_key }}"
        region: us-east-1
        key_name: Kiu
        instance_type: t2.medium
        image_id: xxxxxxxxxxxxx
        wait: yes
        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
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.

        volumes:
          - device_name: /dev/xvda
            ebs:
              volume_type: gp3
              volume_size: 20
              delete_on_termination: yes
        vpc_subnet_id: xxxxxxxxxxxx
        network:
          assign_public_ip: no
        security_groups: ["xxxxxxxxxx", "xxxxxxxxxxx", "xxxxxxxxxxxxxx"]
        tags:
          Enviroment: TEST
        count: 1
    - name: Pause Few Seconds
      pause:
        seconds: 20
        prompt: "Please wait"

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

    - name: Get Information for EC2 Instances
      ec2_instance_info:
        region: us-east-1
        filters:
          "tag:Name": new-{{ CARRIER }}.test
      register: ec2_metadata
    - name: Parse JSON
      set_fact:
        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