Using ansible modules to create gke clusters failing

52 views Asked by At

I am using Ansible' to create GKE` cluster using the below module

          https://docs.ansible.com/ansible/latest/collections/google/cloud/gcp_container_cluster_module.html

It was working earlier but suddenly the jenkins build has started failing with below errors

fatal: [localhost]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3.9"}, "changed": false, "msg": "GCP returned error: {'error': {'code': 400, 'message': 'Cluster_ipv4_cidr=192.168.8.0/21 and cluster_secondary_range_name=ndev-pods cannot be specified at the same time.', 'status': 'INVALID_ARGUMENT', '

The following snippet is from jenkinsfile to execute the ansible module

         stage ('Execute ansible playbook - DTA') {
        steps {
            script {
                sh "ansible-galaxy collection install google.cloud"
                sh "pip install google-auth"
                
                dir("${env.WORKSPACE}/gke/ansible-gke") {
                    sh "ansible-playbook gke-cluster.yml"
                }
            }
        }
    }

    stage ('Connect to cluster (update kubeconfig)') {
        steps {
            script {
                dir("${env.WORKSPACE}/gke/ansible-gke") {

                Map jsonObj = readJSON file: 'variables.json'

                //def jsonObj;
                jsonObj = readJSON file: 'variables.json'

                sh "gcloud container clusters get-credentials ${jsonObj.cluster_name} --zone ${jsonObj.zone} --project ${jsonObj.project}"

Following is the code snippet from ansible module for assigning the IP addresses where the build is failing

name: "Create a GKE cluster"
google.cloud.gcp_container_cluster:
name: "{{ name }}"
initial_node_count: "{{ initial_node_count }}"
location: "{{ gcp_zone }}"
project: "{{ gcp_project }}"
release_channel:
  channel: "{{ channel}}"
ip_allocation_policy:
  cluster_ipv4_cidr_block: "{{ cluster_ipv4_cidr_block}}"
  cluster_secondary_range_name: "{{ cluster_secondary_range_name}}"
  services_ipv4_cidr_block: "{{ services_ipv4_cidr_block}}"
  services_secondary_range_name: "{{ services_secondary_range_name}}"

Any suggestion to resolve the issue ?

1

There are 1 answers

0
iamwillbin On

Specifically, the error message is saying that you cannot specify both the cluster_ipv4_cidr and cluster_secondary_range_name parameters at the same time.

Depending on your requirements, you should either remove the cluster_ipv4_cidr parameter or the cluster_secondary_range_name parameter from your playbook, but not both.

  • If you want to specify the IP address range for pods using cluster_ipv4_cidr, remove the cluster_secondary_range_name parameter from your playbook.

  • If you want to specify a secondary range for pods using cluster_secondary_range_name, remove the cluster_ipv4_cidr parameter from your playbook.

Make sure that the remaining parameter is properly configured with the correct IP address range or range name that meets your networking requirements then

re-run the Ansible playbook to create the GKE cluster.

Here's an example:

- name: Create GKE Cluster
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Create GKE Cluster
      gcp_container_cluster:
        name: my-gke-cluster
        location: us-central1-a
        # Remove one of the following lines, depending on your
          requirements
        cluster_ipv4_cidr: 192.168.8.0/21  # Specify the IP address range for pods
        # OR
        cluster_secondary_range_name: ndev-pods  # Specify the secondary range for pods
        node_pools:
          - name: default-pool
            machine_type: n1-standard-1
            initial_node_count: 1

This document might also help you in deploying Kubernetes clusters using ansible in GCP.