title: How to make Ansible Dynamic Inventory work with Google Cloud Platform (Google Compute Engine), GCP
body: <p>I used Ansible to create a gce cluster following the guideline at: <a href="https://docs.ansible.com/ansible/latest/scenario_guides/guide_gce.html" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/scenario_guides/guide_gce.html</a></p>
  
  <p>And at the end of the GCE creations, I used the <code>add_host</code> Ansible module to register all instances in their corresponding groups. <code>e.g. gce_master_ip</code></p>
  
  <p>But then when I try to run the following tasks after the creation task, they would not work:</p>
  
  <pre><code>- name: Create redis on the master
    hosts: gce_master_ip
    connection: ssh
    become: True
    gather_facts: True
    vars_files:
      - gcp_vars/secrets/auth.yml
      - gcp_vars/machines.yml
    roles:
      - { role: redis, tags: ["redis"] }
  </code></pre>
  
  <p>Within the <code>auth.yml</code> file I already provided the service account email, path to the json credential file and the project id. But apparently that's not enough. I got errors like below:</p>
  
  <pre><code>UNREACHABLE! =&gt; {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey).\r\n", "unreachable": true}
  </code></pre>
  
  <p>This a typical ssh username and credentials not permitted or not provided. In this case I would say I did not setup anything of the username and private key for the <code>ssh</code> connection that Ansible will use to do the connecting.</p>
  
  <p>Is there anything I should do to make sure the corresponding credentials are provided to establish the connection?</p>
  
  <p>During my search I think one question just briefly mentioned that you could use the <code>gcloud compute ssh...</code> command. But is there a way I could specify in Ansible to not using the classic <code>ssh</code> and use the gcloud one? </p>