0
votes
   "msg": {
        "changed": false,
        "failed": false,
        "hosts": [
            {
                "available": "2",
                "description": "",
                "disable_until": "11111",
                "error": "Received empty response from Zabbix Agent at [172.37.11.70]. Assuming that agent dropped connection because of access permissions.",
                "errors_from": "1613663584",
                "flags": "0",
                "host": "i-0cff88b809xxxxx",
                "hostid": "45314",
                "hostinterfaces": [
                    {
                        "bulk": "1",
                        "dns": "",
                        "hostid": "45314",
                        "interfaceid": "35204",
                        "ip": "172.37.11.70",
                        "main": "1",
                        "port": "10050",
                        "type": "1",
                        "useip": "1"
                    }
                ],


- name: Read JSON file (can also be a variable).
  debug:
    msg: "{{ lookup('file', '/opt/plt/zabbix-host-data.json') }}"
  register: zabbix_json
  tags:
    - zabbix

registering the above output- name: Get Zabbix Host ID value.

  set_fact:
      zabbix_hostid_value: "{{ zabbix_json.hosts[0].hostid }}"
  tags:
    - zabbix

trying to query it for the value of the hostid but getting errors

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'hosts'\n\nThe error appears to be in '/root/ALT-Applications/build_config/ansible/roles/zabbix_agent/tasks/remoteserver-addhost.yml': line 96, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Get Zabbix Host ID value.\n ^ here\n"}

any help appreciated

1

1 Answers

0
votes

This seems to be a FAQ recently, but combining the debug: task and the register: keyword do not work together, at least not in any way that's helpful.

You can change your set_fact: invocation just slightly in order to make it behave as you expect:

- set_fact:
      zabbix_hostid_value: "{{ zabbix_json.hosts[0].hostid }}"
  tags:
    - zabbix
  vars:
    zabbix_json: "{{ lookup('file', '/opt/plt/zabbix-host-data.json') }}"