9
10

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 5 years have passed since last update.

Oracle Cloud(OCI)のoci cli設定ファイルの使い方をまとめてみた

Last updated at Posted at 2019-01-29

Oracle Cloud Infrastructure(OCI)のcliを利用する際の設定ファイル(configファイルとoci_cli_rcファイル)についてまとめました。うまく使うとcliがとても便利に使えます!

1.configファイル

概要

必須の設定ファイル。
ユーザーやテナンシーのOCIDなどcliを使うための基本情報を記載

記載する内容

[DEFAULT]
user=<使用するユーザーのOCID>
Fingerprint=<APIキーのフィンガープリント>
Key_file=<APIキーの秘密鍵のパス>
tenancy=<テナンシーのOCID>
region=<リージョン名>

作成方法

  • 初回はoci setup configで対話的に生成可能(手書きでもOK)
  • デフォルトの場所は
    • Windows : \Users<ユーザー名>.oci\config
    • Linux : /home/<ユーザー名>/.oci/config

その他

プロファイルのセクションを追加して、ociコマンド利用時に切り替え可能

・sample

[DEFAULT]
user=ocid1.user.XXXXXXXXXXXXXX
Fingerprint=xx: xx: xx: xx: xx: 
Key_file=/home/opc/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.XXXXXXXX
region=us-phoenix-1

[TEST]
user=ocid1.user.XXXXXXXXXXXXXX
Fingerprint=xx: xx: xx: xx: xx: 
Key_file=/home/opc/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.XXXXXXXX
region=us-ashburn-1

・利用方法
$ oci xxx xxx --profile TEST : [TEST]に記載した内容を使用

・優先順位
“--profile”オプション > 環境変数”OCI_CLI_PROFILE” > default_profile

・ユースケース
プロファイルのセクションは、リージョンを変えたいときも使いますが、どちらかというと次に説明するoci_cli_rcでデフォルト値を設定するときに使います。

2.oci_cli_rcファイル

概要

cliを便利に使うための追加設定ファイル

記載する内容/サンプル

[OCI_CLI_SETTINGS]
# default_profile=<デフォルトとして使用するプロファイル>
# 例)[TEST]プロファイルをデフォルトとする
default_profile=TEST 

[OCI_CLI_CANNED_QUERIES]
# <queryオプションで使用するJMESPath>
# 例)"test"という名前でフィルタ
filter_by_display_name_contains_text=data[?contains("display-name", `test`)]	

[OCI_CLI_COMMAND_ALIASES]
#<エイリアス>=<コマンド>
# 例)"list"コマンドを"ls"で実行可能に
ls = list   

[OCI_CLI_PARAM_ALIASES]
# 例)"availability-domain"オプションを"--ad"で指定可能に
--ad = --availability-domain  

# [<セクション名>]
# <オプション>=<値>
# DEFAULTプロファイルで使用するコンパートメントのOCIDを指定
[DEFAULT]
compartment-id = ocid1.compartment.oc1..aaaaaaaasample001

# TESTプロファイルで使用するコンパートメントのOCIDを指定
[TEST]
compartment-id = ocid1.compartment.oc1..aaaaaaaasample002

作成方法

  • oci setup oci-cli-rcでファイル生成
  • デフォルトの場所は "~/.oci/oci_cli_rc"

使用方法

  • 使用するoci_cli_rcファイルの指定

    • デフォルトの場所(“~/.oci/oci_cli_rc”)にある場合、指定不要
    • ”--cli-rc-file”オプションを指定すれば任意の場所のファイルを指定して実行可能
  • 優先順位
    - コマンドラインで指定 > oci_cli_rcで定義されたデフォルト値
    - 広い範囲で定義されたデフォルト値 > より狭い範囲で定義されたデフォルト値

その他

生成時に設定されている内容

[OCI_CLI_CANNED_QUERIES]
# For list results, this gets the ID and display-name of each item in the list. Note that when the names of attirbutes have
# dashes in them they need to be surrounded with double quotes. This query knows to look for a list because of the
# [*] syntax
get_id_and_display_name_from_list=data[*].{id: id, "display-name": "display-name"}

get_id_and_display_name_from_single_result=data.{id: id, "display-name": "display-name"}

# Retrieves a comma separated string, for example:
#     ocid1.instance.oc1.phx.xyz....,cli_test_instance_675195,RUNNING
get_id_display_name_and_lifecycle_state_from_single_result_as_csv=data.[id, "display-name", "lifecycle-state"] | join(`,`, @)

# Retrieves comma separated strings from a list of results
get_id_display_name_and_lifecycle_state_from_list_as_csv=data[*].[join(`,`, [id, "display-name", "lifecycle-state"])][]

# Filters where the display name contains some text
filter_by_display_name_contains_text=data[?contains("display-name", `your_text_here`)]

# Filters where the display name contains some text and pull out certain attributes(id and time-created)
filter_by_display_name_contains_text_and_get_attributes=data[?contains("display-name", `your_text_here`)].{id: id, timeCreated: "time-created"}

# Get the top 5 results from a list operation
get_top_5_results=data[:5]

# Get the last 2 results from a list operation
get_last_2_results=data[-2:]

# List instance public IPs from list vnics response
# Example: oci compute instance list-vnics --instance-id $I --query query://list_public_ips
list_public_ips=data[?"public-ip" != null]."public-ip"

# Get first public IP from list vnics response
# Example: oci compute instance list-vnics --instance-id $I --query query://get_public_ip
get_public_ip=data[?"public-ip" != null]."public-ip" | [0]



[OCI_CLI_COMMAND_ALIASES]
# This lets you use "ls" instead of "list" for any list command in the CLI
ls = list

