2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OCI Cloud Shellを使ってリソースの情報を効率よく取得する方法

Last updated at Posted at 2024-09-13

概要

この記事はOracle Cloud Infrastructure(OCI)のCloud Shellを使って、任意のリソース情報を取得する方法を記載します。

対象読者

OCIのリソース情報をコンソール画面で取得するのが大変だと感じている人
Cloud Shellで情報を取得する方法を知りたい人

環境

OCIのCloud Shell

コマンドの調べ方

CLIで使えるコマンドは下記のリファレンスに記載されています。

Oracle Cloud Infrastructure CLI Command Reference

たとえば、Computeの情報を調べる際は、Compute Serviceのリンクを押します。

image.png

Computeの中に様々なコマンドがあるため、自分の必要なコマンドを見つけます。
ここではinstanceを使います。

image.png

基本

基本的な操作を記載します。例としてComputeのinstanceを用います。

リソース情報を取得する

情報を取得する場合はgetまたはlistを使います。
listはコンパートメント内にあるすべてのComputeリソースを取得し、getは指定したComputeリソースの情報を取得します。

そのため基本の流れは

  1. コンパートメントIDを調べる
  2. oci hogehoge listコマンドで一覧情報を取得する
  3. 対象のOCIDを取得する
  4. oci hogehoge getコマンドで特定のリソースの情報を取得する

となります。

コマンド

