You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Ansible调用Hetzner hcloud模块获取服务器信息报错求助

解决Ansible无法找到hcloud模块的问题

核心问题分析

错误提示找不到hcloud模块,原因是你使用了旧版独立模块的命名方式,而当前安装的hetzner.hcloud集合采用了新的模块命名规范;同时任务的目标主机设置不符合API调用场景的要求。

具体修复步骤

1. 使用集合模块的全限定名称

hetzner.hcloud集合中获取服务器列表的模块为hetzner.hcloud.server_info,替代旧的hcloud模块。新模块无需通过command参数指定操作,直接通过模块本身实现服务器列表查询。

2. 调整任务目标主机为localhost

该任务是直接调用Hetzner公开API,无需连接远程服务器,因此需将hosts设为localhost,并添加connection: local确保任务在本地执行。

3. 优化变量引用(可选)

你已在vars中定义hcloud_token变量,可直接引用该变量避免重复写入API token,提升配置可维护性。

修正后的完整Playbook

---
- name: List Hetzner server names and IP addresses
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    hcloud_token: "MY_HETZNER_API_TOKEN"
  tasks:
    - name: Fetch Hetzner server list
      hetzner.hcloud.server_info:
        api_token: "{{ hcloud_token }}"
      register: server_list

    - name: Print server names and IP addresses
      debug:
        msg: "Server {{ item.name }} has IP address {{ item.public_net.ipv4.ip }}"
      loop: "{{ server_list.hcloud_servers }}"

额外验证步骤

若问题仍存在,可执行以下命令确认集合模块是否被Ansible正确识别:

ansible-doc hetzner.hcloud.server_info

如果能正常显示模块文档,说明集合模块路径配置无问题。

内容的提问来源于stack exchange,提问作者Melvin Magro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 22:31:57