diff --git a/tasks/host-configure.yml b/tasks/host-configure.yml index 646aaf1..3353239 100644 --- a/tasks/host-configure.yml +++ b/tasks/host-configure.yml @@ -1,62 +1,11 @@ --- -- name: cmk_discovery - uri: - method: POST - url: '{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/webapi.py?action=discover_services&mode=refresh{{ - cmk_auth }}' - body: request={"hostname":"{{ instance_name | default(inventory_hostname) }}"} - body_format: raw - status_code: 200 - validate_certs: false - follow_redirects: true - become: false - when: cmk_add_host and output.result_code == 0 - delegate_to: localhost - tags: cmk_register -- name: cmk_apply - uri: - method: POST - url: '{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/webapi.py?action=activate_changes&mode=specific{{ - cmk_auth }}' - body: request={"sites":["{{ cmk_omd_site }}"]} - body_format: raw - status_code: 200 - validate_certs: false - follow_redirects: true - become: false - run_once: true - when: cmk_add_host and output.result_code == 0 - delegate_to: localhost - tags: cmk_register -- name: Wait some time - pause: - seconds: 10 -- name: cmk_discovery x 2 - uri: - method: POST - url: '{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/webapi.py?action=discover_services&mode=refresh{{ - cmk_auth }}' - body: request={"hostname":"{{ instance_name | default(inventory_hostname) }}"} - body_format: raw - status_code: 200 - validate_certs: false - follow_redirects: true - become: false - when: cmk_add_host and output.result_code == 0 - delegate_to: localhost - tags: cmk_register -- name: cmk_apply - uri: - method: POST - url: '{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/webapi.py?action=activate_changes&mode=specific{{ - cmk_auth }}' - body: request={"sites":["{{ cmk_omd_site }}"]} - body_format: raw - status_code: 200 - validate_certs: false - follow_redirects: true - become: false - run_once: true - when: cmk_add_host and output.result_code == 0 - delegate_to: localhost - tags: cmk_register +- include_tasks: host-configure/legacy.yml + when: > + cmk_add_host and + (cmk_major | int <= 2 and + cmk_minor | int < 2 or cmk_force_install) +- include_tasks: host-configure/modern.yml + when: > + cmk_add_host and + (cmk_major | int >= 2 and + cmk_minor | int >= 2 or cmk_force_install) diff --git a/tasks/host-configure/legacy.yml b/tasks/host-configure/legacy.yml new file mode 100644 index 0000000..227e30c --- /dev/null +++ b/tasks/host-configure/legacy.yml @@ -0,0 +1,68 @@ +--- +- name: Output contains + debug: + var: output + +- name: cmk_discovery + local_action: + module: uri + method: POST + url: '{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/webapi.py?action=discover_services&mode=refresh{{ + cmk_auth }}' + body: request={"hostname":"{{ instance_name | default(inventory_hostname) }}"} + body_format: raw + status_code: 200 + validate_certs: false + follow_redirects: true + become: false + when: cmk_add_host and output.id != "" + tags: cmk_register + +- name: cmk_apply + local_action: + module: uri + method: POST + url: '{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/webapi.py?action=activate_changes&mode=specific{{ + cmk_auth }}' + body: request={"sites":["{{ cmk_omd_site }}"]} + body_format: raw + status_code: 200 + validate_certs: false + follow_redirects: true + become: false + run_once: true + when: cmk_add_host and output.result_code == 0 + delegate_to: localhost + tags: cmk_register +- name: Wait some time + pause: + seconds: 10 +- name: cmk_discovery x 2 + local_action: + module: uri + method: POST + url: '{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/webapi.py?action=discover_services&mode=refresh{{ + cmk_auth }}' + body: request={"hostname":"{{ instance_name | default(inventory_hostname) }}"} + body_format: raw + status_code: 200 + validate_certs: false + follow_redirects: true + become: false + when: cmk_add_host and output.result_code == 0 + tags: cmk_register +- name: cmk_apply + local_action: + module: uri + method: POST + url: '{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/webapi.py?action=activate_changes&mode=specific{{ + cmk_auth }}' + body: request={"sites":["{{ cmk_omd_site }}"]} + body_format: raw + status_code: 200 + validate_certs: false + follow_redirects: true + become: false + run_once: true + when: cmk_add_host and output.result_code == 0 + tags: cmk_register diff --git a/tasks/host-configure/modern.yml b/tasks/host-configure/modern.yml new file mode 100644 index 0000000..a3ee259 --- /dev/null +++ b/tasks/host-configure/modern.yml @@ -0,0 +1,57 @@ +--- +- name: Output contains + debug: + var: output + +- name: cmk_discovery + local_action: + module: uri + method: POST + url: "{{ cmk_api_url }}/domain-types/service_discovery_run/actions/start/invoke" + headers: + Authorization: "Bearer {{ cmk_username }} {{ cmk_secret }}" + Accept: application/json + Content-Type: application/json + body_format: raw + body: | + { + "host_name": "{{ inventory_hostname | upper }}", + "mode": "fix_all" + } + status_code: 200 + validate_certs: false + follow_redirects: true + become: false + when: cmk_add_host and res_add.status != 400 and output.id != "" + tags: cmk_register + +- name: Just Waiting + pause: + seconds: 20 + +- name: cmk_apply + local_action: + module: uri + method: POST + url: '{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/api/1.0/domain-types/activation_run/actions/activate-changes/invoke' + headers: + Authorization: Bearer {{ cmk_username }} {{ cmk_secret }} + Accept: application/json + If-Match: "*" + body_format: json + body: + redirect: false + sites: + - "{{ cmk_omd_site }}" + force_foreign_changes: false + status_code: [200] + validate_certs: false + follow_redirects: true + become: false + run_once: true + when: cmk_add_host and res_add.status != 400 and output.id != "" + tags: cmk_register + +- name: Wait some time + pause: + seconds: 10