# Execute DDL scripts on GCP Cloud SQL instance via Ansible

**URL:** <https://forum.ansible.com/t/execute-ddl-scripts-on-gcp-cloud-sql-instance-via-ansible/8394>\
**Category:** Get Help\
**Tags:** gcp, collections\
**Created:** [September 10, 2024, 12:17pm UTC](https://forum.ansible.com/t/execute-ddl-scripts-on-gcp-cloud-sql-instance-via-ansible/8394 "2024-09-10T12:17:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [September 10, 2024, 12:17pm UTC](https://forum.ansible.com/t/execute-ddl-scripts-on-gcp-cloud-sql-instance-via-ansible/8394/1 "2024-09-10T12:17:40Z")

</div>

Hi All,  
I am working on an ansible playbook to execute DDL scripts on a private google cloud SQL instance. Below is the playbook code i am using:

- name: Check postgreSQL version  
hosts: localhost  
gather\_facts: no  
become: yes  
vars:  
project\_id: “xxxxxx”  
instance\_name: “xxxxxx”  
region: “xxxxxx”  
tasks:
  - name: Install python3  
apt:  
name: python3

  - name: Install pip3  
apt:  
name: python3-pip

  - name: Install libpq-dev  
apt:  
name: libpq-dev

  - name: Install psycopg2  
apt:  
name: python3-psycopg2

  - name: Connect to SQL instance  
community.postgresql.postgresql\_db:  
db: “{{ db\_name }}”  
login\_host: “{{ ansible\_host }}”  
login\_password: “{{ db\_password }}”  
login\_user: “{{ db\_user }}”  
port: “5432”  
state: present  
target: DDL.sql

Below is the snippet of my inventory file:

cloudsql-instance ansible\_host=x.x.x.x ansible\_user=postgres ansible\_ssh\_private\_key\_file=~/.ssh/id\_rsa

I am executing the playbook from a VM which is on the same network as that of the private cloud sql instance, but the playbook is not able to connect to the instance. How can i achieve this? Kindly assist.

Thanks

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [September 10, 2024, 12:34pm UTC](https://forum.ansible.com/t/execute-ddl-scripts-on-gcp-cloud-sql-instance-via-ansible/8394/2 "2024-09-10T12:34:43Z")

</div>

You’re targeting localhost, so it’s not clear where the inventory is used or where the playbook is failing. Assuming you can ssh manually to cloudsql-instance, you can run your playbook with `-vvvv` to compare the ssh command ansible is using with what you expect.

---

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [September 10, 2024, 12:48pm UTC](https://forum.ansible.com/t/execute-ddl-scripts-on-gcp-cloud-sql-instance-via-ansible/8394/4 "2024-09-10T12:48:03Z")

</div>

We cannot ssh into private google cloud sql instance. That is the challenge. Is there any way to do it? Any collection that supports connection mechanisms other than ssh?

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [September 10, 2024, 1:18pm UTC](https://forum.ansible.com/t/execute-ddl-scripts-on-gcp-cloud-sql-instance-via-ansible/8394/5 "2024-09-10T13:18:52Z")

</div>

I don’t see any custom connection plugins in the google.cloud or community.google collections. 😞 One could be created probably - here’s an example of an alternate connection method for AWS [community.aws.aws\_ssm connection – connect to EC2 instances via AWS Systems Manager — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/community/aws/aws_ssm_connection.html) (all of the google.cloud stuff is autogenerated though, so I’m not sure new plugins can easily be added to that collection).

How would you manually connect? You might be able to configure a custom [ssh executable](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html#parameter-ssh_executable) that invokes the command instead of ssh (here’s an example using `gcloud compute ssh` [google cloud platform - Ansible GCP IAP tunnel - Stack Overflow](https://stackoverflow.com/a/66564017)).

---

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [September 10, 2024, 1:36pm UTC](https://forum.ansible.com/t/execute-ddl-scripts-on-gcp-cloud-sql-instance-via-ansible/8394/7 "2024-09-10T13:36:36Z")

</div>

I believe shell commands can be used, but i wanted to understand if there is any collection/plugin available for ssh connectivity to cloudSQL instance or via API calls.

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [September 10, 2024, 1:37pm UTC](https://forum.ansible.com/t/execute-ddl-scripts-on-gcp-cloud-sql-instance-via-ansible/8394/8 "2024-09-10T13:37:21Z")

</div>

Yes, I understand. I could not find a custom connection plugin. You can see the list of plugins included in the collection here [Google.Cloud — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/google/cloud/index.html).

---

<div class="post-metadata">

**Author:** ![SaravanaSelvaraj](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/saravanaselvaraj/32/2841_2.png) [@SaravanaSelvaraj](https://forum.ansible.com/u/SaravanaSelvaraj)\
**Post date:** [April 25, 2025, 8:57pm UTC](https://forum.ansible.com/t/execute-ddl-scripts-on-gcp-cloud-sql-instance-via-ansible/8394/9 "2025-04-25T20:57:23Z")

</div>

Hello @Chandra_Mukherjee , were you able to achieve this by any method ? I am also having the same problem. Could you please help me ?
