Home > Software engineering >  Ansible - create list out of with_items
Ansible - create list out of with_items

Time:07-20

How can I create a list out of with_items? I tried the following but it records only the last group ID instead of creating a list.

  - name: "Generate list"
    set_fact:
      my_new_list: "{{ [ item.group_id  ] }}" 
    with_items:
      - "{{ec2_info.instances[0].security_groups}}"

ec2_info.instances[0].security_groups has multiple group_id's.

                            "security_groups": [
                                {
                                    "group_id": "sg-0500c5b20f7c152b4",
                                    "group_name": "ManageIQ"
                                },
                                {
                                    "group_id": "sg-062178ea5fabaf350",
                                    "group_name": "launch-wizard-1"
                                }
                            ],

CodePudding user response:

this playbook traps all the list:

 - name: "Generate list"
    set_fact:
      my_new_list: "{{ my_new_list|d([])   [ item.group_id  ] }}" 
    with_items:
      - "{{ec2_info.instances[0].security_groups}}"
  • Related