The equivalence of launch_block_device_mappings(aws) but for azure - amazon-web-services

I am using packer to build ami. I have the AWS one as follows:
{
"name": "ami",
"type": "amazon-ebs",
"region": "us-west-1",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"ami_users": "{{user `ami_account_ids`}}",
"ami_regions": "{{user `ami_regions`}}",
"source_ami": "{{user `source_ami`}}",
"instance_type": "t2.2xlarge",
"ssh_username": "ubuntu",
"ami_name": "{{user `tag`}}-{{timestamp}}",
"shutdown_behavior": "stop",
"launch_block_device_mappings": [
{
"device_name": "/dev/sda1",
"volume_size": 30,
"volume_type": "gp2",
"delete_on_termination": true
},
{
"device_name": "/dev/sdb",
"volume_size": 50,
"volume_type": "gp2",
"delete_on_termination": true
},
{
"device_name": "/dev/sdc",
"volume_size": 30,
"volume_type": "gp2",
"delete_on_termination": true
}
]
}
I am trying to recreate the above specification but for azure and am stuck on the disk/block device part. I am trying to find the equivalence of launch_block_device_mappings but for azure. The current code of azure is as follows:
{
"name": "azure",
"type": "azure-arm",
"client_id": "{{user `azure_client_id`}}",
"client_secret": "{{user `azure_client_secret`}}",
"tenant_id": "{{user `azure_tenant_id`}}",
"subscription_id": "{{user `azure_subscription_id`}}",
"managed_image_resource_group_name": "images",
"managed_image_name": "{{user `tag`}}-{{timestamp}}",
"ssh_username": "ubuntu",
"ssh_private_key_file": "~/.ssh/id_rsa",
"os_type": "Linux",
"os_disk_size_gb": 30,
"disk_additional_size": [50, 30],
"image_publisher": "Canonical",
"image_offer": "UbuntuServer",
"image_sku": "18.04-LTS",
"location": "{{user `azure_region`}}",
"vm_size": "Standard_D4s_v3"
}

Related

How to upgrade AWS EC2 from t2.2xlarge to t3.2xlarge?

