# Copyright 2024 Google Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. --- # Pre-test setup - name: Delete the test secret if it exists google.cloud.gcp_secret_manager: name: "{{ lookup_resource_name }}" version: "all" project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file | default(omit) }}" state: absent - name: Create a secret google.cloud.gcp_secret_manager: name: "{{ lookup_resource_name }}" value: "ansible lookup test secret value" labels: key1: "val1" key2: "val2" project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file | default(omit) }}" state: present - name: Add a new version to a secret google.cloud.gcp_secret_manager: name: "{{ lookup_resource_name }}" value: "ansible lookup test secret value updated" project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file | default(omit) }}" state: present # ---------------------------------------------------------- - name: Retrieve the latest secret version of a secret ansible.builtin.debug: msg: "{{ lookup('google.cloud.gcp_secret_manager', key=lookup_resource_name, project=gcp_project, auth_kind=gcp_cred_kind, service_account_file=gcp_cred_file | default(omit)) }}" register: result - name: Assert secret value ansible.builtin.assert: that: - result.msg == "ansible lookup test secret value updated" # ---------------------------------------------------------- - name: Retrieve the specified secret version of a secret ansible.builtin.debug: msg: "{{ lookup('google.cloud.gcp_secret_manager', key=lookup_resource_name, version='1', project=gcp_project, auth_kind=gcp_cred_kind, service_account_file=gcp_cred_file | default(omit)) }}" register: result - name: Assert secret value ansible.builtin.assert: that: - result.msg == "ansible lookup test secret value" # --------------------------------------------------------- # Post-test teardown # If errors happen, don't crash the playbook! - name: Delete the test secret google.cloud.gcp_secret_manager: name: "{{ lookup_resource_name }}" version: "all" project: "{{ gcp_project }}" auth_kind: "{{ gcp_cred_kind }}" service_account_file: "{{ gcp_cred_file | default(omit) }}" state: absent ignore_errors: true