X

News, tips, partners, and perspectives for the Oracle Linux operating system and upstream Linux kernel work

Easy Compute Instance Metadata Access with OCI Utils

About OCI Utilities

Instances created in Oracle Cloud Infrastructure using Oracle-Provided Images based on Oracle Linux include a pre-installed set of utilities that are designed to make it easier to work with Oracle Linux images. This is a quick blog post to demonstrate how the oci-metadata command included in OCI Utilities make quick work of accessing instance metadata.

Update - March 4th, 2019: This post was updated to include the --value-only option

As of this writing, the following components are included. You can read more about each of the utilities in the OCI Utilities documentation.

  • ocid
  • oci-growfs
  • oci-iscsi-config
  • oci-metadata
  • oci-network-config
  • oci-network-inspector
  • oci-network-inspector
  • oci-public-ip

Working With Instance Metadata Using oci-metadata

Display all instance metadata

To display all metadata in human-readable format, simply run oci-metadata

$ oci-metadata
Instance details:
  Display Name: autonomous blog
  Region: iad - us-ashburn-1 (Ashburn, VA, USA)
  Canonical Region Name: us-ashburn-1
  Availability Domain: PDkt:US-ASHBURN-AD-3
  Fault domain: FAULT-DOMAIN-2
  OCID: ocid1.instance.oc1.iad.abuwcl.................7crrhz2g......aq
  Compartment OCID: ocid1.tenancy.oc1..aaaaaaaa5............qok3lunzc6.....jw7q
  Instance shape: VM.Standard2.1
  Image ID: ocid1.image.oc1.iad.aaaaaaaawuf..............zjc7klojix6vmk42va
  Created at: 1548877740674
  state: Running
  Instance Metadata:
    user_data: dW5kZWZpbmVk
    ssh_authorized_keys: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAxxxxxxxxxxVMheESQgRukanNBmLxaXA0kZw4DxaCispcEjTgAmBmHpUWQBsG7Y/s3zVQDUZ5irMKr2Rtc5DAkH+y6SsNw+xxxxxx+Zix85RClbmu3vl6Mf1++15VoxxxxxEP16mPZl+Cfk/T9LVIlMtV+brph8AQACxFxxxxxxWSNTj1tE8DTml2QnSA6F6MtP6OvOQ0KzQViNm1kN9MaarGOoNxxxxxxNyJGayh8YA6+n8Y07A3fr870H bmc
Networking details:
  VNIC OCID: ocid1.vnic.oc1.iad.abuwcljsrul..............................hioysuicdmzcq
  VLAN Tag: 804
  MAC address: 02:00:17:01:78:09
  Subnet CIDR block: 10.0.2.0/24
  Virtual router IP address: 10.0.2.1
  Private IP address: 10.0.2.3
 

To display all metadata in JSON format:

$ oci-metadata -j 
oci-metadata -j
{
  "instance": {
    "compartmentId": "ocid1.tenancy.oc1..aaaaaaaa5............qok3lunzc6.....jw7q", 
    "displayName": "autonomous blog", 
    "timeCreated": 1548877740674, 
    "state": "Running", 
    "image": "ocid1.image.oc1.iad.aaaaaaaawufnve5jxze4xf7orejupw5iq3pms6cuadzjc7klojix6vmk42va", 
    "canonicalRegionName": "us-ashburn-1", 
    "metadata": {
...

Display a specific metadata key value

The following command displays the value of the canonicalRegionName key, trimming the path to the last component only using the --trim option.

$ oci-metadata -g canonicalRegionName --trim
Canonical Region Name: us-ashburn-1

Exporting metadata values as environment variables

Using eval

To set an environment with the name and value from instance metadata:

$ eval $(oci-metadata --get compartmentId --export)
$ echo $compartmentId
ocid1.tenancy.oc1..aaaaaaaa5............qok3lunzc6.....jw7q

Using the --value-only option

The --value-only option, as the name implies, outputs only the key value without a label. For example:

$ oci-metadata -g "CanonicalRegionName" --value-only
us-phoenix-1

Or, to assign the compartment ID to an environment variable:

$ export MYCOMPARTMENT=`oci-metadata -g "compartmentID" --value-only`
$ echo MYCOMPARTMENT
ocid1.tenancy.oc1..aaaaaaaa5............qok3lunzc6.....jw7q

Using jq

To set an environment variable you name yourself, you can extract the raw value of a key using the jq JSON processor:

$ export MYCOMPARTMENT=`oci-metadata -j --trim -g /instance/compartmentID | jq -r .[]`
$ echo MYCOMPARTMENT
ocid1.tenancy.oc1..aaaaaaaa5............qok3lunzc6.....jw7q

Display the Instance's Public IP Address

To display the instance's public IP address

$ oci-public-ip
Public IP address: 129.xxx.yyy.175
 

And to extract just the IP address:

oci-public-ip -j | jq -r .[]
129.xxx.yyy.175

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services