# This lets you do "oci os object rm" rather than "oci os object delete". This is an example of a "command sequence alias", where the alias on the left
# hand side only applies when the command sequence on the right hand side is invoked. You can think of these as being of the form:
#    <alias> = <dot-separated sequence of groups and sub-groups>.<command or group to alias>
#
# So in our example, <alias> = rm, <sequence of groups and sub-groups> = os object, <command or group to alias> = delete
rm = os.object.delete



[OCI_CLI_PARAM_ALIASES]
# Parameter aliases either need to start with a double dash (--) or be a single dash (-) followed by a single letter. For example: --foo, -f
--ad = --availability-domain
--dn = --display-name
--egress-rules = --egress-security-rules
--ingress-rules = --ingress-security-rules

ユースケース

一番良く使うのはコンパートメントのデフォルト値だと思います。これしないとcliコマンド使うたびにコンパートメントを指定しなければいけなくなります。
また、複数のアカウントを使い分けている場合も、こちらが大変便利です。

oci cliのupdate

記事関係ないですが、cliのバージョンを最新にした際のメモをこちらに。

PS C:\test>  oci cli -v
2.5.2
PS C:\test> pip list
Package         Version
--------------- ----------
oci-cli         2.5.5
PS C:\test> pip install oci-cli --upgrade
Collecting oci-cli
  Downloading https://files.pythonhosted.org/packages/83/aa/922fc63808ffc79d114474bef9af8b4a066e4653898b6df427cfcf98f166/oci_cli-2.5.5-py2.py3-none-any.whl (3.8MB)
    100% |################################| 3.8MB 2.2MB/s
Requirement already satisfied, skipping upgrade: idna<2.7,>=2.5 in c:\python27\lib\site-packages (from oci-cli) (2.6)
Requirement already satisfied, skipping upgrade: retrying==1.3.3 in c:\python27\lib\site-packages (from oci-cli) (1.3.3)
Requirement already satisfied, skipping upgrade: python-dateutil==2.7.3 in c:\python27\lib\site-packages (from oci-cli) (2.7.3)
Requirement already satisfied, skipping upgrade: certifi in c:\python27\lib\site-packages (from oci-cli) (2018.11.29)
Requirement already satisfied, skipping upgrade: PyYAML==3.13 in c:\python27\lib\site-packages (from oci-cli) (3.13)
Requirement already satisfied, skipping upgrade: cryptography==2.4.2 in c:\python27\lib\site-packages (from oci-cli) (2.4.2)
Requirement already satisfied, skipping upgrade: httpsig-cffi==15.0.0 in c:\python27\lib\site-packages (from oci-cli) (15.0.0)
Requirement already satisfied, skipping upgrade: pyOpenSSL==18.0.0 in c:\python27\lib\site-packages (from oci-cli) (18.0.0)
Requirement already satisfied, skipping upgrade: pytz==2016.10 in c:\python27\lib\site-packages (from oci-cli) (2016.10)
Requirement already satisfied, skipping upgrade: cx-Oracle==7.0 in c:\python27\lib\site-packages (from oci-cli) (7.0.0)
Requirement already satisfied, skipping upgrade: click==6.7 in c:\python27\lib\site-packages (from oci-cli) (6.7)
Requirement already satisfied, skipping upgrade: terminaltables==3.1.0 in c:\python27\lib\site-packages (from oci-cli) (3.1.0)
Requirement already satisfied, skipping upgrade: arrow==0.10.0 in c:\python27\lib\site-packages (from oci-cli) (0.10.0)
Requirement already satisfied, skipping upgrade: six==1.11.0 in c:\python27\lib\site-packages (from oci-cli) (1.11.0)
Requirement already satisfied, skipping upgrade: configparser==3.5.0 in c:\python27\lib\site-packages (from oci-cli) (3.5.0)
Requirement already satisfied, skipping upgrade: jmespath==0.9.3 in c:\python27\lib\site-packages (from oci-cli) (0.9.3)
Collecting oci==2.2.4 (from oci-cli)
  Downloading https://files.pythonhosted.org/packages/d2/55/400fd5817860774174c9cdbce62b863eea1fdec73eb5f68b63e5b9f3b838/oci-2.2.4-py2.py3-none-any.whl (2.0MB)
    100% |################################| 2.0MB 3.3MB/s
Requirement already satisfied, skipping upgrade: asn1crypto>=0.21.0 in c:\python27\lib\site-packages (from cryptography==2.4.2->oci-cli) (0.24.0)
Requirement already satisfied, skipping upgrade: enum34; python_version < "3" in c:\python27\lib\site-packages (from cryptography==2.4.2->oci-cli) (1.1.6)
Requirement already satisfied, skipping upgrade: cffi!=1.11.3,>=1.7 in c:\python27\lib\site-packages (from cryptography==2.4.2->oci-cli) (1.11.5)
Requirement already satisfied, skipping upgrade: ipaddress; python_version < "3" in c:\python27\lib\site-packages (from cryptography==2.4.2->oci-cli) (1.0.22)
Requirement already satisfied, skipping upgrade: pycparser in c:\python27\lib\site-packages (from cffi!=1.11.3,>=1.7->cryptography==2.4.2->oci-cli) (2.19)
Installing collected packages: oci, oci-cli
  Found existing installation: oci 2.2.1
    Uninstalling oci-2.2.1:
      Successfully uninstalled oci-2.2.1
  Found existing installation: oci-cli 2.5.2
    Uninstalling oci-cli-2.5.2:
      Successfully uninstalled oci-cli-2.5.2
Successfully installed oci-2.2.4 oci-cli-2.5.5
PS C:\test>  oci cli -v
2.5.5

参考

9
10
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
9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?