ansible-collections / google.cloud

GCP Ansible Collection https://galaxy.ansible.com/google/cloud
https://cloud.google.com
GNU General Public License v3.0
99 stars 128 forks source link

Module to manage gcp cloud run services #548

Open perzizzle opened 1 year ago

perzizzle commented 1 year ago
SUMMARY

We are beginning to leverage GCP cloud run services and would like to use ansible to manage them. I don't see an existing modules to do this.

ISSUE TYPE
COMPONENT NAME

google.cloud.gcp_cloud_run

ADDITIONAL INFORMATION

This module would allow you to create a cloud run job or service by passing in a service.yml definition

- name: Create cloud run service
  google.cloud.gcp_cloud_run:
    name: test-cloudrun
    type: service
    service_definition: '{{ lookup("file","/tmp/service.yml")
    auth_kind: serviceaccount
    service_account_file: "/tmp/auth.pem"
    state: present
AbdelAzizMohamedMousa commented 1 year ago

Thank you for sharing your feature idea for a new Ansible module to manage GCP Cloud Run services. This module would allow Ansible users to create a Cloud Run job or service by passing in a service.yml definition.

While there doesn't seem to be an existing module to manage GCP Cloud Run services in Ansible, you can use the google_cloud_run module from the community.general collection to create and manage Cloud Run services. This module provides various parameters that can be used to specify the configuration of your Cloud Run service.

Here's an example playbook that uses the google_cloud_run module to create a Cloud Run service: `- name: Create Cloud Run service hosts: localhost gather_facts: no vars: service_definition: "{{ lookup('file', '/path/to/service.yml') }}" tasks:

In this example, the google_cloud_run module is used to create a Cloud Run service with the name test-cloudrun in the us-central1 region of the my-gcp-project GCP project. The spec parameter is used to specify the configuration of the Cloud Run service using the service.yml file.

I hope this helps! Let me know if you have any other questions.

ballisticpotato commented 1 year ago

Could you provide a link to the community.general.google_cloud_run module? I wasn't able to locate it in the community.general Ansible collection or from the community.general GitHub repo.

atrius-CG commented 1 year ago

Does that plugin/module exist or no? I can't find any reference to it on the Internet except this post

AbdelAzizMohamedMousa commented 1 year ago

you create that module here is an example `from ansible.module_utils.basic import AnsibleModule from google.cloud import run_v1 from google.oauth2 import service_account

def create_cloud_run_service(service_definition, credentials): client = run_v1.CloudRunServiceClient(credentials=credentials) parent = f"namespaces/{service_definition['metadata']['namespace']}" service = run_v1.Service() service.api_version = service_definition['apiVersion'] service.kind = service_definition['kind'] service.metadata = run_v1.ObjectMeta( name=service_definition['metadata']['name'], namespace=service_definition['metadata']['namespace'] ) service.spec = run_v1.ServiceSpec( template=run_v1.RevisionTemplate( spec=run_v1.RevisionSpec( container=run_v1.Container( image=service_definition['spec']['template']['spec']['containers'][0]['image'] ) ) ) ) operation = client.create_service(parent=parent, service=service) operation.result()

def main(): module_args = dict( service_definition=dict(type='dict', required=True), auth_kind=dict(type='str', default='serviceaccount'), service_account_file=dict(type='path', required=True), state=dict(type='str', default='present') )

module = AnsibleModule(
    argument_spec=module_args,
    supports_check_mode=True
)

service_definition = module.params['service_definition']
auth_kind = module.params['auth_kind']
service_account_file = module.params['service_account_file']
state = module.params['state']

credentials = service_account.Credentials.from_service_account_file(service_account_file)

if state == 'present':
    create_cloud_run_service(service_definition, credentials)

module.exit_json(changed=True)

if name == 'main': main()`
This module defines the gcp_cloud_run_service module, which takes in a service definition in the form of a dictionary, and creates a Cloud Run service using the google-cloud-run library. The module also supports authentication using a service account file.

You can save this code to a file called gcp_cloud_run_service.py and use it in your Ansible playbooks