-1

I'm trying to receive a list of all my server names and their IPs on Hetzner using ansible and hcloud module however I'm receiving the following error;

ERROR! couldn't resolve module/action 'hcloud'. This often indicates a misspelling, missing collection, or incorrect module path.

The error appears to be in '/home/melvmagr/repos/ansible/server-content/server-content.yml': line 8, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
    - name: List servers
      ^ here

Here is my YAML file;

---
- name: List Hetzner server names and IP addresses
  hosts: servername_example
  gather_facts: false
  vars:
    hcloud_token: "MY_HETZNER_API_TOKEN"
  tasks:
    - name: List servers
      hcloud:
        api_token: "MY_HETZNER_API_TOKEN"
        state: present
        command: server_list
      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.servers }}"

More info that could prove to be helpful:

❯ ansible --version
ansible [core 2.12.10]
  config file = /home/melvmagr/repos/ansible/ansible.cfg
  configured module search path = ['/home/melvmagr/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/melvmagr/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0]
  jinja version = 2.10.1
  libyaml = True

This is my ansible.cfg file;

[defaults]
inventory=./inventories
host_key_checking = False
log_path=/var/tmp/ansible_history
timeout=300

I've also installed the following collections;

ansible-galaxy collection install hetzner.hcloud

❯ ls ~/.ansible/collections/ansible_collections/hetzner/hcloud/
CHANGELOG.rst  COPYING  FILES.json  MANIFEST.json  README.md  changelogs  meta  plugins  tests

Any help would be greatly appreciated. Thank you!

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • 1
    There is no `hcloud` task, there is a [`hcloud_server_info`](https://docs.ansible.com/ansible/latest/collections/hetzner/hcloud/hcloud_server_info_module.html#ansible-collections-hetzner-hcloud-hcloud-server-info-module) to gather info on servers though – β.εηοιτ.βε Jan 05 '23 at 12:40
  • @β.εηοιτ.βε Thanks a lot for that. I've been trying to use the hcloud_server_info as you specified (Am a bit of a beginner) and have altered the yaml file https://prnt.sc/JgZaRAAUpNAW (I hid the hetzner API token) but getting the error; **An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'ip'** haven't found much help online unfortunately. What IP is he referring to there? – Melvin Magro Jan 06 '23 at 14:04
  • `item.public_net.ipv4.ip` > threre is no `ipv4` dictionnary in the return of this module, there is a [`ipv4_address` string](https://docs.ansible.com/ansible/latest/collections/hetzner/hcloud/hcloud_server_info_module.html#return-hcloud_server_info/ipv4_address), though. When unsure, do a `- debug: var=my_registred_variable` to see all there is in the return, or use the documentation. – β.εηοιτ.βε Jan 06 '23 at 16:56
  • Hello @β.εηοιτ.βε Thanks very much for your help! I managed to find a much easier way to get the info I needed without complicating it :) Will put it in answer form so that others may be able to see it as well – Melvin Magro Jan 09 '23 at 13:42

1 Answers1

0

Managed to solve it by simplifying my playbook the following way;

---
- name: Get server information from Hetzner Cloud
  hosts: myservername
  gather_facts: false
  tasks:
  - name: Print server names and IP addresses
    debug:
      msg: "{{ inventory_hostname }} IP is {{ ansible_ssh_host }}"