Files
ansible-role-checkmk-agent/tasks/host-management/modern.yml
Aaron Guise c29898b542
Some checks failed
CI / lint (push) Successful in 2m1s
CI / Molecule Test (almalinux8) (push) Failing after 13s
CI / Molecule Test (almalinux9) (push) Failing after 18s
CI / release (push) Has been skipped
CI / notify (push) Has been skipped
fix: Revert changes to use local_action 🐛
2025-02-21 21:35:36 +13:00

124 lines
3.8 KiB
YAML

---
- name: add host to omd (new)
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
delegate_to: localhost
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: 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
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: Unpick response
set_fact:
etag: "{{ res_host_exists.stdout | regex_search('.*etag:.\"(?P<etag>.+)\".*', '\\g<etag>') | 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)
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)
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)
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