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!