--- - name: add host to omd (new) win_uri: method: POST headers: Authorization: Bearer {{ cmk_username }} {{ cmk_secret }} Accept: application/json url: '{{ cmk_api_url }}/domain-types/host_config/collections/all' body: folder: "{{ cmk_folder | default('~Unsorted') }}" host_name: '{{ inventory_hostname | upper }}' attributes: tag_criticality: prod tag_agent: cmk-agent ipaddress: "{{ cmk_host_ip | default(hostvars[inventory_hostname]['ansible_default_ipv4']['address'], true) }}" body_format: json return_content: true status_code: [200, 400] register: res_add become: false when: > cmk_add_host and cmk_major | int >= 2 and cmk_minor | int >= 2 - name: Parse result set_fact: output: '{{ res_add.content | from_json }}' when: cmk_add_host - name: Fetch etag (new) win_uri: method: GET headers: Authorization: Bearer {{ cmk_username }} {{ cmk_secret }} Accept: application/json url: "{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/api/v0/objects/host_config/{{ inventory_hostname | upper }}" return_content: true register: res_host_exists become: false when: > cmk_add_host and (cmk_major | int >= 2 and cmk_minor | int >= 2) and (res_add.status == 400 and output.fields.host_name | first | regex_search('already exists.$')) # - name: Host exists fetch etag (new) # noqa command-instead-of-module # shell: # cmd: | # curl \ # -G \ # -i \ # --request GET \ # --write-out "\nxxx-status_code=%{http_code}\n" \ # --header "Authorization: Bearer {{ cmk_username }} {{ cmk_secret }}" \ # --header "Accept: application/json" \ # "{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/api/v0/objects/host_config/{{ inventory_hostname | upper }}" # register: res_host_exists # become: false # when: > # cmk_add_host and (cmk_major | int >= 2 and cmk_minor | int >= 2) and # (res_add.status == 400 and output.fields.host_name | first | regex_search('already # exists.$')) - name: Unpick response set_fact: etag: "{{ res_host_exists.stdout | regex_search('.*etag:.\"(?P.+)\".*', '\\g') | first }}" when: > cmk_add_host and (cmk_major | int >= 2 and cmk_minor | int >= 2) and (res_add.status == 400 and output.fields.host_name | first | regex_search('already exists.$')) - name: Update host as already exists? (new) win_uri: method: PUT headers: Authorization: Bearer {{ cmk_username }} {{ cmk_secret }} Accept: application/json If-Match: '{{ etag }}' url: '{{ cmk_omd_protocol }}://{{ cmk_omd_host }}/{{ cmk_omd_site }}/check_mk/api/v0/objects/host_config/{{ inventory_hostname | upper }}' body: update_attributes: ipaddress: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}" body_format: json return_content: true status_code: [200] register: res become: false delegate_to: localhost when: > cmk_add_host and (cmk_major | int >= 2 and cmk_minor | int >= 2) and (res_add.status == 400 and output.fields.host_name | first | regex_search('already exists.$')) - name: Parse result set_fact: output: '{{ res_add.content | from_json }}' when: cmk_add_host - name: cmk_discovery (new) win_uri: method: POST headers: Authorization: Bearer {{ cmk_username }} {{ cmk_secret }} Accept: application/json url: '{{ cmk_api_url }}/domain-types/service_discovery_run/actions/start/invoke' body: host_name: '{{ inventory_hostname | upper }}' mode: fix_all body_format: json status_code: [200, 302] become: false delegate_to: localhost when: > cmk_add_host and cmk_major | int >= 2 and cmk_minor | int >= 2 or cmk_force_install - name: cmk_apply (new) win_uri: method: POST headers: Authorization: Bearer {{ cmk_username }} {{ cmk_secret }} Accept: application/json If-Match: "*" url: '{{ cmk_api_url }}/domain-types/activation_run/actions/activate-changes/invoke' body: redirect: false sites: - "{{ cmk_omd_site }}" force_foreign_changes: false body_format: json status_code: 200 become: false run_once: true when: > cmk_add_host and cmk_major | int >= 2 and cmk_minor | int >= 2 or cmk_force_install