Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform 创建和设置GCP虚拟机&x27;s具有ansible,ssh权限被拒绝(公钥)_Google Cloud Platform_Ssh_Ansible - Fatal编程技术网

Google cloud platform 创建和设置GCP虚拟机&x27;s具有ansible,ssh权限被拒绝(公钥)

Google cloud platform 创建和设置GCP虚拟机&x27;s具有ansible,ssh权限被拒绝(公钥),google-cloud-platform,ssh,ansible,Google Cloud Platform,Ssh,Ansible,在执行playbook之前,我已经创建了一个服务帐户,并授予它“计算管理员”、“操作系统登录管理员”和“服务帐户用户”的权限。然后我在我的机器上下载了json密钥。服务帐户状态为“活动”。 在我的机器上,我写了一个剧本来设置一个gcp虚拟机,安装apache并在那里复制一个虚拟网页 - name: Create Compute Engine instances hosts: localhost gather_facts: no vars: gc

在执行playbook之前,我已经创建了一个服务帐户,并授予它“计算管理员”、“操作系统登录管理员”和“服务帐户用户”的权限。然后我在我的机器上下载了json密钥。服务帐户状态为“活动”。 在我的机器上,我写了一个剧本来设置一个gcp虚拟机,安装apache并在那里复制一个虚拟网页

- name: Create Compute Engine instances
      hosts: localhost
      gather_facts: no
      vars:
        gcp_project: ansible-xxxxxx
        gcp_cred_kind: serviceaccount
        gcp_cred_file: ~/ansible-key.json
        zone: "us-central1-a"
        region: "us-central1"
        machine_type: "n1-standard-1"
        image: "projects/ubuntu-os-cloud/global/images/family/ubuntu-1604-lts"

      tasks:
        - name: Create an IP address for instance
          gcp_compute_address:
            name: "{{ zone }}-ip"
            region: "{{ region }}"
            project: "{{ gcp_project }}"
            service_account_file: "{{ gcp_cred_file }}"
            auth_kind: "{{ gcp_cred_kind }}"
          register: gce_ip
        - name: Bring up the instance in the zone.
          gcp_compute_instance:
            name: "{{ zone }}"
            machine_type: "{{ machine_type }}"
            disks:
              - auto_delete: true
                boot: true
                initialize_params:
                  source_image: "{{ image }}"
            network_interfaces:
              - access_configs:
                  - name: External NAT
                    nat_ip: "{{ gce_ip }}"
                    type: ONE_TO_ONE_NAT
            tags:
              items:
                - http-server
                - https-server
            zone: "{{ zone }}"
            project: "{{ gcp_project }}"
            service_account_file: "{{ gcp_cred_file }}"
            auth_kind: "{{ gcp_cred_kind }}"
          register: gce
…实例化VM后,我通过ssh连接到它

post_tasks:
    - name: Wait for SSH for instance
      wait_for: delay=5 sleep=5 host={{ gce_ip.address }} port=22 state=started timeout=100
    - name: Save host data for first zone
      add_host: hostname={{ gce_ip.address }} groupname=gce_instances_ips
ansible剧本从来没有通过这一步, 我使用
ansible playbook main.yaml——用户sa_123456789
和 给定的错误是

fatal: [130.211.225.130]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: sa_104318085248975873144@130.211.225.130: Permission denied (publickey).", "unreachable": true}
或者一个简单的超时

fatal: [localhost]: FAILED! => {"changed": false, "elapsed": 105, "msg": "Timeout when waiting for 130.211.225.130:22"}
在GCE的元数据中,我还将enable oslogin设置为TRUE。 虚拟机创建时没有任何问题,可以使用GCP控制台(GUI)访问。如果我试图通过ssh访问私密生成的密钥,那么机器似乎无法访问。
是否有人遇到过此类错误?

当没有生成和设置有效的公钥和私钥时,通常会发生此错误

尝试以下任一方法:

  • 在playbook目录中创建/编辑您的
    ansible.cfg
    文件,并为密钥的完整路径添加一行:

    [defaults]
    privatekeyfile = /Users/username/.ssh/private_key    
    
    它为playbook中的所有主机全局设置私钥

  • 使用以下行将私钥添加到剧本中:

    vars:
    ansible\u ssh\u私钥\u文件:“/home/ansible/.ssh/id\u rsa”
    
  • 您还可以定义要在命令行中直接使用的私钥:

    ansible-playbook -vvvv --private-key=/Users/you/.ssh/your_key playbookname.yml