list
oci compute instance list --compartment-id "ocid"
list 出力結果
{
  "data": [
    {
      "agent-config": {
        "are-all-plugins-disabled": false,
        "is-management-disabled": false,
        "is-monitoring-disabled": false,
        "plugins-config": [
          {
            "desired-state": "DISABLED",
            "name": "Vulnerability Scanning"
          },
          {
            "desired-state": "DISABLED",
            "name": "Oracle Java Management Service"
          },
          {
            "desired-state": "ENABLED",
            "name": "OS Management Service Agent"
          },
          {
            "desired-state": "DISABLED",
            "name": "Management Agent"
          },
          {
            "desired-state": "ENABLED",
            "name": "Custom Logs Monitoring"
          },
          {
            "desired-state": "DISABLED",
            "name": "Compute RDMA GPU Monitoring"
          },
          {
            "desired-state": "ENABLED",
            "name": "Compute Instance Run Command"
          },
          {
            "desired-state": "ENABLED",
            "name": "Compute Instance Monitoring"
          },
          {
            "desired-state": "DISABLED",
            "name": "Compute HPC RDMA Auto-Configuration"
          },
          {
            "desired-state": "DISABLED",
            "name": "Compute HPC RDMA Authentication"
          },
          {
            "desired-state": "DISABLED",
            "name": "Block Volume Management"
          },
          {
            "desired-state": "DISABLED",
            "name": "Bastion"
          }
        ]
      },
      "availability-config": {
        "is-live-migration-preferred": null,
        "recovery-action": "RESTORE_INSTANCE"
      },
      "availability-domain": "*****:*****-*****-1-***-1",
      "capacity-reservation-id": null,
      "cluster-placement-group-id": null,
      "compartment-id": "ocid1.****.****.********",
      "dedicated-vm-host-id": null,
      "defined-tags": {
        "Oracle-Tags": {
          "CreatedBy": "********",
          "CreatedOn": "****-**-**T**:**:**.***Z"
        }
      },
      "display-name": "*****",
      "extended-metadata": {},
      "fault-domain": "FAULT-DOMAIN-2",
      "freeform-tags": {},
      "id": "ocid1.instance.****.********",
      "image-id": "ocid1.image.****.********",
      "instance-configuration-id": null,
      "instance-options": {
        "are-legacy-imds-endpoints-disabled": false
      },
      "ipxe-script": null,
      "is-cross-numa-node": false,
      "launch-mode": "PARAVIRTUALIZED",
      "launch-options": {
        "boot-volume-type": "PARAVIRTUALIZED",
        "firmware": "UEFI_64",
        "is-consistent-volume-naming-enabled": true,
        "is-pv-encryption-in-transit-enabled": true,
        "network-type": "PARAVIRTUALIZED",
        "remote-data-volume-type": "PARAVIRTUALIZED"
      },
      "lifecycle-state": "RUNNING",
      "metadata": {
        "ssh_authorized_keys": "ssh-rsa ***** ssh-key-****-**-**"
      },
      "platform-config": null,
      "preemptible-instance-config": null,
      "region": "*****-*****-1",
      "shape": "VM.Standard.E2.1.Micro",
      "shape-config": {
        "baseline-ocpu-utilization": null,
        "gpu-description": null,
        "gpus": 0,
        "local-disk-description": null,
        "local-disks": 0,
        "local-disks-total-size-in-gbs": null,
        "max-vnic-attachments": 1,
        "memory-in-gbs": 1.0,
        "networking-bandwidth-in-gbps": 0.48,
        "ocpus": 1.0,
        "processor-description": "*****"
      },
      "source-details": {
        "boot-volume-size-in-gbs": null,
        "boot-volume-vpus-per-gb": null,
        "image-id": "ocid1.image.****.********",
        "instance-source-image-filter-details": null,
        "kms-key-id": null,
        "source-type": "image"
      },
      "system-tags": {
        "orcl-cloud": {
          "free-tier-retained": "true"
        }
      },
      "time-created": "****-**-**T**:**:**.*********+**:**",
      "time-maintenance-reboot-due": null
    },
    {
      "agent-config": {
        "are-all-plugins-disabled": false,
        "is-management-disabled": false,
        "is-monitoring-disabled": false,
        "plugins-config": [
          {
            "desired-state": "DISABLED",
            "name": "Vulnerability Scanning"
          },
          {
            "desired-state": "DISABLED",
            "name": "Oracle Java Management Service"
          },
          {
            "desired-state": "DISABLED",
            "name": "OS Management Service Agent"
          },
          {
            "desired-state": "DISABLED",
            "name": "OS Management Hub Agent"
          },
          {
            "desired-state": "DISABLED",
            "name": "Management Agent"
          },
          {
            "desired-state": "ENABLED",
            "name": "Custom Logs Monitoring"
          },
          {
            "desired-state": "DISABLED",
            "name": "Compute RDMA GPU Monitoring"
          },
          {
            "desired-state": "ENABLED",
            "name": "Compute Instance Run Command"
          },
          {
            "desired-state": "ENABLED",
            "name": "Compute Instance Monitoring"
          },
          {
            "desired-state": "DISABLED",
            "name": "Compute HPC RDMA Auto-Configuration"
          },
          {
            "desired-state": "DISABLED",
            "name": "Compute HPC RDMA Authentication"
          },
          {
            "desired-state": "ENABLED",
            "name": "Cloud Guard Workload Protection"
          },
          {
            "desired-state": "DISABLED",
            "name": "Block Volume Management"
          },
          {
            "desired-state": "DISABLED",
            "name": "Bastion"
          }
        ]
      },
      "availability-config": {
        "is-live-migration-preferred": null,
        "recovery-action": "RESTORE_INSTANCE"
      },
      "availability-domain": "*****:*****-*****-1-***-1",
      "capacity-reservation-id": null,
      "cluster-placement-group-id": null,
      "compartment-id": "ocid1.****.****.********",
      "dedicated-vm-host-id": null,
      "defined-tags": {
        "Oracle-Tags": {
          "CreatedBy": "********",
          "CreatedOn": "****-**-**T**:**:**.***Z"
        }
      },
      "display-name": "*****",
      "extended-metadata": {},
      "fault-domain": "FAULT-DOMAIN-2",
      "freeform-tags": {},
      "id": "ocid1.instance.****.********",
      "image-id": "ocid1.image.****.********",
      "instance-configuration-id": null,
      "instance-options": {
        "are-legacy-imds-endpoints-disabled": false
      },
      "ipxe-script": null,
      "is-cross-numa-node": false,
      "launch-mode": "PARAVIRTUALIZED",
      "launch-options": {
        "boot-volume-type": "PARAVIRTUALIZED",
        "firmware": "UEFI_64",
        "is-consistent-volume-naming-enabled": true,
        "is-pv-encryption-in-transit-enabled": true,
        "network-type": "PARAVIRTUALIZED",
        "remote-data-volume-type": "PARAVIRTUALIZED"
      },
      "lifecycle-state": "STOPPED",
      "metadata": {
        "ssh_authorized_keys": "ssh-rsa ***** ssh-key-****-**-**"
      },
      "platform-config": null,
      "preemptible-instance-config": null,
      "region": "*****-*****-1",
      "shape": "VM.Standard.E2.1.Micro",
      "shape-config": {
        "baseline-ocpu-utilization": null,
        "gpu-description": null,
        "gpus": 0,
        "local-disk-description": null,
        "local-disks": 0,
        "local-disks-total-size-in-gbs": null,
        "max-vnic-attachments": 1,
        "memory-in-gbs": 1.0,
        "networking-bandwidth-in-gbps": 0.48,
        "ocpus": 1.0,
        "processor-description": "*****"
      },
      "source-details": {
        "boot-volume-size-in-gbs": null,
        "boot-volume-vpus-per-gb": null,
        "image-id": "ocid1.image.****.********",
        "instance-source-image-filter-details": null,
        "kms-key-id": null,
        "source-type": "image"
      },
      "system-tags": {
        "orcl-cloud": {
          "free-tier-retained": "true"
        }
      },
      "time-created": "****-**-**T**:**:**.*********+**:**",
      "time-maintenance-reboot-due": null
    }
  ]
}

