The 1Password Connect Ansible collection contains modules that allow you to interact with your 1Password Connect deployment from Ansible playbooks. The modules communicate with the Connect API to support managing 1Password vaults and items through create, read, update, and delete operations.
Use the onepassword.connect collection in an Ansible task:
Add onepassword.connect to the task collections.
playbook.yaml
Report incorrect code
Copy
Ask AI
collections: - onepassword.connect # Specify the 1Password collection
Provide the Connect server access token using the token variable in the Ansible task or the OP_CONNECT_TOKEN environment variable. You must set this value in each Ansible task.It’s best practice to use a local variable to provide the Connect server access token because it’s more secure. The following example sets the connect_token variable to the Connect token value, then references it for the token field.
playbook.yaml
Report incorrect code
Copy
Ask AI
vars: connect_token: "<connect-server-token>" # Set the Connect server access tokencollections: - onepassword.connect # Specify the 1Password collectiontasks: - onepassword.connect.generic_item: token: "{{ connect_token }}"
Provide the Connect server hostname, IP address, or URL through the hostname variable in the Ansible task or the OP_CONNECT_HOST environment variable. You must set this value in each Ansible task.
playbook.yaml
Report incorrect code
Copy
Ask AI
environment: OP_CONNECT_HOST: <connect-host> # Set the Connect server hostnamecollections: - onepassword.connect # Specify the 1Password collection
The following example uses the generic_item module to create a 1Password item. It also creates the Random Code value with a custom generator_recipe.
playbook.yaml
Report incorrect code
Copy
Ask AI
- name: Create 1Password Secret hosts: localhost vars: connect_token: "<connect-server-token>" # Set the Connect server access token environment: OP_CONNECT_HOST: <connect-host> # Set the Connect server hostname collections: - onepassword.connect # Specify the 1Password collection tasks: - onepassword.connect.generic_item: token: "{{ connect_token }}" # Pass the Connect server access token variable vault_id: "<vault-id>" # Set the 1Password vault ID title: Club Membership state: present fields: - label: Codeword value: "hunter2" section: "Personal Info" field_type: concealed - label: Random Code generate_value: on_create # Generate the value on creation generator_recipe: length: 16 include_symbols: no no_log: true # Turn off logs to avoid logging sensitive data register: op_item # Note: register is Ansible syntax
The following example uses the generic_item module to update a 1Password item. It also sets the generate_value setting to always, which means 1Password generates a new value for the field each time you run the playbook.
The update operation completely replaces the item matching the title or uuid field. You will lose any properties that you don’t provide in the task definition.To avoid losing data, store the items created by Ansible in a vault that’s scoped in a way that only the Connect server can access it.
playbook.yaml
Report incorrect code
Copy
Ask AI
- name: Update a 1Password Secret hosts: localhost vars: connect_token: "<connect-server-token>" # Set the Connect server access token environment: OP_CONNECT_HOST: <connect-host> # Set the Connect server hostname OP_VAULT_ID: "<vault-id>" # Set the 1Password vault ID collections: - onepassword.connect # Specify the 1Password collection tasks: - onepassword.connect.generic_item: token: "{{ connect_token }}" # Pass the Connect server access token variable title: Club Membership # uuid: 1ff75fa9fexample -- or use an Item ID to locate an item instead state: present fields: - label: Codeword field_type: concealed - label: Dashboard Password generate_value: always # Generate a new value every time the playbook runs generator_recipe: # Provide a custom password recipe length: 16 include_symbols: no no_log: true # Turn off logs to avoid logging sensitive data
The following example uses the item_info module to find a 1Password item by name.
playbook.yaml
Report incorrect code
Copy
Ask AI
hosts: localhost vars: connect_token: "<connect-server-token>" # Set the Connect server access token environment: OP_CONNECT_HOST: <connect-host> # Set the Connect server hostname collections: - onepassword.connect # Specify the 1Password collection tasks: - name: Find the item with the label "Staging Database" in the vault "Staging Env" item_info: token: "{{ connect_token }}" item: Staging Database vault: Staging Env no_log: true # Turn off logs to avoid logging sensitive data register: op_item
The following example uses the field_info module to get the value of a specific field in a 1Password item.
playbook.yaml
Report incorrect code
Copy
Ask AI
hosts: localhost vars: connect_token: "<connect-server-token>" # Set the Connect server access token environment: OP_CONNECT_HOST: <connect-host> # Set the Connect server hostname collections: - onepassword.connect # Specify the 1Password collection tasks: - name: Find a field labeled "username" in an item named "MySQL Database" in a specific vault field_info: token: "{{ connect_token }}" # Pass the Connect token variable item: MySQL Database field: username vault: <vault-id> # Set the 1Password vault ID no_log: true # Turn off logs to avoid logging sensitive data register: op_item - name: Print the field definition ansible.builtin.debug: msg: "{{ op_item.field }}"
All modules support the following variable definitions. You can either explicitly define the value on the Ansible task or let Ansible fall back to an environment variable to use the same value across all tasks.
Module variable
Environment variable
Description
hostname
OP_CONNECT_HOST
Specifies the hostname, IP address, or URL where your Connect server is deployed.
token
OP_CONNECT_TOKEN
Specifies the string value of your Connect server access token.
vault_id
OP_VAULT_ID
(Optional) The UUID of a 1Password vault. It must be a vault the Connect server token has access to.
Module variables take precedence over environment variables. If you plan to use an environment variable, make sure the corresponding module variable is absent.
If the module can’t find a matching item by its uuid or title, it creates a new item with the defined values.
If the module finds a matching item on the server, it completely replaces the old item with a new item defined by the playbook values.
Behavior when the state is absent (state: absent):
If the module can’t find the item by its uuid or title, no action is taken.
If the module finds an item matching the uuid or title, it deletes the item. Otherwise, no action is taken.
When you use the generic_item module to create or update a 1Password item, you can have 1Password generate a field’s value. You can specify one of three settings for generate_value:
generate_value setting
Effect
never(Default)
Don’t generate the field value. Use the value parameter instead.
on_create
Generate the value when creating the field.
always
Generate a new value for the field every time the playbook is run. Overwrites the value parameter.
The following example generates a value (with a custom recipe) for the Random Code field by using the on_create setting and supplying a custom generator_recipe.
playbook.yaml
Report incorrect code
Copy
Ask AI
- name: Create 1Password Secret hosts: localhost vars: connect_token: "<connect-server-token>" # Set the Connect server access token environment: OP_CONNECT_HOST: <connect-host> # Set the Connect server hostname collections: - onepassword.connect # Specify the 1Password collection tasks: - onepassword.connect.generic_item: token: "{{ connect_token }}" # Pass the Connect token variable vault_id: "<vault-id>" # Set the 1Password vault ID (optional) title: Club Membership state: present fields: - label: Codeword value: "hunter2" section: "Personal Info" field_type: concealed - label: Random Code generate_value: on_create # Generate the field value on creation generator_recipe: # Provide a custom password recipe length: 16 include_digits: no no_log: true # Turn off logs to avoid logging sensitive data register: op_item
Use the item_info module to search for or get information about a 1Password item (such as the fields or metadata).
When you search for an item, 1Password first searches for the uuid (if it’s provided), then searches for the title. When searching for an item by its title, the module uses a case-sensitive, exact-match query.
Use the onepassword.connect.field_info module to get the value of an item field.The field_info module first finds the item by title or UUID, then searches for the requested field by name. If you provide a section, the module only searches within that item section. If you don’t provide a section, the field name must be unique within the item.The search method compares field names using the unicodedata.normalize function and the NKFD form.
It’s best practice to turn off task logging for any tasks that interact with 1Password Connect. Ansible might print sensitive information if no_log is unset or set to false.To turn off logging, set no_log to true:
playbook.yaml
Report incorrect code
Copy
Ask AI
collections: - onepassword.connect # Specify the 1Password collectiontasks: - name: Find the item with the label "Staging Database" in the vault "Staging Env" item_info: token: "{{ connect_token }}" # Pass the Connect token variable item: Staging Database vault: Staging Env no_log: true # Turn off logs to avoid logging sensitive data register: op_item
Avoid using environment variables for sensitive information
It’s best practice to use a local variable to set sensitive information, such as the Connect server access token, because Ansible environment variables are normally passed in plain text.