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?

More than 1 year has passed since last update.

OCI CLIでOCIリソースを表示する

Posted at

はじめに

OCIに限らず、パブリッククラウドのリソースはGUIのコンソールで管理することが多いと思いますが、それとは別にCLIで管理することも可能です。
ここでは、OCI CLIを使用したリソース表示方法を確認します。

全部やってるとキリがないので、何かのタイミングで確認したコマンドを追記していくようにしたいと思います。

OCI CLIのバージョン

$ oci --version
3.6.1

Region

リージョンと各リージョンの略称を表示する

$ oci iam region list
{
  "data": [
    {
      "key": "AMS",
      "name": "eu-amsterdam-1"
    },
    {
      "key": "ARN",
      "name": "eu-stockholm-1"
    },
    {
      "key": "AUH",
      "name": "me-abudhabi-1"
    },
    {
      "key": "BOM",
      "name": "ap-mumbai-1"
    },
・・省略・・

OCI CLIの出力はJSON形式になります。
これくらいだったらJSONでもわかりますが、--output tableオプションを追加すると、見やすくなります。

$ oci iam region list --output table
+-----+-------------------+
| key | name              |
+-----+-------------------+
| AMS | eu-amsterdam-1    |
| ARN | eu-stockholm-1    |
| AUH | me-abudhabi-1     |
| BOM | ap-mumbai-1       |
| CDG | eu-paris-1        |
| CWL | uk-cardiff-1      |
| DXB | me-dubai-1        |
| FRA | eu-frankfurt-1    |
| GRU | sa-saopaulo-1     |
| HYD | ap-hyderabad-1    |
| IAD | us-ashburn-1      |
| ICN | ap-seoul-1        |
| JED | me-jeddah-1       |
| JNB | af-johannesburg-1 |
| KIX | ap-osaka-1        |
| LHR | uk-london-1       |
| LIN | eu-milan-1        |
| MEL | ap-melbourne-1    |
| MRS | eu-marseille-1    |
| MTZ | il-jerusalem-1    |
| NRT | ap-tokyo-1        |
| PHX | us-phoenix-1      |
| SCL | sa-santiago-1     |
| SIN | ap-singapore-1    |
| SJC | us-sanjose-1      |
| SYD | ap-sydney-1       |
| VCP | sa-vinhedo-1      |
| YNY | ap-chuncheon-1    |
| YUL | ca-montreal-1     |
| YYZ | ca-toronto-1      |
| ZRH | eu-zurich-1       |
+-----+-------------------+

Compute

コンパートメントのOCIDを指定する必要があります。OCIDは長いので、環境変数に設定しておきます。

$ export OCID=ocid1.compartment.oc1..aaaaaaaamyemvxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Computeインスタンスを一覧表示する

$  oci compute instance list -c $OCID
{
  "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": "ENABLED",
            "name": "OS Management Service Agent"
          },
          {
・・・省略・・・

これもtable形式で表示できますが、値が多いため以下のように逆に見づらくなります。

$ oci compute instance list -c $OCID --output table
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| agent-config                
・・・省略・・・

このような場合は、--queryオプションを使用して、出力したい値のみを出力します。

インスタンス名とインスタンスのOCIDを一覧表示する

$ oci compute instance list -c $OCID --query "data [*].{InstanceName:\"display-name\", OCID:id}" --output table
+--------------+-------------------------------------------------------------------------------------+
| InstanceName | OCID                                                                                |
+--------------+-------------------------------------------------------------------------------------+
| master03     | ocid1.instance.oc1.iad.anuwcljtssxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| worker03     | ocid1.instance.oc1.iad.anuwcljtssxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+--------------+-------------------------------------------------------------------------------------+

インスタンス名、CPU、メモリ、Shape、Statusを一覧表示する

$ oci compute instance list -c $OCID --query "data [*].{InstanceName:\"display-name\", Shape:shape, OCPU:\"shape-config\".ocpus , Memory:\"shape-config\".\"memory-in-gbs\", Status:\"lifecycle-state\"}" --output table
+--------------+--------+------+---------------------+---------+
| InstanceName | Memory | OCPU | Shape               | Status  |
+--------------+--------+------+---------------------+---------+
| master03     | 16.0   | 1.0  | VM.Standard.E4.Flex | RUNNING |
| worker03     | 16.0   | 1.0  | VM.Standard.E4.Flex | RUNNING |
+--------------+--------+------+---------------------+---------+

指定したリージョンのインスタンス名、CPU、メモリ、Shape、Statusを一覧表示する

$ oci compute instance list -c $OCID --query "data [*].{InstanceName:\"display-name\", Shape:shape, OCPU:\"shape-config\".ocpus , Memory:\"shape-config\".\"memory-in-gbs\", Status:\"lifecycle-state\"}" --output table --region NRT
+--------------+--------+------+---------------------+---------+
| InstanceName | Memory | OCPU | Shape               | Status  |
+--------------+--------+------+---------------------+---------+
| OL01         | 8.0    | 1.0  | VM.Standard.E4.Flex | STOPPED |
| OL02         | 8.0    | 1.0  | VM.Standard.E4.Flex | STOPPED |
| OL03         | 8.0    | 1.0  | VM.Standard.E4.Flex | STOPPED |
+--------------+--------+------+---------------------+---------+

(参考)JMESPath

JMESPathはJSONのクエリ言語です。JSONの出力から必要な値を抽出します。

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?