Comment 1 for bug 1724626

Revision history for this message
Chad Smith (chad.smith) wrote :

Minimally this is resolved as of cloud-init version 21.4 which surfaced a /run/cloud-init/cloud-id file which represents the unique cloud datasource type cloud-init detects:

cat /run/cloud-init/cloud-id will have the following values:
aws
azure
gce
openstack
lxd
nocloud

Note as well that cloud-init creates a symlink pointing to this file of the format:
   /run/cloud-init/cloud-id-<cloud_name>

This allows scripts to easily check presence of a cloud-specific file /run/cloud-init/cloud-id-aws to indicate what platform cloud-init detected.

Note that this value will be incorrect if someone installs NoCloud datasource seed files on an Ec2 vm in /var/lib/cloud/seed/nocloud-net/(user-data|meta-data) because cloud-init will detect the NoCloud datasource before Ec2 datasource in that case.

Additional sources of flat files that can provide more context for identity of the system/platform

would be /run/cloud-init/instance-data.json which is structured configuration attributes that cloud-init reads from the datasource it detects.

instance-data.json provide versioning of generalized config variables under the v1 key of instance-data.json

Here are some examples on various clouds (again note that any cloud could have a NoCloud seed file placed in the environment that would prevent cloud-init from detecting the underlying cloud platform config on those systems):

https://hackmd.io/aAJFerDRRNaoL0Q_vDRYBg

Keep in mind as well there are additional pieces of data that are cloud-specific which give you more details about the offering, instance size etc under a top-level key in instance-data.json "ds".

On EC2 you get something like the following under 'ds'

{
 "_doc": "EXPERIMENTAL: The structure and format of content scoped under the 'ds' key may change in subsequent releases of cloud-init.",
 "_metadata_api_version": "2021-03-23",
 "dynamic": {
  "instance-identity": {
   "document": {
    "accountId": "329910648999",
    "architecture": "x86_64",
    "availabilityZone": "us-east-2b",
    "billingProducts": null,
    "devpayProductCodes": null,
    "imageId": "ami-0283a57753b18025b",
    "instanceId": "i-0ecd5e14298b43fbf",
    "instanceType": "t2.micro",
    "kernelId": null,
    "marketplaceProductCodes": null,
    "pendingTime": "2022-12-16T21:13:36Z",
    "privateIp": "172.31.24.213",
    "ramdiskId": null,
    "region": "us-east-2",
    "version": "2017-09-30"
   },
   "pkcs7": [
    "MIAGCSqGSIb3DQEHAqCAMI..."
   ],
   "rsa2048": [
    "MIAGCS..."
   ],
   "signature": [
    "Qk99HHJxS..."
   ]
  }
 },
 "meta-data": {
  "ami-id": "ami-0283a57753b18025b",
  "ami-launch-index": "0",
  "ami-manifest-path": "(unknown)",
  "block-device-mapping": {
   "ami": "/dev/sda1",
   "ephemeral0": "sdb",
   "ephemeral1": "sdc",
   "root": "/dev/sda1"
  },
  "events": {
   "maintenance": {
    "history": "[]",
    "scheduled": "[]"
   }
  },
  "hostname": "ip-172-31-24-213.us-east-2.compute.internal",
  "identity-credentials": {
   "ec2": {
    "info": {
     "AccountId": "32991064...",
     "Code": "Success",
     "LastUpdated": "2022-12-16T21:13:57Z"
    }
   }
  },
  "instance-action": "none",
  "instance-id": "i-0ecd5e14298b43fbf",
  "instance-life-cycle": "on-demand",
  "instance-type": "t2.micro",
  "local-hostname": "ip-172-31-24-213.us-east-2.compute.internal",
  "local-ipv4": "172.31.24.213",
  "mac": "06:94:4b:ae:c3:f0",
  "metrics": {
   "vhostmd": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  },
  "network": {
   "interfaces": {
    "macs": {
     "06:94:4b:ae:c3:f0": {
      "device-number": "0",
      "interface-id": "eni-08d6bc92857ee79ba",
      "ipv4-associations": {
       "3.17.151.217": "172.31.24.213"
      },
      "local-hostname": "ip-172-31-24-213.us-east-2.compute.internal",
      "local-ipv4s": "172.31.24.213",
      "mac": "06:94:4b:ae:c3:f0",
      "owner-id": "329910648901",
      "public-hostname": "ec2-3-X-Y-217.us-east-2.compute.amazonaws.com",
      "public-ipv4s": "3.X.Y.217",
      "security-group-ids": "sg-08016df3e948f9b1f",
      "security-groups": "launch-wizard-7",
      "subnet-id": "subnet-0eeece021217b39ed",
      "subnet-ipv4-cidr-block": "172.31.16.0/20",
      "vpc-id": "vpc-0f7d68c4a9c1d6593",
      "vpc-ipv4-cidr-block": "172.31.0.0/16",
      "vpc-ipv4-cidr-blocks": "172.31.0.0/16"
     }
    }
   }
  },
  "placement": {
   "availability-zone": "us-east-2b",
   "availability-zone-id": "use2-az2",
   "region": "us-east-2"
  },
  "profile": "default-hvm",
  "public-hostname": "ec2-3-17-151-217.us-east-2.compute.amazonaws.com",
  "public-ipv4": "3.17.151.217",
  "public-keys": {
   "sles": [
    "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC..."
   ]
  },
  "reservation-id": "r-0949782d2698a13fa",
  "security-groups": "launch-wizard-7",
  "services": {
   "domain": "amazonaws.com",
   "partition": "aws"
  }
 }
}