Ssh

為什麼模組 os_keypair 不返回 private_key?

  • June 21, 2016

我正在使用 OpenStack ansible 模組部署到 Rackspace Cloud。

我在創建 SSH 密鑰對時遇到問題。它創建公鑰,並將其安裝在主機上。但是我可以看到返回的 JSON 不包含 private_key 鍵值對。

- name: SSH key check
 os_keypair:
   auth:
     auth_url: "{{ os_auth_url }}"
     project_name: "{{ os_project_name }}"
     username: "{{ os_username }}"
     password: "{{ os_password }}"
   state: present
   region_name: "{{ os_region }}"
   name: "{{ os_key_name }}"
 register: keypair

- name: Create local public
 local_action:
   module: copy
   content: "{{ keypair.key.public_key }}"
   dest: "~/.ssh/{{ keypair.key.name }}.pub"

- name: Create local private key
 local_action:
   module: copy
   content: "{{ keypair.key.private_key }}"
   dest: "~/.ssh/{{ keypair.key.name }}"
 ignore_errors: True

密鑰對 var 的值:

{
"changed": false,
"id": "openstack",
"key": {
 "public_key": "ssh-rsa AAAAByouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeyouknowwhatapublickeylookslikeJiln Generated-by-Nova",
 "NAME_ATTR": "name",
 "name": "openstack",
 "fingerprint": "89:e9:3d:s0:m3:nu:mb:3r:41:d5:09:8b:a9:b8:50:65",
 "id": "openstack",
 "human_id": null,
 "request_ids": [],
 "HUMAN_ID": false,
 "x_openstack_request_ids": []
 }
}

如您所見private_key,不存在。根據文件,它應該返回它。

對這一切都很陌生,所以要溫柔:)

我相信該os_keypair模組沒有對系統進行任何更改。您的keypair變數的內容表明:

{
 "changed": false,
 ...
}

ansible 文件說這是關於 a 的返回private_key

僅當為使用者生成密鑰對時(例如,當創建一個且未指定公鑰時)。

如果存在公鑰,我認為該模組不會生成新的密鑰對。

引用自:https://serverfault.com/questions/784981