Skip to content

Commit

Permalink
Merge pull request #596 from Checkmk/fix/host-initial-folder
Browse files Browse the repository at this point in the history
Fix host being always initially created in main folder.
  • Loading branch information
robin-checkmk authored May 1, 2024
2 parents e185724 + 2001daf commit b5d61ca
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/fix_host_initial_folder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Host module - Fix hosts being always created in the main directory.
9 changes: 2 additions & 7 deletions plugins/modules/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,7 @@ def __init__(self, module):
# Get the current host from the API and set some parameters
self._get_current()

if self.state == "present":
if (
self.params.get("folder")
and self.current["folder"] != self.params["folder"]
):
self.desired["folder"] = self.params["folder"]
self.desired["folder"] = self.params["folder"]

if self.params.get("nodes"):
self.desired["nodes"] = self.params.get("nodes")
Expand Down Expand Up @@ -423,7 +418,7 @@ def _build_modify_cluster_endpoint(self):
return HostEndpoints.modify_cluster % self.desired["host_name"]

def _detect_changes_folder(self):
current_folder = self.current.get("folder")
current_folder = self.current.get("folder", "/")
desired_folder = self.desired.get("folder")
changes = []

Expand Down
48 changes: 32 additions & 16 deletions tests/integration/targets/host/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
attributes:
site: "{{ outer_item.site }}"
ipaddress: 127.0.0.1
Expand All @@ -40,14 +40,30 @@
delegate_to: localhost
run_once: true # noqa run-once[task]

- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Verify host folder."
ansible.builtin.assert:
that: "(item.folder | default('/')) == extensions.folder"
vars:
extensions: "{{ lookup('checkmk.general.host',
item.name,
server_url=checkmk_var_server_url,
site=outer_item.site,
validate_certs=False,
automation_user=checkmk_var_automation_user,
automation_secret=checkmk_var_automation_secret)
}}"
delegate_to: localhost
run_once: true # noqa run-once[task]
loop: "{{ checkmk_hosts }}"

- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Delete hosts."
host:
server_url: "{{ checkmk_var_server_url }}"
site: "{{ outer_item.site }}"
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
state: "absent"
delegate_to: localhost
run_once: true # noqa run-once[task]
Expand Down Expand Up @@ -134,7 +150,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
state: "absent"
delegate_to: localhost
run_once: true # noqa run-once[task]
Expand All @@ -160,7 +176,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
attributes:
site: "{{ outer_item.site }}"
ipaddress: 127.0.0.1
Expand Down Expand Up @@ -188,7 +204,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
nodes: "{{ item.nodes }}"
attributes:
site: "{{ outer_item.site }}"
Expand Down Expand Up @@ -217,7 +233,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
nodes: "{{ item.nodes }}"
attributes:
site: "{{ outer_item.site }}"
Expand All @@ -234,7 +250,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ first.name }}"
folder: "{{ first.folder }}"
folder: "{{ first.folder | default(omit) }}"
nodes: "{{ second.nodes }}"
attributes:
site: "{{ outer_item.site }}"
Expand Down Expand Up @@ -265,7 +281,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ first.name }}"
folder: "{{ first.folder }}"
folder: "{{ first.folder | default(omit) }}"
nodes: "{{ second.nodes }}"
attributes:
site: "{{ outer_item.site }}"
Expand All @@ -284,7 +300,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
add_nodes: "{{ item.add_nodes }}"
attributes:
site: "{{ outer_item.site }}"
Expand Down Expand Up @@ -313,7 +329,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
add_nodes: "{{ item.add_nodes }}"
attributes:
site: "{{ outer_item.site }}"
Expand Down Expand Up @@ -342,7 +358,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
remove_nodes: "{{ item.remove_nodes }}"
attributes:
site: "{{ outer_item.site }}"
Expand Down Expand Up @@ -371,7 +387,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
remove_nodes: "{{ item.remove_nodes }}"
attributes:
site: "{{ outer_item.site }}"
Expand Down Expand Up @@ -401,7 +417,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
state: "absent"
delegate_to: localhost
run_once: true # noqa run-once[task]
Expand All @@ -426,7 +442,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
state: "absent"
delegate_to: localhost
run_once: true # noqa run-once[task]
Expand Down Expand Up @@ -506,7 +522,7 @@
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
nodes: "{{ item.nodes }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
state: "absent"
delegate_to: localhost
run_once: true # noqa run-once[task]
Expand All @@ -519,7 +535,7 @@
automation_user: "{{ checkmk_var_automation_user }}"
automation_secret: "{{ checkmk_var_automation_secret }}"
name: "{{ item.name }}"
folder: "{{ item.folder }}"
folder: "{{ item.folder | default(omit) }}"
state: "absent"
delegate_to: localhost
run_once: true # noqa run-once[task]
Expand Down
12 changes: 9 additions & 3 deletions tests/integration/targets/host/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ test_sites:
- version: "2.1.0p41"
edition: "cre"
site: "old_cre"
- version: "2.0.0p39"
edition: "cre"
site: "ancient_cre"
# Temporarily disable due to #596 until the permanent change to remove it lands.
# - version: "2.0.0p39"
# edition: "cre"
# site: "ancient_cre"

checkmk_var_folders:
- path: /foo
Expand All @@ -24,6 +25,7 @@ checkmk_var_folders:
name: Digital

checkmk_hosts:
- name: test0.tld
- name: test1.tld
folder: "/"
- name: test2.tld
Expand All @@ -36,6 +38,10 @@ checkmk_hosts:
folder: "/foo/bar1"

checkmk_cluster_hosts:
- name: cluster_test0.tld
nodes:
- "test0.tld"
- "test4.tld"
- name: cluster_test1.tld
folder: "/"
nodes:
Expand Down

0 comments on commit b5d61ca

Please sign in to comment.