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

3,884 views
Skip to first unread message

SysAdmin EM

unread,
May 19, 2022, 3:32:32 PM5/19/22
to ansible...@googlegroups.com
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
        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"
    - 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?

Regards,

Matt Martz

unread,
May 19, 2022, 3:40:54 PM5/19/22
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGUDtnk196JTg8S0-xECGT8eKF%2BLv9QmNgPzW3c6ZUhUFPKXXg%40mail.gmail.com.


--
Matt Martz
@sivel
sivel.net

SysAdmin EM

unread,
May 19, 2022, 4:14:26 PM5/19/22
to ansible...@googlegroups.com
I tried something a little smaller and it works properly:

---
- name: New EC2 Instances

  hosts: localhost
  gather_facts: no
  vars_files:
    - /etc/ansible/loyalty/vars/vars.yaml
  vars:
    pwd_alias: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
    CARRIER_UPPERCASE: "{{ CARRIER | upper }}"
  tasks:
    - set_fact:
       MY_PASS: "{{ pwd_alias }}"
    - name: Get EC2 info
      ec2_instance_info: 
        region: us-east-1
        filters:
          "tag:Name": new-{{ CARRIER }}.stage
      register: ec2_metadata
    - name: Parsing JSON
      set_fact: 
        ip_addr: "{{ ec2_metadata.instances[0].network_interfaces[0].private_ip_address }}"
    - name: Show Result
      debug:
        msg: "{{ ip_addr }}"

Why is this example working for me and the other one not? I’m a little lost, someone could help me?

TASK [Show Result] *********************************************************************************************************************
ok: [localhost] => {
    "msg": "172.31.x.x"
}

Dick Visser

unread,
May 20, 2022, 11:05:28 AM5/20/22
to ansible...@googlegroups.com
A few observations inline:
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
Reply all
Reply to author
Forward
0 new messages