I have an t2.2xlarge AWS EC2 instance that i need to change it's type to t3.2xlarge.
But when i try to start it i get an
"Error starting instances The requested configuration is currently not
supported. Please check the documentation for supported
configurations."
When i run the check script everything is fine
https://github.com/awslabs/aws-support-tools/tree/master/EC2/NitroInstanceChecks
OK NVMe Module is installed and available on your instance
OK ENA Module with version is installed and available on your instance
OK fstab file looks fine and does not contain any device names.
And i also did all the checks described here
https://aws.amazon.com/premiumsupport/knowledge-center/boot-error-linux-nitro-instance/
aws ec2 describe-instances --instance-ids my-instance-id --query "Reservations[].Instances[].EnaSupport"
[
true
]
Is there anything else i should change to be able to start it as t3.2xlarge?
To reproduce:
Create an t2.2xlarge instance with default settings
Stop it and change type to t3.2xlarge
Try to start it
More detailed info about instance
aws ec2 describe-instances
{
"Reservations": [
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-***********",
"InstanceId": "i-***********",
"InstanceType": "t2.2xlarge",
"KeyName": "***********",
"LaunchTime": "2020-11-24T06:11:41+00:00",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "us-east-1e",
"GroupName": "",
"Tenancy": "default"
},
"PrivateDnsName": "ip-***********.ec2.internal",
"PrivateIpAddress": "***********",
"ProductCodes": [],
"PublicDnsName": "ec2-***********.compute-1.amazonaws.com",
"PublicIpAddress": "***********",
"State": {
"Code": 16,
"Name": "running"
},
"StateTransitionReason": "",
"SubnetId": "subnet-***********",
"VpcId": "vpc-***********",
"Architecture": "x86_64",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {
"AttachTime": "2020-10-06T05:07:35+00:00",
"DeleteOnTermination": true,
"Status": "attached",
"VolumeId": "vol-***********"
}
}
],
"ClientToken": "",
"EbsOptimized": false,
"EnaSupport": true,
"Hypervisor": "xen",
"NetworkInterfaces": [
{
"Association": {
"IpOwnerId": "amazon",
"PublicDnsName": "***********.compute-1.amazonaws.com",
"PublicIp": "***********"
},
"Attachment": {
"AttachTime": "2020-10-06T05:07:34+00:00",
"AttachmentId": "eni-attach-***********",
"DeleteOnTermination": true,
"DeviceIndex": 0,
"Status": "attached",
"NetworkCardIndex": 0
},
"Description": "",
"Groups": [
{
"GroupName": "launch-wizard-1",
"GroupId": "sg-***********"
}
],
"Ipv6Addresses": [],
"MacAddress": "***********",
"NetworkInterfaceId": "eni-***********",
"OwnerId": "***********",
"PrivateDnsName": "ip-***********.ec2.internal",
"PrivateIpAddress": "***********",
"PrivateIpAddresses": [
{
"Association": {
"IpOwnerId": "amazon",
"PublicDnsName": "ec2-***********.compute-1.amazonaws.com",
"PublicIp": "***********"
},
"Primary": true,
"PrivateDnsName": "ip-***********.ec2.internal",
"PrivateIpAddress": "***********"
}
],
"SourceDestCheck": true,
"Status": "in-use",
"SubnetId": "subnet-***********",
"VpcId": "vpc-***********",
"InterfaceType": "interface"
}
],
"RootDeviceName": "/dev/sda1",
"RootDeviceType": "ebs",
"SecurityGroups": [
{
"GroupName": "launch-wizard-1",
"GroupId": "sg-***********"
}
],
"SourceDestCheck": true,
"Tags": [
{
"Key": "Name",
"Value": ""
}
],
"VirtualizationType": "hvm",
"CpuOptions": {
"CoreCount": 8,
"ThreadsPerCore": 1
},
"CapacityReservationSpecification": {
"CapacityReservationPreference": "open"
},
"HibernationOptions": {
"Configured": false
},
"MetadataOptions": {
"State": "applied",
"HttpTokens": "optional",
"HttpPutResponseHopLimit": 1,
"HttpEndpoint": "enabled"
},
"EnclaveOptions": {
"Enabled": false
}
}
],
"OwnerId": "***********",
"ReservationId": "r-***********"
}
]
}
I tried to launch a t3.2xlarge in us-east-1e and got the following error:
Your requested instance type (t3.2xlarge) is not supported in your requested Availability Zone (us-east-1e). Please retry your request by not specifying an Availability Zone or choosing us-east-1a, us-east-1b, us-east-1c, us-east-1d, us-east-1f.
AWS probably doesn't have t3.2xlarge instances available in this AZ.

Creating a JSON table from 'aws autoscaling describe-auto-scaling-groups'

