How To Create GKE Cluster Using Ansible

Reading Time: 2 minutes


Ansible is an automation tool for IT. It controls configuration management, ad-hoc task execution, network automation, multi-node orchestration, application deployment, and cloud provisioning. Google Kubernetes Engine gives an environment for deploying, managing, and scaling our containerized application. In this blog, we will demonstrate How To Create GKE Cluster Using Ansible.


Create GKE Cluster Using Ansible

The GCP modules require both the requests and the google-auth libraries to be installed.

  pip install requests google-auth

Folder Hierarchy structure

Create GKE Cluster Using Ansible

Dyanmic variable for Gke Cluster

   # changes according to requirements
   zone: us-central1-c
   region: us-centeral1
   project_id: <> #enter you project id
   gcloud_sa_path: "~/gcpserviceaccount.json" # Enter path to you service account json file
   gcloud_service_account: ""
   credential: "{{lookup('env','HOME') }}/{{gcloud_sa_path}}"

   #Cluster information
   cluster_name: "gkepratice" #Name of the cluster
   initial_node_count: 1   #Number of node for cluster
   disk_size_gb: 100
   disk_type: "pd-ssd"  #disk types 
   machine_type: "e2-medium"  #image types

Add this above file gcp.yaml under the inventory folder

Create a role for the K8s cluster and node pool

Roles let you automatically load related vars, files, tasks, handlers, and other Ansible artifacts based on a known file structure. After you group your content into roles, you can easily reuse them and share them with other users.

For creating cluster and node pool, we are using two ansible module

- name: create a cluster
    name: "{{ cluster_name }}"
    initial_node_count: "{{ initial_node_count }}"
    location: "{{ zone }}"
    network: "{{ }}"
    project: "{{ project_id }}"
    auth_kind: serviceaccount
    service_account_file: "{{ credential }}"
    state: present
  register: cluster  

#create node pool
- name: create a node pool
    name: my-pool-{{ cluster_name }}
    initial_node_count: "{{ initial_node_count }}"
    cluster: "{{ cluster }}"
      disk_size_gb: "{{ disk_size_gb }}"
      disk_type: "{{ disk_type }}"
      machine_type: "{{ machine_type }}"
    location: "{{ zone }}"
    project: "{{ project_id }}"
    auth_kind: serviceaccount
    service_account_file: "{{ credential }}"
    state: present

Use above code under roles/kubernets/tasks/main.yml

Create Role for k8s networks

It is an optional part if we don’t create a network it will make our cluster on the default network. if you don’t want to create a network feel free to skip this part and do a comment network in roles/kubernets/tasks/main.yml

 - name: Create network
    name: network-{{cluster_name}}
    auto_create_subnetworks: 'true'
    project: "{{project_id}}"
    auth_kind: serviceaccount
    service_account_file: "{{credential}}"
    state: present
  register: network

Create playbook Kubernetes

- name: create cluster
  hosts: localhost
  gather_facts: false
  environment:   #cred for serviceaccount.json
    GOOGLE_CREDENTIALS: "{{ credential }}"

    - network
    - kubernetes

Run ansible-playbook for Setup GKE

ansible-playbook  k8s.yml -i inventory/gcp_value.yaml



In this blog, we set up the google Kubernetes engine is very minimal code. we can change the variable file so we can use the same roles to spin up a new cluster in a different zone with a different name using ansible we can set up our infrastructure.

Written by 

Shivam Pateriya is a DevOps Engineer at Knoldus. He likes to learn about emerging technologies. His keen interest in Python, Cloud, and Automation.