> 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/use-connect-with-ansible.md).

# AnsibleでConnectを使う

[1Password Connect Ansible collection](https://github.com/1Password/ansible-onepasswordconnect-collection)には、 [Anisble](https://docs.ansible.com/ansible/latest/getting_started/basic_concepts.html)Playbook から 1Password Connect デプロイメントを操Createできるようにするモジュールが含まれています。これらのモジュールは [Connect API](https://developer.1password.com/docs/connect/connect-api-reference/) と通信して、Create、読み取り、更新、削除の操Createを通じて 1Password 保管庫とアイテムの管理をサポートします。

### **Requirements** <a href="#poaoltczl4iy" id="poaoltczl4iy"></a>

1Password Connect Ansible コレクションを使う前に、次の必要条件を満たす必要があります。

* [Connect サーバーをCreateして展開します。](https://developer.1password.com/docs/connect/get-started/)
* [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html)をインストールするバージョン 7.x 以降。
* [Ansible Core](https://docs.ansible.com/core.html)をインストールするバージョン 2.14 以降。
* [Python](https://www.python.org/downloads/)をインストールするバージョン3.8以降。

### **Get started** <a href="#nmommo2pjhni" id="nmommo2pjhni"></a>

1Password Ansible コレクションの使用を開始するには、次の手順に従ってください。

* ①[1Password コレクションをインストールします。](https://developer.1password.com/docs/connect/ansible-collection/#step-1)
* ②[Ansible プレイブックで 1Password コレクションを使用します。](https://developer.1password.com/docs/connect/ansible-collection/#step-2)
* ③[サンプルプレイブック](https://developer.1password.com/docs/connect/ansible-collection/#examples)を調べてみましょう。

#### **Step 1: Install the collection** <a href="#g2rvfftivsdp" id="g2rvfftivsdp"></a>

onepassword.connectAnsible Galaxyからコレクションをインストールします。

$ ansible-galaxy collection install onepassword.connect

INFO

1Password Ansible コレクションは、[Red Hat Ansible Automation Platform ](https://www.redhat.com/en/technologies/management/ansible)でも利用できます。

#### **Step 2: Use the collection in an Ansible task** <a href="#ubrd47iv0u53" id="ubrd47iv0u53"></a>

Ansible タスクでonepassword.connectコレクションを使用します。

①collectionsタスクにonepassword.connect追加します。

playbook.yaml

![](/files/70PtG0pl7tfapwvHn9Uy)

②Ansible タスクのtoken変数またはOP\_CONNECT\_TOKEN環境変数を通じて Connect サーバー トークンを提供します。この値は各 Ansible タスクで設定する必要があります。

[より安全なため](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_environment.html#working-with-language-specific-version-managers)、ローカル変数を使用して Connect サーバー トークンを設定するのが[best practice](https://developer.1password.com/docs/connect/ansible-collection/#sensitive-data)です。次の例では、connect\_token変数を Connect サーバーのトークン値に設定し、それをtokenフィールドで参照します。

playbook.yaml

![](/files/ivpNujgsWnr0XnUGenZi)

③Ansible タスクのhostname変数またはOP\_CONNECT\_HOST環境変数を通じて、Connect サーバーのホスト名、IP アドレス、または URL を指定します。この値は、各 Ansible タスクで設定する必要があります。

![](/files/munsQpCbGoSEFBEGTWt3)

### **Examples** <a href="#id-7tkn8c3bpct4" id="id-7tkn8c3bpct4"></a>

特定のタスクを実行する方法については、次の例を参照してください。

* [アイテムをCreateします。](https://developer.1password.com/docs/connect/ansible-collection/#create-item)
* [アイテムを更新します。](https://developer.1password.com/docs/connect/ansible-collection/#update-item)
* [名前でアイテムを検索します。](https://developer.1password.com/docs/connect/ansible-collection/#find-item-by-name)
* [フィールドの値を取得します。](https://developer.1password.com/docs/connect/ansible-collection/#get-field-value)

#### **Create an item** <a href="#id-614462fi1br4" id="id-614462fi1br4"></a>

#### 次の例では、[generic\_itemモジュール](https://developer.1password.com/docs/connect/ansible-collection/#generic-item)を使用して1Password アイテムをCreateします。また、カスタムgenerator\_recipeでRandom Code値もCreateします。 <a href="#pp7qlttw56m2" id="pp7qlttw56m2"></a>

playbook.yaml

![](/files/EqX5IYk4vex3SYEK7d6U)

#### **Update an item** <a href="#puf7bdbj3yz3" id="puf7bdbj3yz3"></a>

次の例では、[generic\_itemモジュールを](https://developer.1password.com/docs/connect/ansible-collection/#generic-item)使用して1Password アイテムを更新します。また、generate\_value設定を に設定しますalways。つまり、プレイブックを実行するたびに 1Password がフィールドに新しい値を生Createします。

注意

更新操Createにより、titleまたはuuidフィールドに一致する項目が完全に置き換えられます。タスク定義で指定していないプロパティはすべて失われます。

データが失われないようにするには、Ansible によってCreateされたアイテムを、Connect サーバーだけがアクセスできるスコープの保管庫にSaveします。

![](/files/mFhtpsZUeygJHTdJ3wbO)

#### **Find an item by name** <a href="#xvf5hlky235v" id="xvf5hlky235v"></a>

次の例では、[item\_infoモジュールを](https://developer.1password.com/docs/connect/ansible-collection/#item-info)使用して、名前で 1Password 項目を検索します。

![](/files/USxYAf0o8YZGc6PHkmCL)

#### **Get the value of a field** <a href="#sfj9xqf5s1ul" id="sfj9xqf5s1ul"></a>

次の例では、[field\_infoモジュールを](https://developer.1password.com/docs/connect/ansible-collection/#field-info)使用して1Password アイテム内の特定のフィールドの値を取得します。

![](/files/GZVR6MFXwcHl2VgeiEj5)

### **Reference**[**​**](https://developer.1password.com/docs/connect/ansible-collection/#reference) <a href="#id-5y451gj1rdo6" id="id-5y451gj1rdo6"></a>

使用可能な[変数](https://developer.1password.com/docs/connect/ansible-collection/#variables)と[モジュール](https://developer.1password.com/docs/connect/ansible-collection/#modules)の詳細については、次のセクションを参照してください。

#### **Variables** <a href="#byejrv3a61vi" id="byejrv3a61vi"></a>

**全ての**[モジュール](https://developer.1password.com/docs/connect/ansible-collection/#modules)は、次の変数定義をサポートしています。Ansible タスクで値を明示的に定義するか、Ansible が環境変数にフォールバックして全てのタスクで同じ値を使うようにすることができます。

![](/files/DaqoQ1SClyyLTj7o57q1)

注意

モジュール変数は環境変数よりも優先されます。環境変数を使う場合は、対応するモジュール変数が存在しないことを確認してください。

#### **Modules** <a href="#id-243iuctw9jju" id="id-243iuctw9jju"></a>

1Password Ansible コレクションには次のモジュールがあります。

* [generic\_itemモジュール](https://developer.1password.com/docs/connect/ansible-collection/#generic-item)
* [item\_infoモジュール](https://developer.1password.com/docs/connect/ansible-collection/#item-info)
* [field\_infoモジュール](https://developer.1password.com/docs/connect/ansible-collection/#field-info)

**generic\_item**

このgeneric\_itemモジュールを使用して、1Password アイテムをCreate、更新、削除できます。

**State is important**

generic\_item モジュールは [Ansible's present/absent](https://docs.ansible.com/ansible/2.8/user_guide/playbooks_best_practices.html#always-mention-the-state) stateパターンに従います。

状態がpresent（state: present）のときの動作：

* モジュールがuuid またはtitle で一致する項目を見つけられない場合は、定義された値を持つ新しい項目をCreateします。
* モジュールがサーバー上で一致するアイテムを見つけた場合、古いアイテムをプレイブックの値で定義された新しいアイテムに完全に置き換えます。

状態がabsent（state: absent）のときの動作：

* モジュールが uuidまたは titleでアイテムを見つけられない場合、アクションは実行されません。
* モジュールがuuidまたは titleに一致する項目を見つけた場合、その項目を削除します。それ以外の場合、アクションは実行されません。

generic\_item モジュールを使用して 1Password アイテムをCreateまたは更新すると、1Passwordにフィールドの値を生Createさせることができます。 generate\_value には、次の 3 つの設定のいずれかを指定できます。:

| **generate\_value設定** | **効果**                                                   |
| --------------------- | -------------------------------------------------------- |
| never （デフォルト）         | フィールド値を生Createしないでください。代わりにvalueパラメータを使用してください。          |
| on\_create            | フィールドをCreateするときに値を生Createします。                           |
| always                | プレイブックが実行されるたびに、フィールドに新しい値を生Createします。valueパラメータを上書きします。 |

次の例では、on\_create 設定を使用し、カスタムの generated\_recipe を指定することにより、Random Code フィールドの値 (カスタム レシピを使用) を生Createします。

playbook.yaml

![](/files/9rEzRn0bciRI07xgaGCP)

**item\_info**

item\_infoモジュールを使用して、1Password アイテム (フィールドやメタデータなど) に関する情報を検索または取得します。

**info**

アイテムを検索する場合、1Password は最初にuuid（提供されている場合）を検索し、次に titleを検索します。 titleでアイテムを検索する場合、モジュールは大文字と小文字を区別した完全一致クエリを使用します。

**field\_info**

onepassword.connect.field\_infoモジュールを使用して、アイテム フィールドの値を取得します。

field\_infoモジュールは最初にタイトルまたは UUID でアイテムを検索し、次に要求されたフィールドを名前で検索します。 sectionを指定した場合、モジュールはそのアイテム セクション内でのみ検索します。 セクションを指定しない場合は、フィールド名はアイテム内で一意である必要があります。

検索方法は、フィールド名を[unicodedata.normalize ](https://docs.python.org/3/library/unicodedata.html#unicodedata.normalize)機能とNKFD形状。

### **Best practices** <a href="#id-2jyarqjc8crn" id="id-2jyarqjc8crn"></a>

1Password Ansible コレクションを使う場合は、次のベスト プラクティスを考慮してください。

* [タスクのログ記録をオフにします。](https://developer.1password.com/docs/connect/ansible-collection/#turn-off-logging)
* [機密情報には環境変数を使用しないでください。](https://developer.1password.com/docs/connect/ansible-collection/#sensitive-data)

#### &#x20;<a href="#hw6wc1h044oq" id="hw6wc1h044oq"></a>

#### **Turn off task logging**[**​**](https://developer.1password.com/docs/connect/ansible-collection/#turn-off-logging) <a href="#id-1yzi4av5ra2b" id="id-1yzi4av5ra2b"></a>

1Password Connect と対話するタスクのタスク ログをオフにすることがベスト プラクティスです。 no\_log が設定されていない場合、または false に設定されている場合、Ansible は機密情報を出力する可能性があります。

ログ記録をオフにするには、no\_log を true に設定します。

![](/files/Rnwj4pMZW1lFl5oaVj3t)

#### **Avoid using environment variables for sensitive information** <a href="#id-7f1b91fa168r" id="id-7f1b91fa168r"></a>

Connectサーバートークンなどの機密情報を設定する場合は、 [Ansible環境変数](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_environment.html)を使うのがベストプラクティスです。通常はプレーンテキストで渡されます。
