<p>I'm creating a cluster and node pool with</p>
<pre><code>- name: &quot;Create Google Kubernetes Engine Cluster to be setup with with kubectl&quot;
  gcp_container_cluster:
    name: &quot;{{cluster_name}}&quot;
    project: &quot;{{project_id}}&quot;
    auth_kind: &quot;serviceaccount&quot;
    location: &quot;{{cluster_location}}&quot;
    logging_service: &quot;none&quot;
    monitoring_service: &quot;none&quot;
    service_account_contents: &quot;{{service_account_contents}}&quot;
    initial_node_count: 1
  register: cluster
- name: &quot;Create node pool for system pods&quot;
  gcp_container_node_pool:
    name: &quot;default-pool&quot;
    project: &quot;{{project_id}}&quot;
    cluster: &quot;{{ cluster }}&quot;
    auth_kind: &quot;serviceaccount&quot;
    location: &quot;{{cluster_location}}&quot;
    autoscaling:
      enabled: &quot;no&quot;
    initial_node_count: 1
    service_account_contents: &quot;{{service_account_contents}}&quot;
    config:
      machine_type: &quot;g1-small&quot;
      disk_size_gb: 25
      preemptible: &quot;no&quot;
    management:
      auto_repair: &quot;yes&quot;
      auto_upgrade: &quot;yes&quot;
</code></pre>
<p>which works fine, however I find no information how to authenticate and thus gain authorization for <code>kubectl</code> commands executed with the Ansible <code>k8s</code> module.</p>
<p>First of all, the documentation is not helpful both in terms of wording and examples. I didn't find any guide, blogpost or else and feel like I'm the first person performing this tasks with Ansible. I examined the returned cluster object (stored with <code>register</code> above) closely, but didn't find anything suitable. This applies to both <code>gcp_container_cluster</code> and <code>gcp_container_cluster_info</code>.</p>
<p>As far as I understand I need to obtain a kubeconfig somehow with an Ansible module which I can use in the k8s module, like I'd do with <code>gcloud container clusters get-credentials</code>. I'd like to see if there's a built-in way first before I do a hacky workaround using shell commands in Ansible.</p>
<p>I'm using Ansible 2.9.10 on Ubuntu 20.04.</p>

Kalle Richter
<p>When you create the cluster using the <code>gcp_container_cluster</code>, the return value will include the <a href="https://docs.ansible.com/ansible/latest/modules/gcp_container_cluster_module.html#return-kubectlPath" rel="nofollow noreferrer">path where the kubeconfig file is stored</a>. You can refer to this path using your example with {{ cluster.kubectlPath }}.</p>
<p>When you want to use the <code>k8s</code> module, you can define the <a href="https://docs.ansible.com/ansible/latest/modules/k8s_module.html#parameter-kubeconfig" rel="nofollow noreferrer">kubeconfig file path</a> to use for the module.. It will look something like this:</p>
<pre><code>    - name: &quot;Create Google Kubernetes Engine Cluster to be setup with with kubectl&quot;
      gcp_container_cluster:
        name: &quot;{{cluster_name}}&quot;
        kubectlPath: /path/to/save/config
        [...]
      register: cluster
    - name: &quot;Create k8s resource&quot;
      k8s:
        kubeconfig: &quot;{{ cluster.kubectlPath }}&quot;
        definition:
          [...]
</code></pre>
<p>EDIT:
I misread the document, in the &quot;response&quot; section it clarifies that this field must be set for the file to be written in this path, there is no default value for this field.</p>
