> For the complete documentation index, see [llms.txt](https://manual.dxable.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://manual.dxable.com/1password/load-secrets-into-config-files.md).

# シークレットをスクリプトにロードする

1Password CLIを使ってスクリプトにシークレットを読み込むことで、スクリプト内の認証情報は常に1Passwordアカウントの情報と同期され、シークレットがプレーンテキストで公開されることはありません。

次の方法を個別または組み合わせて使用​​して、シークレットをスクリプトに読み込むことができます。

* op runコマンドを使って環境にシークレットを読み込む
* op readコマンドでシークレットを読みこむ
* op injectコマンドでシークレットを設定ファイルに読み込む
* op plugin runコマンドでシェルプラグインを使ってシークレットを読み込む<br>

ヒント

[最小特権の原則](https://developer.1password.com/docs/cli/best-practices/)に従うために、[1Password Service Accounts](https://developer.1password.com/docs/service-accounts/)を使うことをお勧めします。サービスアカウントは、1Password CLIを特定のVaultに制限することをサポートしているため、承認されたターミナルセッションのプロセスは、特定の目的のために必要なアイテムにのみアクセスできます。

個人アカウントにSSOまたはMFAの必要条件がある場合にも、サービスアカウントは便利です。

### 必要条件 <a href="#u7ytwb814otf" id="u7ytwb814otf"></a>

1Password CLIを使ってスクリプトにシークレットを読み込む前に、次の操Createを行う必要があります。

1. [1Passwordにサインアップしてください。](https://1password.com/pricing)
2. [1Password CLIをインストールしてください。](https://developer.1password.com/docs/cli/get-started/#step-1-install-1password-cli)
3. スクリプトに必要なシークレットを1PasswordアカウントにSaveしてください。<br>

### オプション1：op runコマンドを使って環境にシークレットをロードする <a href="#id-9wvwguad9cop" id="id-9wvwguad9cop"></a>

平文のシークレットを使う代わりに、[シークレット参照](https://developer.1password.com/docs/cli/secret-reference-syntax/)を含む環境ファイルを使用し、実行時に[op run](https://developer.1password.com/docs/cli/reference/commands/run/)コマンドを使ってシークレットをスクリプトに渡すことができます。

この方法を使うと、[各環境で使うシークレットのセットを簡単に変更できる](https://developer.1password.com/docs/cli/secrets-environment-variables/#step-3-differentiate-between-environments)ため、DevOpsエンジニアは1つのシークレットのセットで本番環境でスクリプトを使って、一方で開発者はローカルマシン上で異なるシークレットで同じスクリプトを使うということができます。

例えば、AWS\_SECRET\_ACCESS\_KEYとAWS\_ACCESS\_KEY\_IDという環境変数を使ってスクリプトでAWSコマンドにシークレットを指定し、認証情報がprod vaultのawsアイテムのsecret-keyフィールドとaccess-keyフィールドにSaveされている場合、環境ファイルは次のようになります：

yourscript.env

AWS\_SECRET\_ACCESS\_KEY=op\://prod/aws/secret-key

AWS\_ACCESS\_KEY\_ID=op\://prod/aws/access-key

シークレットをスクリプトに渡すには、環境ファイルに--env-fileフラグを設定して、スクリプト全体をop runコマンドでラップします。

$ op run --env-file yourscript.env -- yourscript.sh

\
[環境にシークレットをロードする方法](https://developer.1password.com/docs/cli/secrets-environment-variables/)についてより深く学んでください。<br>

### オプション2：op readコマンドでシークレットを読み込む <a href="#aiu6a7fqfztc" id="aiu6a7fqfztc"></a>

[スクリプト内で直接](https://developer.1password.com/docs/cli/secrets-scripts/#directly-in-your-script)、または[環境変数を使って](https://developer.1password.com/docs/cli/secrets-scripts/#with-environment-variables)、シークレット参照でop readを利用できます。

### &#x20;スクリプト内で直接op readコマンドを使う <a href="#jjztudnuhw6l" id="jjztudnuhw6l"></a>

この方法では、シークレットはシークレット参照を含む単一のコマンドにのみ渡されます。

例えば、DockerにログインするコマンドでDockerユーザー名とパスワードをシークレット参照に置き換えるには、次のようにします。

yourscript.sh

$ #!/bin/bash

$

$ docker login -u $(op read op\://prod/docker/username) -p $(op read op\://prod/docker/password)

### 環境変数で使う <a href="#b4s1meex4tp4" id="b4s1meex4tp4"></a>

スクリプト内で、op readに対して環境変数をセットするコマンドや[シークレット参照](https://developer.1password.com/docs/cli/secret-reference-syntax/)を含めることもできます。

例えば、スクリプト内で、AWS\_SECRET\_ACCESS\_KEYとAWS\_ACCESS\_KEY\_IDという環境変数を使ってシークレットを含むAWSコマンドを指定する場合、そのスクリプトは次のようになります。\
yourscript.sh

$ #!/bin/bash

$

$ export AWS\_SECRET\_ACCESS\_KEY=$(op read op\://prod/aws/secret-key)

$ export AWS\_ACCESS\_KEY\_ID=$(op read op\://prod/aws/access-key-id)

$aws sts get-caller-identity<br>

### オプション3：op injectコマンドでシークレットを設定ファイルに挿入する <a href="#vkibloi3oilj" id="vkibloi3oilj"></a>

スクリプトが設定ファイルを使う場合は、シークレット参照を使って構成ファイルをテンプレート化し、op injectコマンドを使って、実行時にシークレット参照を基に解決したシークレットを含む設定ファイルをスクリプトに渡すことができます。

これにより、設定ファイルをソース管理にチェックインし、開発者ワークステーションとCIと運用サーバー全体で同期を保つことができます。また、シークレット参照内にテンプレート変数を含めて、さまざまな環境に対して異なるシークレットのセットを読み込むこともできます。

[シークレットを設定ファイルにロードする方法](https://developer.1password.com/docs/cli/secrets-config-files/)についてより深く学んでください。<br>

### オプション 4：op plugin runコマンドでシェルプラグインを使いシークレットをロードする <a href="#gx63tk3p2509" id="gx63tk3p2509"></a>

スクリプトが対話的に実行され、スクリプトを使う各ユーザーが自分の個人トークンで認証する場合、1Password Shell Pluginを使ってスクリプトを使う前に必要な設定を最小限に抑えることができます。シェルlプラグインは、スクリプトの実行時に各ユーザーに資格情報を選択するよう求めます。

スクリプトを使う各ユーザーは、認証に資格情報を使うタイミングを設定するよう求められます。選択した資格情報がスクリプトの今後の呼び出しにも使われるようにするには、資格情報をグローバルまたはディレクトリーのデフォルトとして設定します。

シェルプラグインを使って個々のコマンドを認証するには、各コマンドをop plugin runでラップします。例えばAWSシェルプラグインを使って、sts get-caller-identityコマンドにAWS Access KeyとSecret Key IDを提供するには、次のようにします。

yourscript.sh

$ #!/bin/bash

$

$ op plugin run -- aws sts get-caller-identity

スクリプト全体でシェルプラグインを使うには、スクリプトの先頭にツールの実行可能コマンドのエイリアスを含めることができます。例えばこのスクリプトでは、AWSシェルプラグインを使って、awsスクリプト内の全コマンドのシークレットを提供します。

yourscript.sh

$ #!/bin/bash

$

$ alias aws="op plugin run -- aws"

$ aws sts get-caller-identity

利用しているツール用のシェルプラグインが存在しない場合は、[新しいプラグインをCreate](https://developer.1password.com/docs/cli/shell-plugins/contribute/)できます。

### **参考** <a href="#k35bnj9xaukx" id="k35bnj9xaukx"></a>

* [Example CLI scripts](https://developer.1password.com/docs/cli/scripts/)
* [Get started with secret references](https://developer.1password.com/docs/cli/secret-references/)
* [Load secrets into the environment](https://developer.1password.com/docs/cli/secrets-environment-variables/)
* [Load secrets into config files](https://developer.1password.com/docs/cli/secrets-config-files/)
* [Use 1Password Shell Plugins to securely authenticate third-party CLIs](https://developer.1password.com/docs/cli/shell-plugins/)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://manual.dxable.com/1password/load-secrets-into-config-files.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
