# Execute Ansible playbooks in Terraform main.tf file

**URL:** <https://forum.ansible.com/t/execute-ansible-playbooks-in-terraform-main-tf-file/36892>\
**Category:** Ansible Project\
**Tags:** gcp\
**Created:** [July 23, 2022, 3:22pm UTC](https://forum.ansible.com/t/execute-ansible-playbooks-in-terraform-main-tf-file/36892 "2022-07-23T15:22:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaushal\_Shriyan](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@Kaushal\_Shriyan](https://forum.ansible.com/u/Kaushal_Shriyan)\
**Post date:** [July 23, 2022, 3:22pm UTC](https://forum.ansible.com/t/execute-ansible-playbooks-in-terraform-main-tf-file/36892/1 "2022-07-23T15:22:10Z")

</div>

Hi,

Is there a way to execute Ansible playbooks in Terraform main.tf file? For example I want to deploy nginx webserver inside GCP GCE VM Instance using Ansible playbook once GCP GCE VM Instance is provisioned using Terraform.

Please guide. Thanks in advance.

Best Regards,

Kaushal

---

<div class="post-metadata">

**Author:** ![Rilindo\_Foster1](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Rilindo\_Foster1](https://forum.ansible.com/u/Rilindo_Foster1)\
**Post date:** [July 23, 2022, 3:43pm UTC](https://forum.ansible.com/t/execute-ansible-playbooks-in-terraform-main-tf-file/36892/2 "2022-07-23T15:43:26Z")

</div>

Hi Kausal,

Depending on the provider, you can use cloud-init to:

- Invoke an installation of Ansible

- Download the Ansible code

- Run the code from a local installed instance of Ansible

- Rilindo

---

<div class="post-metadata">

**Author:** ![Kaushal\_Shriyan](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@Kaushal\_Shriyan](https://forum.ansible.com/u/Kaushal_Shriyan)\
**Post date:** [July 23, 2022, 4:00pm UTC](https://forum.ansible.com/t/execute-ansible-playbooks-in-terraform-main-tf-file/36892/3 "2022-07-23T16:00:59Z")

</div>

Thanks Rilindo for the quick response and much appreciated. I will appreciate if you can provide me with a basic working example to understand it and I will implement it once i play around with it. Thanks in Advance.

Best Regards,

Kaushal

---

<div class="post-metadata">

**Author:** ![Kaushal\_Shriyan](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@Kaushal\_Shriyan](https://forum.ansible.com/u/Kaushal_Shriyan)\
**Post date:** [July 23, 2022, 4:04pm UTC](https://forum.ansible.com/t/execute-ansible-playbooks-in-terraform-main-tf-file/36892/4 "2022-07-23T16:04:17Z")

</div>

Hi Rilindo,

I am using Google Cloud Platform (GCP) - Google Cloud as my primary cloud service provider. Please guide. Thanks in advance.

Best Regards,

Kaushal

---

<div class="post-metadata">

**Author:** ![Rilindo\_Foster1](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Rilindo\_Foster1](https://forum.ansible.com/u/Rilindo_Foster1)\
**Post date:** [July 23, 2022, 4:11pm UTC](https://forum.ansible.com/t/execute-ansible-playbooks-in-terraform-main-tf-file/36892/5 "2022-07-23T16:11:01Z")

</div>

Hi Kaushal,

You would want to review the Terraform documentation for google\_compute\_instance - likely metadata\_startup\_script is your answer:

[https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute\_instance#metadata\_startup\_script](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance#metadata_startup_script)

Any more this goes beyond the scope of this mailing list, so I would recommend reaching to appropriate Hashicorp or GCP resources for more help.

- Rilindo

---

<div class="post-metadata">

**Author:** ![Nico\_Kadel-Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nico_kadel-garcia/32/9998_2.png) [@Nico\_Kadel-Garcia](https://forum.ansible.com/u/Nico_Kadel-Garcia)\
**Post date:** [July 23, 2022, 7:16pm UTC](https://forum.ansible.com/t/execute-ansible-playbooks-in-terraform-main-tf-file/36892/6 "2022-07-23T19:16:07Z")

</div>

> Hi Kausal,
> 
> Depending on the provider, you can use cloud-init to:
> 
> - Invoke an installation of Ansible  
> - Download the Ansible code  
> - Run the code from a local installed instance of Ansible
> 
> - Rilindo

I've done this myself, especially to use consistent playbooks for  
docker and Terraform and other virtualization tools without a shared  
Ansible server. Not to \*never\* install the ansible package this way,  
only install "ansible-core", because the ansible package is a bulky  
conglomeration of roughly 100 add-on packages, none or nearly none of  
which are of any use to such a micro ansible setup. Installing the  
"ansible" package slows such installations quite a lot and soaks up  
space not needed for a VM or docker image at all.

Nico Kadel-Garcia
