【问题标题】:Create and setup GCP VM's with ansible, ssh Permission denied (publickey)使用 ansible、ssh Permission denied (publickey) 创建和设置 GCP VM
【发布时间】:2020-11-05 00:48:31
【问题描述】:

在执行 playbook 之前,我创建了一个服务帐户并为其授予“Compute admin”、“OS Login admin”和“服务帐户”的权限用户”。然后我在我的机器上下载了 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-playbook 永远不会通过这一步, 调用它我使用ansible-playbook main.yaml --user 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。 VM 的创建没有任何问题,并且可以使用 GCP 控制台 (GUI) 进行访问。如果我尝试使用私下生成的密钥通过 ssh 访问,则机器似乎无法访问。 有没有人遇到过这种类型的错误?

【问题讨论】:

    标签: google-cloud-platform ssh ansible


    【解决方案1】:

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

    尝试以下任何一种方法:

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

      [defaults]
      privatekeyfile = /Users/username/.ssh/private_key    
      

      它为 playbook 中的所有主机全局设置私钥。

    2. 使用以下行将私钥添加到您的 playbook:

      vars:
        ansible_ssh_private_key_file: "/home/ansible/.ssh/id_rsa"
      
    3. 您也可以在命令行中定义直接使用的私钥:

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

    【讨论】:

      猜你喜欢
      • 2018-03-01
      • 2021-10-30
      • 2019-01-25
      • 1970-01-01
      • 2012-05-03
      • 2019-02-21
      • 2018-06-01
      • 2018-05-17
      • 2021-02-05
      相关资源
      最近更新 更多