I have a problem with parsing output/transforming it from aws autoscaling describe-auto-scaling-groups
The output looks like this:
{
"AutoScalingGroups": [
{
"AutoScalingGroupName": "eks-nodegroup-AZ1",
"AutoScalingGroupARN": "arn:aws:autoscaling:eu-central-1::autoScalingGroup:854a8f05-cd3c-421d-abf3-0f3730d0b068:autoScalingGroupName/eks-nodegroup-AZ1",
"LaunchTemplate": {
"LaunchTemplateId": "lt-XXXXXXXXXXXXX",
"LaunchTemplateName": "eks-nodegroup-AZ1",
"Version": "$Latest"
},
"MinSize": 1,
"MaxSize": 6,
"DesiredCapacity": 1,
"DefaultCooldown": 300,
"AvailabilityZones": [
"eu-central-1a"
],
"LoadBalancerNames": [],
"TargetGroupARNs": [],
"HealthCheckType": "EC2",
"HealthCheckGracePeriod": 300,
"Instances": [
{
"InstanceId": "i-XXXXXXXXXXXXXXX",
"AvailabilityZone": "eu-central-1a",
"LifecycleState": "InService",
"HealthStatus": "Healthy",
"LaunchTemplate": {
"LaunchTemplateId": "lt-XXXXXXXXXXXXX",
"LaunchTemplateName": "eks-nodegroup-AZ1",
"Version": "1"
},
"ProtectedFromScaleIn": false
}
],
"CreatedTime": "2019-09-24T17:24:57.805Z",
"SuspendedProcesses": [],
"VPCZoneIdentifier": "subnet-XXXXXXXXXXXX",
"EnabledMetrics": [],
"Tags": [
{
"ResourceId": "eks-nodegroup-AZ1",
"ResourceType": "auto-scaling-group",
"Key": "Name",
"Value": "eks-nodegroup-AZ1",
"PropagateAtLaunch": true
},
{
"ResourceId": "eks-nodegroup-AZ1",
"ResourceType": "auto-scaling-group",
"Key": "k8s.io/cluster-autoscaler/enabled",
"Value": "true",
"PropagateAtLaunch": true
},
{
"ResourceId": "eks-nodegroup-AZ1",
"ResourceType": "auto-scaling-group",
"Key": "k8s.io/cluster-autoscaler/k8s-team-sandbox",
"Value": "true",
"PropagateAtLaunch": true
},
{
"ResourceId": "eks-nodegroup-AZ1",
"ResourceType": "auto-scaling-group",
"Key": "kubernetes.io/cluster/k8s-team-sandbox",
"Value": "owned",
"PropagateAtLaunch": true
}
],
"TerminationPolicies": [
"Default"
],
"NewInstancesProtectedFromScaleIn": false,
"ServiceLinkedRoleARN": "arn:aws:iam::XXXXXXXXXXXXXXX:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
},
{
"AutoScalingGroupName": "eks-k8s-team-sandbox-AZ2",
"AutoScalingGroupARN": "arn:aws:autoscaling:eu-central-1::autoScalingGroup:25324f3a-b911-453c-b316-46657e850b19:autoScalingGroupName/eks-nodegroup-AZ2",
"LaunchTemplate": {
"LaunchTemplateId": "lt-XXXXXXXXXXXX",
"LaunchTemplateName": "eks-nodegroup-AZ2",
"Version": "$Latest"
},
"MinSize": 1,
"MaxSize": 6,
"DesiredCapacity": 1,
"DefaultCooldown": 300,
"AvailabilityZones": [
"eu-central-1b"
],
"LoadBalancerNames": [],
"TargetGroupARNs": [],
"HealthCheckType": "EC2",
"HealthCheckGracePeriod": 300,
"Instances": [
{
"InstanceId": "i-XXXXXXXXXXXX",
"AvailabilityZone": "eu-central-1b",
"LifecycleState": "InService",
"HealthStatus": "Healthy",
"LaunchTemplate": {
"LaunchTemplateId": "lt-XXXXXXXXXX",
"LaunchTemplateName": "eks-nodegroup-AZ2",
"Version": "1"
},
"ProtectedFromScaleIn": false
}
],
"CreatedTime": "2019-09-24T17:24:57.982Z",
"SuspendedProcesses": [],
"VPCZoneIdentifier": "subnet-XXXXXXXX",
"EnabledMetrics": [],
"Tags": [
{
"ResourceId": "eks-nodegroup-AZ2",
"ResourceType": "auto-scaling-group",
"Key": "Name",
"Value": "eks-nodegroup-AZ2",
"PropagateAtLaunch": true
},
{
"ResourceId": "eks-nodegroup-AZ2",
"ResourceType": "auto-scaling-group",
"Key": "k8s.io/cluster-autoscaler/enabled",
"Value": "true",
"PropagateAtLaunch": true
},
{
"ResourceId": "eks-nodegroup-AZ2",
"ResourceType": "auto-scaling-group",
"Key": "k8s.io/cluster-autoscaler/k8s-team-sandbox",
"Value": "true",
"PropagateAtLaunch": true
},
{
"ResourceId": "eks-nodegroup-AZ2",
"ResourceType": "auto-scaling-group",
"Key": "kubernetes.io/cluster/k8s-team-sandbox",
"Value": "owned",
"PropagateAtLaunch": true
}
],
"TerminationPolicies": [
"Default"
],
"NewInstancesProtectedFromScaleIn": false,
"ServiceLinkedRoleARN": "arn:aws:iam::ARN:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
}
]
}
I need to parse it to get :
{
"eks-nodegroup-AZ1" : "$DesiredCapacityForEks-nodegroup-AZ1",
"eks-nodegroup-AZ2" : "$DesiredCapacityForEks-nodegroup-AZ2",
"eks-nodegroup-AZ3" : "$DesiredCapacityForEks-nodegroup-AZ3",
"eks-nodegroup-AZX" : "$DesiredCapacityForEks-nodegroup-AZX",
}
The following expected output will be used for external data resource for terraform to be able to automate DesiredCapacity value during the ASG rolling-updates.
Thanks,
Dominik
Try with your response,
response | jq '.AutoScalingGroups[] | {(.AutoScalingGroupName): .DesiredCapacity}' | jq -s add

