Execute Ansible playbooks in Terraform main.tf file

240 views
Skip to first unread message

kaushal...@gmail.com

unread,
Jul 23, 2022, 11:22:11 AM7/23/22
to Ansible Project

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

Rilindo Foster

unread,
Jul 23, 2022, 11:43:43 AM7/23/22
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d3e76019-0911-4577-a9e4-85575eb885a5n%40googlegroups.com.

kaushal...@gmail.com

unread,
Jul 23, 2022, 12:00:59 PM7/23/22
to Ansible Project
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

kaushal...@gmail.com

unread,
Jul 23, 2022, 12:04:18 PM7/23/22
to Ansible Project
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

Rilindo Foster

unread,
Jul 23, 2022, 12:11:14 PM7/23/22
to ansible...@googlegroups.com
Hi Kaushal,

You would want to review the Terraform documentation for google_compute_instance - likely metadata_startup_script is your answer:


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

Nico Kadel-Garcia

unread,
Jul 23, 2022, 3:16:29 PM7/23/22
to ansible...@googlegroups.com
On Sat, Jul 23, 2022 at 11:43 AM Rilindo Foster <ril...@gmail.com> wrote:
>
> 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


> On Jul 23, 2022, at 10:22 AM, kaushal...@gmail.com <kaushal...@gmail.com> wrote:
>
> 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
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d3e76019-0911-4577-a9e4-85575eb885a5n%40googlegroups.com.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3C809756-8BCE-4F7D-9E6B-E95AB4BF6361%40gmail.com.
Reply all
Reply to author
Forward
0 new messages