DEV Community

Cover image for Streamlining Infrastructure Management: Provisioning Google Cloud VMs with Ansible
#SirPhemmiey
#SirPhemmiey

Posted on • Originally published at oluwafemiakinde.dev on

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. Thats where Ansible comes in. In this article, well 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 doesnt 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 : Ansibles 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 : Ansibles 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 wont be going very deep into them, and Ill be guiding you through the most important concepts.

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

Long story short, lets dive straight into what youre here for!

Provisioning GCP VMs with Ansible

To provision GCP VMs with Ansible, youll 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. Heres the command:

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

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

Enter fullscreen mode Exit fullscreen mode

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

Now youre 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, well 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

Enter fullscreen mode Exit fullscreen mode

Its that simple! You should get a response in your terminal like this:

cover image

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

Thats 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.

Lets 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 78 : Youll need to input the right credentials.

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

Line 1213 : 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>

Enter fullscreen mode Exit fullscreen mode

Line 1415 : Thats 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>

Enter fullscreen mode Exit fullscreen mode

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

Line 25 : Using the register key, were 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 doesnt exist, you can just run the command:

pip3 install <package name>

Enter fullscreen mode Exit fullscreen mode

I know this is just provisioning a VM and doing nothing with it. In my upcoming articles, Ill 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 youre 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 dont forget to follow me to get updated when I publish another one. Thanks!

Top comments (0)