Get AWS machine type (ubuntu/nixos/centos, etc) [duplicate]

This question already has answers here:
How to find OS of an EC2 instance using AWS CLI
(6 answers)
Closed 3 years ago.
Using an AWS cli command like so:
aws ec2 describe-instances
is there a way to get information about what kind of machine (or the AMI used) for each instance? This is the info I see for instance using describe-instances:
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "ecxxx-xxx-xxx-xxx-238.us-xxxt-2.compute.amazonaws.com",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "20xx-x9-21T22:59:00.000Z",
"PublicIpAddress": "xxx.xxx.xxx.xxx",
"PrivateIpAddress": "xxx.xxx.xxx.xxx",
"ProductCodes": [],
"VpcId": "vpx-xx338d07",
"CpuOptions": {
"CoreCount": 2,
"ThreadsPerCore": 1
},
"StateTransitionReason": "",
"InstanceId": "i-xxxxf9762a4140a88",
"EnaSupport": true,
"ImageId": "amx-xx32ec5bc225539f5",
"PrivateDnsName": "ip-xxx-xxx-xxx-xxx.us-xxxt-2.compute.internal",
"KeyName": "ntrs-prod",
"SecurityGroups": [
{
"GroupName": "public web server",
"GroupId": "sg-xxxac430acd613381"
}
],
"ClientToken": "",
"SubnetId": "suxxxx-cc1c0e84",
"InstanceType": "t2.medium",
"CapacityReservationSpecification": {
"CapacityReservationPreference": "open"
},
"NetworkInterfaces": [
{
"Status": "in-use",
"MacAddress": "0z:5O:8z:O5:3o:9z",
"SourceDestCheck": true,
"VpcId": "vpx-xx338d07",
"Description": "",
"NetworkInterfaceId": "enx-xxbd4b3bd8f339aaf",
"PrivateIpAddresses": [
{
"PrivateDnsName": "ip-xxx-xxx-xxx-xxx.us-xxxt-2.compute.internal",
"PrivateIpAddress": "xxx.xxx.xxx.xxx",
"Primary": true,
"Association": {
"PublicIp": "xxx.xxx.xxx.xxx",
"PublicDnsName": "ecxxx-xxx-xxx-xxx-238.us-xxxt-2.compute.amazonaws.com",
"IpOwnerId": "amazon"
}
}
],
"PrivateDnsName": "ip-xxx-xxx-xxx-xxx.us-xxxt-2.compute.internal",
"Attachment": {
"Status": "attached",
"DeviceIndex": 0,
"DeleteOnTermination": true,
"AttachmentId": "enx-xxtach-01948e0bd9059a635",
"AttachTime": "20xx-x9-21T22:59:00.000Z"
},
"Groups": [
{
"GroupName": "public web server",
"GroupId": "sg-xxxac430acd613381"
}
],
"Ipxxxxdresses": [],
"OwnerId": "91xxxx820371",
"PrivateIpAddress": "xxx.xxx.xxx.xxx",
"SubnetId": "suxxxx-cc1c0e84",
"Association": {
"PublicIp": "xxx.xxx.xxx.xxx",
"PublicDnsName": "ecxxx-xxx-xxx-xxx-238.us-xxxt-2.compute.amazonaws.com",
"IpOwnerId": "amazon"
}
}
],
"SourceDestCheck": true,
"Placement": {
"Tenancy": "default",
"GroupName": "",
"AvailabilityZone": "us-xxxt-2a"
},
"Hypervisor": "xen",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vox-xxda51640e0c4ac9a",
"AttachTime": "20xx-x9-21T22:59:01.000Z"
}
}
],
"Architecture": "x86_64",
"RootDeviceType": "ebs",
"RootDeviceName": "/dev/sda1",
"VirtualizationType": "hvm",
"Tags": [
{
"Value": "manu-demo",
"Key": "Name"
},
{
"Value": "Web Server",
"Key": "Type"
}
],
"HibernationOptions": {
"Configured": false
},
"AmiLaunchIndex": 0
}
It's right there in the describe-images output under the field ImageId.
To get a list of instances and their images (i.e. AMIs), you can use the query option to list only those attributes:
$ aws ec2 describe-instances --query "Reservations[*].Instances[*].[InstanceId, ImageId]"