get
oci compute instance get --instance-id "ocid"
get 出力結果
{
  "data": {
    "agent-config": {
      "are-all-plugins-disabled": false,
      "is-management-disabled": false,
      "is-monitoring-disabled": false,
      "plugins-config": [
        {
          "desired-state": "DISABLED",
          "name": "Vulnerability Scanning"
        },
        {
          "desired-state": "DISABLED",
          "name": "Oracle Java Management Service"
        },
        {
          "desired-state": "ENABLED",
          "name": "OS Management Service Agent"
        },
        {
          "desired-state": "DISABLED",
          "name": "Management Agent"
        },
        {
          "desired-state": "ENABLED",
          "name": "Custom Logs Monitoring"
        },
        {
          "desired-state": "DISABLED",
          "name": "Compute RDMA GPU Monitoring"
        },
        {
          "desired-state": "ENABLED",
          "name": "Compute Instance Run Command"
        },
        {
          "desired-state": "ENABLED",
          "name": "Compute Instance Monitoring"
        },
        {
          "desired-state": "DISABLED",
          "name": "Compute HPC RDMA Auto-Configuration"
        },
        {
          "desired-state": "DISABLED",
          "name": "Compute HPC RDMA Authentication"
        },
        {
          "desired-state": "DISABLED",
          "name": "Block Volume Management"
        },
        {
          "desired-state": "DISABLED",
          "name": "Bastion"
        }
      ]
    },
    "availability-config": {
      "is-live-migration-preferred": null,
      "recovery-action": "RESTORE_INSTANCE"
    },
    "availability-domain": "GoDi:AP-OSAKA-1-AD-1",
    "capacity-reservation-id": null,
    "cluster-placement-group-id": null,
    "compartment-id": "[MASKED]",
    "dedicated-vm-host-id": null,
    "defined-tags": {
      "Oracle-Tags": {
        "CreatedBy": "[MASKED]",
        "CreatedOn": "2024-02-13T06:20:06.998Z"
      }
    },
    "display-name": "[MASKED]",
    "extended-metadata": {},
    "fault-domain": "FAULT-DOMAIN-2",
    "freeform-tags": {},
    "id": "[MASKED]",
    "image-id": "[MASKED]",
    "instance-configuration-id": null,
    "instance-options": {
      "are-legacy-imds-endpoints-disabled": false
    },
    "ipxe-script": null,
    "is-cross-numa-node": false,
    "launch-mode": "PARAVIRTUALIZED",
    "launch-options": {
      "boot-volume-type": "PARAVIRTUALIZED",
      "firmware": "UEFI_64",
      "is-consistent-volume-naming-enabled": true,
      "is-pv-encryption-in-transit-enabled": true,
      "network-type": "PARAVIRTUALIZED",
      "remote-data-volume-type": "PARAVIRTUALIZED"
    },
    "lifecycle-state": "RUNNING",
    "metadata": {
      "ssh_authorized_keys": "[MASKED]"
    },
    "platform-config": null,
    "preemptible-instance-config": null,
    "region": "ap-osaka-1",
    "shape": "VM.Standard.E2.1.Micro",
    "shape-config": {
      "baseline-ocpu-utilization": null,
      "gpu-description": null,
      "gpus": 0,
      "local-disk-description": null,
      "local-disks": 0,
      "local-disks-total-size-in-gbs": null,
      "max-vnic-attachments": 1,
      "memory-in-gbs": 1.0,
      "networking-bandwidth-in-gbps": 0.48,
      "ocpus": 1.0,
      "processor-description": "2.0 GHz AMD EPYC\u2122 7551 (Naples)",
      "vcpus": 2
    },
    "source-details": {
      "boot-volume-size-in-gbs": null,
      "boot-volume-vpus-per-gb": null,
      "image-id": "[MASKED]",
      "instance-source-image-filter-details": null,
      "kms-key-id": null,
      "source-type": "image"
    },
    "system-tags": {
      "orcl-cloud": {
        "free-tier-retained": "true"
      }
    },
    "time-created": "2024-02-13T06:20:07.478000+00:00",
    "time-maintenance-reboot-due": null
  },
  "etag": "[MASKED]"
}

