Closed Vanav closed 1 year ago
Some modules in the ansible community galaxy are using purge*
parameters to achive this kind of task.
However, you can glue this behaviour by yourself with some basic ansible knowledge and technics.
---
- hosts: localhost
vars:
my_macros:
- macro: '{$SSLDOMAIN}'
value: 'my.domain.de'
- macro: add
value: example
tasks:
- name: Get host info
community.zabbix.zabbix_host_info:
host_name: my.domain.de
exact_match: true
register: existing
- name: preprocessing
set_fact:
existing_macros: "{{ existing.hosts[0].macros | ansible.utils.remove_keys(target=['description', 'hostid', 'hostmacroid', 'type']) }}"
- debug:
var: my_macros
- debug:
var: existing_macros
- name: macros that need to be created or updated
set_fact:
ADD_AND_UPDATE: "{{ my_macros | difference(existing_macros) | list }}"
REMOVE: "{{ existing_macros | difference(my_macros) | list }}"
- debug:
var: ADD_AND_UPDATE
- debug:
var: REMOVE
- name: add host macros for my.domain.de
with_items: "{{ ADD_AND_UPDATE }}"
community.zabbix.zabbix_hostmacro:
host_name: my.domain.de
macro_name: "{{ item.macro }}"
macro_value: "{{ item.value }}"
state: present
- name: remove host macros for my.domain.de
with_items: "{{ REMOVE }}"
community.zabbix.zabbix_hostmacro:
host_name: my.domain.de
macro_name: "{{ item.macro }}"
macro_value: "{{ item.value }}"
state: absent
result
TASK [Gathering Facts] ******************************************************************************************************************
ok: [localhost]
TASK [set zabbix variables] *************************************************************************************************************
ok: [localhost]
TASK [Get host info] ********************************************************************************************************************
ok: [localhost]
TASK [preprocessing] ********************************************************************************************************************
ok: [localhost]
TASK [debug] ****************************************************************************************************************************
ok: [localhost] => {
"my_macros": [
{
"macro": "{$SSLDOMAIN}",
"value": "my.domain.de"
},
{
"macro": "add",
"value": "example"
}
]
}
TASK [debug] ****************************************************************************************************************************
ok: [localhost] => {
"existing_macros": [
{
"macro": "{$SSLDOMAIN}",
"value": "my.domain.de"
},
{
"macro": "{$UPDATEINTERVAL}",
"value": "1m"
}
]
}
TASK [macros that need to be created or updated] ****************************************************************************************
ok: [localhost]
TASK [debug] ****************************************************************************************************************************
ok: [localhost] => {
"ADD_AND_UPDATE": [
{
"macro": "add",
"value": "example"
}
]
}
TASK [debug] ****************************************************************************************************************************
ok: [localhost] => {
"REMOVE": [
{
"macro": "{$UPDATEINTERVAL}",
"value": "1m"
}
]
}
PLAY RECAP ******************************************************************************************************************************
localhost : ok=9 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Another possibility is to define the entire host with community.zabbix.zabbix_host
incl the macros. There you can define force: true
. That should also kick out all macros that are not defined.
back to your suggestion to include a new parameter feature that does the job.
the macro parameter must be a list of elements. because a macro can have multile properties, like description
and type
.
my proposal
- name: Define host macros
zabbix_hostmacro:
host_name: my_host
macros:
- macro: VFS.FS.PUSED.MAX.WARN:/:
value: 90%
type: text
description: something or null
state: present
purge_macros: true
@markuman Thank you for pointing me to community.zabbix.zabbix_host
→ macros
. Somehow I've missed it or it is new.
Example:
zabbix_macros:
- macro: PROC.NUM.MAX
value: 350
- macro: KERNEL.OPENFILES.MAX
value: 5000
- name: "API | Create a new host using agent2 or update an existing host's info"
zabbix_host:
host_name: '{{ zabbix_agent_hostname }}'
host_groups: '{{ zabbix_host_groups }}'
link_templates: '{{ zabbix_link_templates }}'
interfaces:
- type: agent
main: 1
useip: 1
ip: '{{ ansible_host }}'
port: '{{ zabbix_agent_port }}'
macros: '{{ zabbix_macros }}'
This solves my use case, thank you.
SUMMARY
Need to have a way to define a list of macros for a host, and it should specify the exact state of macros for a host. So, macros not in the list are deleted (used template default), and macros in the list are added. Currently, there is no way to define the exact state of host macros.
ISSUE TYPE
COMPONENT NAME
community.zabbix.zabbix_hostmacro
ADDITIONAL INFORMATION
Example syntax:
The state of host macros is completely defined here: host will always have only these three macros with specified values.
Example of existing feature in another module: https://docs.ansible.com/ansible/latest/collections/community/postgresql/postgresql_membership_module.html#parameter-state