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 in Ansible by Harsh
226 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 by Yesha

Related Questions In Ansible

0 votes
1 answer
0 votes
1 answer
+13 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 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
152 views
0 votes
1 answer
0 votes
1 answer