リソース情報をフィルタリングして取得する

上記の出力を見て分かる通り、100行単位の情報がJsonとして出力されます。
ここからほしい情報だけをフィルタリングします。

--queryオプションを使用する

oci hogehoge コマンドで使える--queryオプションによってフィルタリングします。

たとえば、oci compute listの出力結果からOCIDだけを取得する場合は以下のようにします。

 oci compute instance list --compartment-id "ocid" --query "data[].id"
出力結果
[
  "ocid1.instance.oc1.ap-osaka-1.****gcb7fb6srlov6pbsbqr43py6p7y43ju6zurmybgho6a",
  "ocid1.instance.oc1.ap-osaka-1.****2tgp3vqkv6qkexqkelniph372t2lo25ubngd5wyxb3a"
]

oci compute listコマンドで出力されるJsonは次のようになっています。
--query "data[]"とするとdata配下の配列内を対象にクエリします。

{
  "data": [
    {},  # ここが対象
    {},  # ここが対象
    ...  # ここが対象
    {}   # ここが対象
  ]
}

--query "data[].id"とすれば、data配下の配列内の要素idを取り出します。

{
  "data": [
    {
      "display-name": "hogehoge",
      "id": "ocid"  # ここが対象
      ...
    },  
    {},  
    ...  
    {}   
  ]
}

複数の情報をフィルタリングしたい場合は--query "data[].[id, \"display-name\"]"とします。

oci compute instance list --compartment-id "ocid" --query "data[].[id, \"display-name\"]"

[
  [
    "ocid1.instance.oc1.ap-osaka-1.anvwsljr4qaav3yc7gcb7fb6srlov6pbsbqr43py6p7y43ju6zurmybgho6a",
    "instance-20240213-1519"
  ],
  [
    "ocid1.instance.oc1.ap-osaka-1.anvwsljr4qaav3ycs2tgp3vqkv6qkexqkelniph372t2lo25ubngd5wyxb3a",
    "instance-20240806-2324"
  ]
]

display-nameのようにハイフンなどの特殊文字が入る場合は\"display-name\"としてエスケープします。

応用

複数のリソース情報を取得する

応用としてoci hogehoge listで取得した複数のIDを使ってoci hogehoge getを実行するスクリプトを記載します。


ids=(`oci compute instance list --compartment-id "ocid" --query "data[].id" | jq -r .[]`)

for id in ${ids[@]} ; do 
    oci compute instance get --instance-id ${id} ; 
done

ids変数は配列にするため()で囲みます。

oci compute ~~ --queryのあとにパイプでjqコマンドに渡しています。
これは出力をテキスト出力にするためと、[]をなくすためです。

oci compute ~~ --queryのみ
[
  "ocid1.instance.oc1.ap-osaka-1.****gcb7fb6srlov6pbsbqr43py6p7y43ju6zurmybgho6a",
  "ocid1.instance.oc1.ap-osaka-1.****2tgp3vqkv6qkexqkelniph372t2lo25ubngd5wyxb3a"
]

上記のままだとforループに配列として渡せないので、jq .[][]を取ります。

oci compute ~~ --query | jq .[]
"ocid1.instance.oc1.ap-osaka-1.****gcb7fb6srlov6pbsbqr43py6p7y43ju6zurmybgho6a"
"ocid1.instance.oc1.ap-osaka-1.****2tgp3vqkv6qkexqkelniph372t2lo25ubngd5wyxb3a"

上記のままだと、"ocid"のように""でIDが囲まれてしまっており、このままociコマンドに渡すとOCIDが一致せずにエラーが発生します。

そのため、jq -r .[]とします。
これはraw textで出力するオプションです。

oci compute ~~ --query | jq .[]
ocid1.instance.oc1.ap-osaka-1.****gcb7fb6srlov6pbsbqr43py6p7y43ju6zurmybgho6a
ocid1.instance.oc1.ap-osaka-1.****2tgp3vqkv6qkexqkelniph372t2lo25ubngd5wyxb3a

あとはbashのforループでIDを取り出してoci hogehoge getコマンドを実行します。

任意の--queryオプションをつけて出力をカスタマイズしてください。

参考

OCI公式ドキュメント

CLIの使用

jqのraw出力

How to remove double-quotes in jq output for parsing json files in bash?

質問
https://stackoverflow.com/q/44656515

参考にした回答
https://stackoverflow.com/a/44656583

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?