Ansible - Print all security group ID's

Viewed 55

How can I print only the group_id when debugging? ec2_info.instances[0].security_groups has multiple group ID and I need all the group_id without the group_name.

  - name: Gather information about a particular instance using ID
    amazon.aws.ec2_instance_info:
      aws_access_key: "{{ aws_access_key }}"
      aws_secret_key: "{{ aws_secret_key }}"
      region: "ap-south-1"
      instance_ids:
        - i-08c7051b99bc69ba6
    register: ec2_info

  - name: Print EC2 Info
    debug:
       msg: "EC2 INFO: {{ ec2_info.instances[0].security_groups }}"

Output:

                "hosts": {
                    "localhost": {
                        "_ansible_no_log": false,
                        "_ansible_verbose_always": true,
                        "action": "debug",
                        "changed": false,
                        "msg": "EC2 INFO: [{'group_name': 'ManageIQ', 'group_id': 'sg-0500c5b20f7c152b4'}, {'group_name': 'launch-wizard-1', 'group_id': 'sg-062178ea5fabaf350'}]"
                    }
                },
1 Answers

use this playbook to loop over all group_id

  - name: Print EC2 Info
    debug:
       msg: "EC2 INFO: {{ item.group_id }}"
    loop: "{{ ec2_info.instances[0].security_groups }}"
Related