0

So I'm fairly new to Ansible. I'm trying to get the ip address and hostname from my inventory:

- set_fact:
    ip_out: "{{hostvars[inventory_hostname].ansible_default_ipv4.address }}"
    host_out: "{{hostvars[inventory_hostname].inventory_hostname}}"

And then want to add it in my monitoring system through an API. I'm just not sure how to make my loop work. It works when adding one host at a time but not multiple.

- name: Add host to Check_MK site via WebAPI
  uri:
    url: '{{ cmkclient__connection_string }}?action=add_host&_username={{ cmkclient_api_user }}&_secret={{ cmkclient_api_password }}&output_format=json'
    method: 'POST'
    body: 'request={"attributes":{"alias": "Test", "ipaddress": "{{item[0]}}", "hostname": "{{item[1]}}", "create_folders": "0", "folder": "" }'
    return_content: yes
  delegate_to: localhost
  when: '"No such host" in cmkclient__host_query.json.result'
  register: cmkclient__host_add
  changed_when: (cmkclient__host_add.json is defined) and
                (cmkclient__host_add.json.result_code == 0)
  failed_when: (cmkclient__host_add.json is not defined) or
               (cmkclient__host_add.json.result_code != 0)
  with_nested:
     - "{{ip_out}}"
     - "{{host_out}}"

I get a JSON parsing error. Any ideas would be helpful.

Thanks!

  • Not sure, but looks like Instead of using loop and `item[0]` you could directly use `ansible_default_ipv4['address']` and `inventory_hostname` in the `body`. – seshadri_c Nov 02 '21 at 12:24
  • You might want to look at the brand new [Checkmk Ansible Collection](https://github.com/tribe29/ansible-collection-tribe29.checkmk). We are currently working on it as a side project and keep extending the functionality. But it should already serve your needs quite nicely. – Thorian93 May 07 '22 at 19:56

1 Answers1

0

It seems you'd like to use the IP address and hostname of hosts under the targeted group for the body of API request. Instead of delegating this task to localhost, we could have a play on localhost like:

# gather required facts from all hosts
- hosts: all
  gather_facts: false

  tasks:
    - setup:
        gather_subset: network

- hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - debug:
        msg: "ipaddress: {{ ip_address }}, hostname: {{ host_name }}"
      vars:
        ip_address: "{{ hostvars[item]['ansible_default_ipv4']['address'] }}"
        host_name: "{{ hostvars[item]['inventory_hostname'] }}"
      loop: "{{ groups['all'] }}"

I've used a debug task, but the same loop and vars can be applied to your uri task as well.

Note:

If you are running the uri task on my_group (or all) hosts, then you should be simply able to refer to the required variables directly, without delegating. In this case the task will run on each host of the group using its IP address and hostname.

body: 'request={"attributes":{"alias": "Test", "ipaddress": "{{ ansible_default_ipv4['address'] }}", "hostname": "{{ inventory_hostname }}", "create_folders": "0", "folder": "" }'
seshadri_c
  • 6,906
  • 2
  • 10
  • 24