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"
}
On Thu, May 19, 2022 at 4:40 PM Matt Martz <[email protected]> wrote:
> 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.
>
> On Thu, May 19, 2022 at 2:32 PM SysAdmin EM <[email protected]> wrote:
>
>> 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,
>>
>> --
>> 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 [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/CAGUDtnk196JTg8S0-xECGT8eKF%2BLv9QmNgPzW3c6ZUhUFPKXXg%40mail.gmail.com
>> <https://groups.google.com/d/msgid/ansible-project/CAGUDtnk196JTg8S0-xECGT8eKF%2BLv9QmNgPzW3c6ZUhUFPKXXg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>
>
> --
> Matt Martz
> @sivel
> sivel.net
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAD8N0v8tfp5zbmwSPjVs3qKzKc3X94J94rhK3DzsL%3DNWOiPCzA%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAD8N0v8tfp5zbmwSPjVs3qKzKc3X94J94rhK3DzsL%3DNWOiPCzA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAGUDtnkEkTA1y0RMzCuvBzcb8RhD9E6E9Oc9E-k9cgsQh%3DiNuA%40mail.gmail.com.