【问题标题】:ansible module os_keypair never returns the value of private_keyansible 模块 os_keypair 从不返回 private_key 的值
【发布时间】:2019-09-20 19:27:37
【问题描述】:

我想使用ansible 模块os_keypair 生成带有openstack 的ssh 密钥对。为此,我使用以下代码:

- name: create openstack ssh key pair
  os_keypair:
    name: my-key
    auth: my authentication parameters
    auth_type: password
    state: present
  register: key

当从头开始运行该代码时,即之前没有生成密钥,它的工作原理是在 openstack 中生成和上传密钥。 但是,我需要私钥才能通过 ansible 执行后续操作(例如,local_action 用于将私钥复制到文件中)。模块文档说私钥可作为返回值之一。检查我的返回值时,我得到以下信息:

"key": {
   "created_at": null, 
   "fingerprint": null, 
   "id": null, 
   "is_deleted": null, 
   "location": null, 
   "name": null, 
   "private_key": null, 
   "public_key": null, 
   "type": "ssh", 
   "user_id": null
}

这对我来说有点令人费解。这是正常行为还是该模块有问题?

[更新] 这是重现该行为的完整剧本:

---
- hosts: localhost

  become_user: ansible

  become: True

  tasks:

    - name: create openstack ssh key pair
      os_keypair:
        name: my-key
        auth:
          auth_url: http://cloudsrv1.ill.fr:5000/v3
          username: ansible-test
          password: 1234
          project_name: k8s
          project_domain_name: default
          user_domain_name: default
        auth_type: password
        state: present
      register: key

    - debug:
        var: key

使用 python3 作为 python 解释器执行的剧本。

输出:

TASK [debug] ********************************************************************************************************************
ok: [localhost] => {
    "key": {
        "changed": true, 
        "failed": false, 
        "id": "my-key", 
        "key": {
            "created_at": null, 
            "fingerprint": null, 
            "id": null, 
            "is_deleted": null, 
            "location": null, 
            "name": null, 
            "private_key": null, 
            "public_key": null, 
            "type": "ssh", 
            "user_id": null
        }
    }
}

但是从openstack 快照中可以看出,密钥是生成的。

【问题讨论】:

  • 对我来说似乎工作得很好。您能否使用成功为您重现问题的剧本更新此问题?
  • 非常感谢您的快速反馈。我刚刚添加了一个更新部分以更好地显示问题
  • 这几乎正是我所测试的。我正在使用 Ansible 2.8.4 和 Python 3.7;你可以在这里看到一个完全成功的运行:asciinema.org/a/39bdI6IZiIkksfhjSlyQ8Wsx7
  • @larsks 通过调试机制(ANSIBLE_KEEP_REMOTE_FILES=1 + 模块分解模式)深入研究,我可以看到我的返回值字典在 new_value = _remove_values_conditions(value, no_log_strings , deferred_removals) 的 basic.py 模块。然而,这仍然没有给我任何理由为什么这会发生在我身上而不是你身上。
  • @larsk 你能告诉我你使用的是哪个版本的 openstack 包吗?进一步挖掘,问题来自openstack.compute.v2.keypair.KeyPair 类,该类基于dict,其特殊方法items() 为所有值返回None。要获得 实际 的(例如 private_key、public_key)...必须使用方法 toDict。在ansible.module_utils.common.text.converters.py 模块的通用container_to_text 方法中没有考虑到这个special_behavior 的事实触发了错误的输出。

标签: ansible openstack openstack-nova openstack-horizon


【解决方案1】:

pip 安装最新的openstacksdk 软件包 (0.36) 解决了问题。

【讨论】:

  • 很高兴你知道了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 2012-11-26
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
相关资源
最近更新 更多