Ansible Tower is not registering variable correctly in ec2-remote-facts module

The ec2-remote-facts module works correctly when I do not run it on Ansible Tower. The first example below (not using Tower) includes all of the block_device_mapping information that I use in subsequent tasks.
This is a big issue I were to use Tower in the long run. My code is the same for both examples. Any thoughts that could lead me in the right direction.
My only thought is that since it is not a core module, Ansible Tower is not synced to the module's most recent code perfectly. But I am baffled. Thanks!
Ansible Version - ansible 2.2.0.0 (running on Ubuntu)
Ansible Tower Version - Tower Version 3.0.3 (running on Centos)
---examples below----
-Ansible (not using Tower)-
ok: [localhost -> localhost] => {
"changed": false,
"instances": [
{
"ami_launch_index": "0",
"architecture": "x86_64",
"block_device_mapping": [
{
"attach_time": "2017-01-13T17:05:31.000Z",
"delete_on_termination": false,
"device_name": "/dev/sdb",
"status": "attached",
"volume_id": "vol-132312313212313"
},
{
"attach_time": "2017-01-13T17:05:31.000Z",
"delete_on_termination": true,
"device_name": "/dev/sda1",
"status": "attached",
"volume_id": "vol-123123123123"
},
{
"attach_time": "2017-01-13T17:05:31.000Z",
"delete_on_termination": false,
"device_name": "/dev/sdc",
"status": "attached",
"volume_id": "vol-123123123123"
}
],
"client_token": "",
"ebs_optimized": false,
"groups": [
{
"id": "sg-12312313",
"name": "n123123123
}
],
"hypervisor": "xen",
"id": "i-123123123123",
"image_id": "ami-123123123123",
"instance_profile": null,
"interfaces": [
{
"id": "eni-123123123",
"mac_address": "123123123"
}
],
"kernel": null,
"key_name": "my-v123123",
"launch_time": "2017-01-13T17:05:30.000Z",
"monitoring_state": "disabled",
"persistent": false,
"placement": {
"tenancy": "default",
"zone": "us-east-1b"
},
"private_dns_name": "ip-112312312",
"private_ip_address": "10.1.1.4",
"public_dns_name": "",
"public_ip_address": null,
"ramdisk": null,
"region": "us-east-1",
"requester_id": null,
"root_device_type": "ebs",
"source_destination_check": "true",
"spot_instance_request_id": null,
"state": "running",
"tags": {
"CurrentIP": "10.1.1.1.4",
"Name": "d1",
"Type": "d2"
},
"virtualization_type": "hvm",
"vpc_id": "vpc-123123123"
},
Ansible Tower (notice that its missing the block_device_mapping block of code)
TASK [debug] **********************
ok: [localhost] => {
"db_id.instances": [
{
"ami_launch_index": "0",
"architecture": "x86_64",
"client_token": "",
"ebs_optimized": false,
"groups": [
{
"id": "sg-123123",
"name": "n123123123"
}
],
"hypervisor": "xen",
"id": "i-123123123",
"image_id": "ami-123123",
"instance_profile": null,
"interfaces": [
{
"id": "eni-123123123",
"mac_address": "123123123"
}
],
"kernel": null,
"key_name": "m123123",
"launch_time": "2017-01-13T17:05:30.000Z",
"monitoring_state": "disabled",
"persistent": false,
"placement": {
"tenancy": "default",
"zone": "us-east-1b"
},
"private_dns_name": "ip-1123123123123",
"private_ip_address": "10.1.1.4",
"public_dns_name": "",
"ramdisk": null,
"region": "us-east-1",
"requester_id": null,
"root_device_type": "ebs",
"source_destination_check": "true",
"spot_instance_request_id": null,
"state": "running",
"tags": {
"Name": "123123",
"Type": "123123"
},
"virtualization_type": "hvm",
"vpc_id": "vpc-123123123"
},
I guess you indeed have old Ansible version on your Tower box.
As of today, official Ansible Tower Vagrant box (ansible/tower (virtualbox, 3.0.3)) has ver 2.1.2 inside:
[vagrant#ansible-tower ~]$ ansible --version
ansible 2.1.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
And ec2_remote_facts has no block_device_mapping in this version.
So update Ansible on your Tower box or apply this patch.

Sort EC2 Instances by Tag Name

So I'm using the aws cli to retrieve a list of instances in addition to the jq tool. I do so with the following command.
aws ec2 describe-instances | jq '.Reservations[].Instances[]'
With the following example json response.
{
"Reservations": [
{
"OwnerId": "1234",
"ReservationId": "r-124991k",
"Groups": [],
"Instances": [
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "2015-07-31T16:48:01.000Z",
"PrivateIpAddress": "11.123.104.123",
"ProductCodes": [],
"VpcId": "vpc-sdfsdf9109",
"StateTransitionReason": "",
"InstanceId": "i-1223421",
"ImageId": "ami-aklasd131",
"PrivateDnsName": "private.dns.name",
"KeyName": "testkey",
"SecurityGroups": [
{
"GroupName": "secur-grou",
"GroupId": "sg-a3d234a"
}
],
"ClientToken": "",
"SubnetId": "subnet-57absfsdf",
"InstanceType": "m3.large",
"NetworkInterfaces": [
{
"Status": "in-use",
"MacAddress": "12:c3:f2:5c:5f:4d",
"SourceDestCheck": false,
"VpcId": "vpc-sdfsdf9109",
"Description": "",
"NetworkInterfaceId": "eni-81b1234",
"PrivateIpAddresses": [
{
"Primary": true,
"PrivateIpAddress": "11.123.104.123"
}
],
"Attachment": {
"Status": "attached",
"DeviceIndex": 0,
"DeleteOnTermination": true,
"AttachmentId": "eni-attach-5asdf234",
"AttachTime": "2015-07-30T16:10:39.000Z"
},
"Groups": [
{
"GroupName": "secur-grou",
"GroupId": "sg-a3d234a"
}
],
"SubnetId": "subnet-57absfsdf",
"OwnerId": "1234",
"PrivateIpAddress": "11.123.104.123"
}
],
"SourceDestCheck": false,
"Placement": {
"Tenancy": "default",
"GroupName": "",
"AvailabilityZone": "us-east-1c"
},
"Hypervisor": "xen",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-28c882",
"AttachTime": "2015-07-30T16:10:43.000Z"
}
},
{
"DeviceName": "/dev/sdb",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-3f1d5",
"AttachTime": "2015-07-30T16:10:43.000Z"
}
},
{
"DeviceName": "/dev/sdg",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-81c8b",
"AttachTime": "2015-07-30T16:10:43.000Z"
}
}
],
"Architecture": "x86_64",
"RootDeviceType": "ebs",
"RootDeviceName": "/dev/sda1",
"VirtualizationType": "hvm",
"Tags": [
{
"Value": "apple",
"Key": "Name"
},
{
"Value": "tag",
"Key": "extra"
}
],
"AmiLaunchIndex": 0
}
]
},
{
"OwnerId": "1234",
"ReservationId": "r-2a72342",
"Groups": [],
"Instances": [
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "2015-07-31T16:48:01.000Z",
"PrivateIpAddress": "11.123.104.83",
"ProductCodes": [],
"VpcId": "vpc-sdfsdf9109",
"StateTransitionReason": "",
"InstanceId": "i-f9271451",
"ImageId": "ami-aklasd131",
"PrivateDnsName": "private.name.here",
"KeyName": "testkey",
"SecurityGroups": [
{
"GroupName": "secur-grou",
"GroupId": "sg-a3d234a"
}
],
"ClientToken": "",
"SubnetId": "subnet-5llqjwdf",
"InstanceType": "m3.large",
"NetworkInterfaces": [
{
"Status": "in-use",
"MacAddress": "12:ea:06:8b:ff:86",
"SourceDestCheck": false,
"VpcId": "vpc-sdfsdf9109",
"Description": "",
"NetworkInterfaceId": "eni-82kjs91",
"PrivateIpAddresses": [
{
"Primary": true,
"PrivateIpAddress": "11.123.104.13"
}
],
"Attachment": {
"Status": "attached",
"DeviceIndex": 0,
"DeleteOnTermination": true,
"AttachmentId": "eni-attach-35312341",
"AttachTime": "2015-07-30T17:29:00.000Z"
},
"Groups": [
{
"GroupName": "secur-grou",
"GroupId": "sg-a3d234a"
}
],
"SubnetId": "subnet-57absfsdf",
"OwnerId": "1234",
"PrivateIpAddress": "11.123.104.83"
}
],
"SourceDestCheck": false,
"Placement": {
"Tenancy": "default",
"GroupName": "",
"AvailabilityZone": "us-east-1c"
},
"Hypervisor": "xen",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-ee1c21",
"AttachTime": "2015-07-30T17:29:02.000Z"
}
},
{
"DeviceName": "/dev/sdb",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-13awer",
"AttachTime": "2015-07-30T17:29:02.000Z"
}
},
{
"DeviceName": "/dev/sdg",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-3111",
"AttachTime": "2015-07-30T17:29:02.000Z"
}
}
],
"Architecture": "x86_64",
"RootDeviceType": "ebs",
"RootDeviceName": "/dev/sda1",
"VirtualizationType": "hvm",
"Tags": [
{
"Value": "banana",
"Key": "Name"
},
{
"Value": "tag",
"Key": "extra"
}
],
"AmiLaunchIndex": 0
}
]
}
]
}
But for the instances that are returned I would like them to be sorted by their Tag Name. I've tried the following.
jq '.Reservations[]|=sort_by(.Instances[].Tags[.Key="Name"].Value)'
But jq complains with several error messages. Is there an easy way to sort the json by Name tag and then get the PrivateDNS entry in that sorted order?
Okay, I'm not sure if I understood what you want. This is what I think you are asking for; please correct me if I'm wrong:
In each Reservation, I want the Instances inside to be sorted by the Value of the Tag whose Key is "Name"
And this is what would solve that problem:
jq '.Reservations[].Instances |= sort_by(.Tags[] | select(.Key == "Name").Value)'
Alternatively, you may be asking for this:
I want the Reservations to be sorted by the Value of the Tag whose Key is "Name" inside the Instances.
Which would be solved by this:
jq '.Reservations |= sort_by(.Instances[].Tags[] | select(.Key == "Name").Value)'
(The behavior of this script is probably meaningless if there are several Instances in a Reservation)
As of getting the private DNS values out, just select those with something like:
.Reservations[].Instances[].PrivateDnsName
You can also pipe that after the sorting script.