for loop with multiple variables output

pilchita

New Member
Joined
Dec 9, 2021
Messages
1
Reaction score
0
Credits
15
Hi,
I will try to explain my problem as best I can, my English is not good but I will try again.

I am creating a script to list snapshot from Amazon EC2 instances, I search through the Tag "Name" and use the following command.

Code:
VOLUMEN_ID=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$WILDCARD*" --output text | grep "EBS" | awk {'print $5'})

### output

vol-058db7c144eea4099
vol-0e3dda56a65fb08f4

In the output command I am showing the volumes of each instance.

Then I bring the information of the snapshot created:
Code:
<------>for i in $VOLUMEN_ID; do
<------>VOLUMEN_INFO=$(aws ec2 describe-snapshots --filters "Name=volume-id,Values=$i" | jq -r '.Snapshots[] | {SnapshotId, StartTime , VolumeId }'
<------>echo "$VOLUMEN_INFO"
done

# output

  "SnapshotId": "snap-0d6a5d763704cb67c",
  "StartTime": "2021-12-02T17:06:25.839000+00:00",
  "VolumeId": "vol-058db7c144eea4099"


  "SnapshotId": "snap-0e795e3fc9de3bbf5",
  "StartTime": "2021-12-02T16:47:57.755000+00:00",
  "VolumeId": "vol-058db7c144eea4099"

  "SnapshotId": "snap-0f83f47b0deb6ccdd",
  "StartTime": "2021-12-06T20:13:35.685000+00:00",
  "VolumeId": "vol-0e3dda56a65fb08f4"

My problem is that I need to add a description indicating the Tag Name of the instance to which the snapshot corresponds. In this way I bring the name of each instance:

Code:
TAG=$(aws ec2 describe-snapshots --filters "Name=tag:Name,Values=$WILCARD*" --output text | grep TAGS | awk {'print $3'} | uniq)

# output
INFRAESTRUCTURA
INFRA


The expected output is next but I don’t know how to do it.

Code:
Instances: INFRAESTRUCTURA
  "SnapshotId": "snap-0d6a5d763704cb67c",
  "StartTime": "2021-12-02T17:06:25.839000+00:00",
  "VolumeId": "vol-058db7c144eea4099"

Instances: INFRA
  "SnapshotId": "snap-0e795e3fc9de3bbf5",
  "StartTime": "2021-12-02T16:47:57.755000+00:00",
  "VolumeId": "vol-058db7c144eea4099"

  "SnapshotId": "snap-0f83f47b0deb6ccdd",
  "StartTime": "2021-12-06T20:13:35.685000+00:00",
  "VolumeId": "vol-0e3dda56a65fb08f4"

How should I iterate with loops to show it that way? Regards,
 

Members online


Top