Streamlining Infrastructure Management: Provisioning Google Cloud VMs with Ansible

As more and more organizations move their workloads to the cloud, managing infrastructure becomes an increasingly important task. Infrastructure management involves the provisioning, configuration, and maintenance of computing resources like virtual machines (VMs) in the cloud. However, managing infrastructure can be a complex and time-consuming process, particularly when it comes to managing large-scale deployments. That’s where Ansible comes in. In this article, we’ll explore how Ansible can be used to streamline infrastructure management by provisioning Google Cloud VMs.

What is Ansible?

Ansible is an open-source automation tool that helps with configuration management, application deployment, and task automation. It uses a simple, human-readable language to describe automation tasks and is easy to use even for those without a programming background. Ansible is agentless, which means that it doesn’t require software to be installed on the target host to manage it.

Why use Ansible for infrastructure management?

Ansible can help streamline infrastructure management in several ways:

  1. Consistency: Ansible ensures that infrastructure is provisioned and configured in a consistent manner across all hosts. This can help reduce errors and make troubleshooting easier.

  2. Scalability: Ansible can manage large-scale deployments with ease, making it an ideal choice for organizations with a significant number of hosts.

  3. Reusability: Ansible’s modules and playbooks can be reused across different projects and environments, making it a valuable asset for organizations that require flexibility and agility.

  4. Time-saving: Ansible’s automation capabilities can significantly reduce the time and effort required to manage infrastructure, freeing up IT teams to focus on more strategic initiatives.

Another benefit of using Ansible for infrastructure management is the ability to use it across different cloud providers and even on-premises infrastructure. This means that you can use the same automation tool to manage infrastructure across different environments, reducing the need for specialized skills and tools.

Prerequisites

To follow this article and use Ansible to provision Google Cloud VMs, you should have some basic knowledge of the following:

  1. Linux: Ansible is primarily a Linux automation tool, so you should have some familiarity with Linux commands, file systems, and permissions.

  2. Cloud Computing: You should have a basic understanding of cloud computing concepts, such as virtual machines, cloud providers, and cloud infrastructure.

  3. Google Cloud Platform (GCP): You should have a GCP account and some familiarity with the GCP console, including creating and managing VMs.

  4. Ansible: You should have a basic understanding of Ansible concepts, such as playbooks, modules, variables, and tasks.

If you are not familiar with any of these concepts, you may want to spend some time learning about them before attempting to follow this article. Many online resources are available for learning about Linux, cloud computing, GCP, and Ansible.

Fret not though, we won’t be going very deep into them, and I’ll be guiding you through the most important concepts.

Ansible can provision and automate anything on GCP and other cloud providers. It’s not limited to provisioning VMs on GCP only.

Long story short, let’s dive straight into what you’re here for!

Provisioning GCP VMs with Ansible

To provision GCP VMs with Ansible, you’ll first need to install ansible on your machine by following the instructions here. We will also need to install the Ansible GCP module. Check here to see a list of GCP collections. This module allows you to interact with the GCP API and perform tasks such as creating, starting, stopping, and deleting VMs.

Instead of just installing a single module, it is better to install the whole google cloud collection to avoid getting any errors that something is missing. Here’s the command:

//install ansible if you don't have it
pip install ansible

//install google cloud ansible collection
ansible-galaxy collection install google.cloud

Before you can use the Ansible GCP module though, you’ll need to set up a service account and download the service account key in JSON format (you’ll need the path to it later).

Now you’re ready to create a playbook. A playbook is a file that describes a set of tasks to be executed on a group of hosts. In this case, we’ll create a playbook that provisions a Google Cloud VM. Copy and paste the content of this file to your machine. The filename is playbook.yml

This playbook creates a VM instance with the specified image, machine type, disk size and type, network, and tags.

You can execute the playbook with the ansible-playbook command like this:

ansible-playbook initial.yml

It’s that simple! You should get a response in your terminal like this:

And when I go to the VM instance in the google cloud console here, I can see it created and running!

That’s simple, right? I bet it is! You can see that it took us less than a minute to do this. It may interest you to know that you can run this playbook many times, which means the process is Idempotent. Instead of hard coding the values, you can also pass them as an argument to the playbook. You may want to look into the documentation on how to do that.

Let’s talk briefly about some lines in the file:

Line 6: This is how you set local variables in an Ansible yml file and they can be accessed throughout the file.

Line 7–8: You’ll need to input the right credentials.

Line 11: Instance name can be anything. But ideally, it’s recommended to make it meaningful.

Line 12–13: Zone and region can be any acceptable zone and region respectively. To see a list of all available zones and regions, run the command:

//list available regions
gcloud compute regions list --project=<project-id>

//list available zones
gcloud compute zones list --project=<project-id>

Line 14–15: That’s my preferred machine type and machine image. To see the available list of images, run the command:


gcloud compute images list --uri --project=<project-id>

My Stackoverflow answer here might help you. Please upvote if it helped you 😢.

Line 25: Using the register key, we’re saving the result of the task into that variable. This is useful if we want to perform another task based on the result of a previous task.

Line 48: Finally, we use the debug module to print the VM's IP address. You can see how we used gcp_ip.address to show the address.

NOTE: If by any chance you get an error that a package or library doesn’t exist, you can just run the command:

pip3 install <package name>

I know this is just provisioning a VM and doing nothing with it. In my upcoming articles, I’ll take you through installing and configuring additional things on the VM to make it useful all with Ansible. Stay tuned.

Conclusion

Automating infrastructure management tasks with Ansible can greatly improve efficiency and reduce errors. Provisioning and managing GCP VMs with Ansible is a powerful way to streamline infrastructure management and ensure that your systems are always configured to your specifications. Whether you’re deploying a new application, scaling up an existing system, or just need to make updates to your infrastructure, Ansible provides a simple and powerful way to automate these tasks. By following the steps outlined in this article, you can start using Ansible to provision Google Cloud VMs in no time.

Of course, this is just the beginning of what you can do with Ansible and Google Cloud. Ansible has a wide range of modules for managing different aspects of cloud infrastructure, from networking to security to storage. You can use Ansible to automate the deployment of applications, configure load balancers, and much more.

If you liked this article, please leave a clap or even a comment and don’t forget to follow me to get updated when I publish another one. Thanks!

7
Subscribe to my newsletter

Read articles from Oluwafemi Akinde directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Oluwafemi Akinde
Oluwafemi Akinde

I'm an experienced backend and cloud devops engineer with great experience in building microservices, distributed systems, SOAs, event-driven systems and writing Infrastructure as Code (IaC). In my spare time, I love writing technical articles, reading engineering blogs, as well as reading books on engineering leadership.