> 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-scripts.md).

# シークレットを設定ファイルにロードする

1Password CLIを使うと、[シークレット参照](https://developer.1password.com/docs/cli/secret-reference-syntax/)を使って、平文のシークレットをコードに記述することなく、1Passwordアカウントから設定ファイルにシークレットを自動的に読み込むことができます。これにより、設定ファイルをソース管理にチェックインし、開発者ワークステーション、CI、運用サーバー全体で同期を保つことができます。これは、手動で行う必要があり、エラーが発生しやすい作業です。<br>

{% hint style="success" %}
ヒント&#x20;

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

### [**必須条件**](https://developer.1password.com/docs/cli/secrets-config-files/#requirements) <a href="#id-4fnegtuvb80w" id="id-4fnegtuvb80w"></a>

1Password を使用して設定ファイルを保護する前に、次のことを行う必要があります。

1. [1Passwordにサインアップする。](https://1password.com/pricing)
2. [1Password CLIをインストールする。](https://developer.1password.com/docs/cli/get-started/#step-1-install-1password-cli)
3. プロビジョニングするシークレットを1Passwordアカウントに保存する。

### **ステップ1：**[**シークレット参照**](https://developer.1password.com/docs/cli/secrets-config-files/#step-1-get-secret-references)**を取得する** <a href="#s5egek5yt3qy" id="s5egek5yt3qy"></a>

シークレット参照はいくつかの方法で取得できます。

* [1Passwordデスクトップ アプリを使う場合](https://developer.1password.com/docs/cli/secret-reference-syntax/#with-the-1password-desktop-app)：アプリから秘密の参照をコピーします。
* [1Password for VSCode を使う場合](https://developer.1password.com/docs/vscode/#get-values)：コードを編集するときに 1Password から秘密の参照を挿入します。
* [1Password CLI を使う場合](https://developer.1password.com/docs/cli/secret-reference-syntax/#with-1password-cli)：`op item get`コマンドを使い1つまたは複数のフィールドのシークレット参照を取得します。
* [シークレット参照構文規則](https://developer.1password.com/docs/cli/secret-reference-syntax/#syntax-rules)を使って、シークレット参照を手動で記述します。

### **ステップ 2：設定ファイル内でシークレット参照を使う** <a href="#fkvjcuhhzm3o" id="fkvjcuhhzm3o"></a>

[テンプレート構文](https://developer.1password.com/docs/cli/secrets-template-syntax/)に従って、設定ファイル内の平文のシークレットを適切なシークレット参照に置き換えます。

例えば、次のような設定ファイルから開始するとします。

{% code title="config.yml" %}

```
database:
    host: http://localhost
    port: 5432
    username: op://$APP_ENV/mysql/username
    password: op://$APP_ENV/mysql/password
```

{% endcode %}

そして、`mysql`アイテム上の`username`と`password`シークレットを保存すると`prod`という`vault`に保存すると、次のテンプレート化された構成ファイルが作成されます。

config.yml ファイル\
database:\
host: [http://localhost](http://localhost/)\
port: 5432\
username: op\://prod/mysql/username\
password: op\://prod/mysql/password

### **ステップ2：シークレットを挿入する** <a href="#id-8ay0epef3fnq" id="id-8ay0epef3fnq"></a>

設定ファイルからシークレットをロードし、実行時にプロビジョニングするには、op injectコマンドを使ってシークレットを本番環境に直接挿入します。\
例:

$ op inject -i config.yml.tpl -o config.yml

出力ファイルのconfig.ymlでは、シークレット参照が参照する平文のシークレットに置き換えられます。

設定ファイル テンプレートはソース管理内のコードと一緒に保存されるため、全開発者がファイルの構造を確認できます。<br>

危険

シークレット参照を解決した後の設定ファイルが不要になったら、必ず削除してください。

### **ステップ3：**[環境](https://developer.1password.com/docs/cli/secrets-config-files/#step-3-differentiate-between-environments)ごとに違うシークレットセットに切り替える <a href="#vsexi54xyetf" id="vsexi54xyetf"></a>

あらゆる環境で1Passwordアイテムを同じ方法で整理することを強くお勧めします。\
例：app/dev/db/passwordとapp/prod/db/password

これを行うと、テンプレートファイル内の変数を使用して、別のシークレットセットに切り替えることができます。環境、ステージ、リージョン、またはその他の変数を使用できます。

例:

config.yml ファイル

database:

host: <http://localhost>

port: 5432

username: op\://$APP\_ENV/mysql/username

password: op\://$APP\_ENV/mysql/password

そうすれば、[テンプレート構文](https://developer.1password.com/docs/cli/secrets-template-syntax/)を使って、テンプレートに注入するときにAPP\_ENV変数を設定することができます。

Bash, Zsh, sh, fishの場合\
$ APP\_ENV=prod op inject -i config.yml.tpl -o config.yml\
PowerShellの場合\
①$APP\_ENV変数をprodに設定します。\
PS C:¥> $Env:APP\_ENV = "prod"\
②on injectコマンドでシークレットを挿入します。\
PS C:¥> op inject -i config.yml.tpl -o config.yml

これにより、アプリケーションの近くにあるソース管理に保存されている同じテンプレートファイルを全てのデプロイメントで使用できるようになります。

### **オプション：本番環境でop injectを使う** <a href="#id-3q8d9mtp1gv2" id="id-3q8d9mtp1gv2"></a>

アプリケーションが適切な設定でローカルで動作するようになったので、1Password CLI を使って本番環境でシークレットをプロビジョニングできます。

これを行うには、まず次のことを行う必要があります。

1. [1Password CLI 2を本番環境にインストールします。](https://developer.1password.com/docs/cli/install-server/)
2. [Secrets Automationワークフローを設定します](https://developer.1password.com/docs/connect/)。
3. [1Password Connect Serverをデプロイし](https://developer.1password.com/docs/connect/get-started/#step-2-deploy-1password-connect-server)、本番環境からアクセスできるようにします。

Connect サーバーで1Password CLIを使うには、実稼働環境でのConnectインスタンスの資格情報をOP\_CONNECT\_HOSTとOP\_CONNECT\_TOKENという2つの環境変数に設定します。

これで、シークレットを設定ファイルに移動し、それらにop injectコマンドで簡単にアクセスできるようになります。

Connectサーバーでは次のコマンドを使用できます。

* op run
* op inject
* op read
* op item get

\
次の記事も参照してください。

* [Load secrets into the environment](https://developer.1password.com/docs/cli/secrets-environment-variables/)
* [Secret reference syntax](https://developer.1password.com/docs/cli/secret-reference-syntax/)
* [Template syntax](https://developer.1password.com/docs/cli/secrets-template-syntax/)


---

# 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-scripts.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.
