Error the field args has an invalid value which appears to include a variable that is undefined - Ansible

0 votes

I have a playbook that gets user's ec2_asg_facts and gets everything about a particular AWS auto scaling group. I also want to fetch the IP address of the instance. When I execute the following playbook, I get an error

---
- name: Create a new Demo EC2 instance
  hosts: localhost
  connection: local
  gather_facts: False

  tasks:

    - name: Find ASG in AWS
      ec2_asg_facts:
         aws_access_key: ------------------------
         aws_secret_key: ----------------------
         region: us-east-1
      register: auto_scaling_group
      register: ec2_asg_facts_results
    - name: Create list of instance_ids
      set_fact:
       ec2_asg_instance_ids: "{{ ec2_asg_facts_results.results[0].instances | map(attribute='instance_id') | list }}"
    - name: EC2 facts
      ec2_remote_facts:
       region: us-east-1
       aws_access_key: ---------------------------------
       aws_secret_key: ---------------------------------
       filters:
           instance-id: "{{ ec2_asg_instance_ids.instances[0].interfaces | map(attribute='id') }}"


**ERROR**
TASK [EC2 facts] ********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'list object' has no attribute 'instances'\n\nThe error appears to have been in '/root/ec2-asg.yml': line 25, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n       ec2_asg_instance_ids: \"{{ ec2_asg_facts_results.results[0].instances | map(attribute='instance_id') | list }}\"\n    - name: EC2 facts\n 
Apr 2, 2019 in Ansible by Harsh
3,744 views

1 answer to this question.

0 votes

Hey @Harsh, use with_items to access the result of the registered variable and then get the first item of "asg_instance_ids" when accessing it later:

 - name: Create list of instance_ids
  set_fact:
   ec2_asg_instance_ids: "{{ item.instances | map(attribute='instance_id') | list }}"
  with_items: "{{ ec2_asg_facts_results.results }}"
answered Apr 2, 2019 by Yesha

Related Questions In Ansible

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Error saying "ERROR: ec2_vpc_subnet is not a legal parameter of an Ansible Play"

ec2_vpc_subnet is support from Ansible version 2.0 and ...READ MORE

answered Feb 8, 2019 in Ansible by Dee
1,107 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,036 views
+2 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do I get an ENV variable and store it into a local variable - Ansible

Hey @Ninja, that is possible. What you ...READ MORE

answered Jan 24, 2019 in Ansible by Thomas
2,596 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP