From a91255824ed2b1a7f6510b3fef1983a010d9db95 Mon Sep 17 00:00:00 2001
From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com>
Date: Thu, 20 Apr 2023 06:34:03 +0200
Subject: [PATCH] [PR #6286/76dd465e backport][stable-5] icinga2_host: make use
 of templates and template vars (#6374)

icinga2_host: make use of templates and template vars (#6286)

* icinga2_host: make use of templates, append vars instead of replacing all vars array.

* Initialize `template` variable. Add changelog fragment.

* Update changelogs/fragments/6286-icinga2_host-template-and-template-vars.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 76dd465e082a8b0b7af597fbfdcde0852292887a)

Co-authored-by: yoannlr <32494673+yoannlr@users.noreply.github.com>
---
 ...286-icinga2_host-template-and-template-vars.yml |  2 ++
 plugins/modules/monitoring/icinga2_host.py         | 14 ++++++--------
 2 files changed, 8 insertions(+), 8 deletions(-)
 create mode 100644 changelogs/fragments/6286-icinga2_host-template-and-template-vars.yml

diff --git a/changelogs/fragments/6286-icinga2_host-template-and-template-vars.yml b/changelogs/fragments/6286-icinga2_host-template-and-template-vars.yml
new file mode 100644
index 0000000000..7ddeea37f7
--- /dev/null
+++ b/changelogs/fragments/6286-icinga2_host-template-and-template-vars.yml
@@ -0,0 +1,2 @@
+bugfixes:
+  - "icinga2_host - fix the data structure sent to Icinga to make use of host templates and template vars (https://github.com/ansible-collections/community.general/pull/6286)."
diff --git a/plugins/modules/monitoring/icinga2_host.py b/plugins/modules/monitoring/icinga2_host.py
index 0f4e2b26a0..ee357e896f 100644
--- a/plugins/modules/monitoring/icinga2_host.py
+++ b/plugins/modules/monitoring/icinga2_host.py
@@ -250,9 +250,9 @@ def main():
     state = module.params["state"]
     name = module.params["name"]
     zone = module.params["zone"]
-    template = [name]
+    template = []
     if module.params["template"]:
-        template.append(module.params["template"])
+        template = [module.params["template"]]
     check_command = module.params["check_command"]
     ip = module.params["ip"]
     display_name = module.params["display_name"]
@@ -267,20 +267,18 @@ def main():
         module.fail_json(msg="unable to connect to Icinga. Exception message: %s" % (e))
 
     data = {
+        'templates': template,
         'attrs': {
             'address': ip,
             'display_name': display_name,
             'check_command': check_command,
             'zone': zone,
-            'vars': {
-                'made_by': "ansible",
-            },
-            'templates': template,
+            'vars.made_by': "ansible"
         }
     }
 
-    if variables:
-        data['attrs']['vars'].update(variables)
+    for key, value in variables.items():
+        data['attrs']['vars.' + key] = value
 
     changed = False
     if icinga.exists(name):