| 1 |
--- |
| 2 |
- name: "Set default ip address for zabbix_agent_ip | Windows" |
| 3 |
ansible.builtin.set_fact: |
| 4 |
zabbix_agent_ip: "{{ ansible_facts['ip_addresses'] | ansible.utils.ipaddr('private') | first }}" |
| 5 |
when: |
| 6 |
- zabbix_agent_ip is not defined |
| 7 |
- ansible_facts['os_family'] == 'Windows' |
| 8 |
|
| 9 |
- name: "Set default ip address for zabbix_agent_ip | {{ ansible_facts['os_family'] }}" |
| 10 |
ansible.builtin.set_fact: |
| 11 |
zabbix_agent_ip: "{{ ansible_facts['default_ipv4'].address | default(ansible_facts['all_ipv4_addresses'][0]) }}" |
| 12 |
when: |
| 13 |
- zabbix_agent_ip is not defined |
| 14 |
- ansible_facts['default_ipv4'].address is defined or ansible_facts['all_ipv4_addresses'] | length > 0 |
| 15 |
- ansible_facts['os_family'] != 'Windows' |
| 16 |
|
| 17 |
# Fun fact, the first set_fact that successfully defines zabbix_agent_ip |
| 18 |
# breaks the block, so no further tasks are run within the block. |
| 19 |
- name: "Detect IPv4 address" |
| 20 |
when: |
| 21 |
- zabbix_agent_ip is not defined |
| 22 |
- zabbix_agent_detect_ip |
| 23 |
- ansible_facts['os_family'] != 'Windows" |
| 24 |
block: |
| 25 |
- name: "Set first private ip address for zabbix_agent_ip" |
| 26 |
ansible.builtin.set_fact: |
| 27 |
zabbix_agent_ip: "{{ ansible_facts['all_ipv4_addresses'] | ansible.utils.ipaddr('private') | first }}" |
| 28 |
when: ansible_facts['all_ipv4_addresses'] | ansible.utils.ipaddr('private') | length > 0 |
| 29 |
|
| 30 |
- name: "Set first public ip address for zabbix_agent_ip" |
| 31 |
ansible.builtin.set_fact: |
| 32 |
zabbix_agent_ip: "{{ ansible_facts['all_ipv4_addresses'] | ansible.utils.ipaddr('public') | first }}" |
| 33 |
when: ansible_facts['all_ipv4_addresses'] | ansible.utils.ipaddr('public') | length > 0 |
| 34 |
|
| 35 |
|
| 36 |
- name: "API | Create host groups" |
| 37 |
community.zabbix.zabbix_group: |
| 38 |
host_groups: "{{ zabbix_host_groups }}" |
| 39 |
state: present |
| 40 |
when: |
| 41 |
- zabbix_api_create_hostgroup | bool |
| 42 |
register: zabbix_api_hostgroup_created |
| 43 |
until: zabbix_api_hostgroup_created is succeeded |
| 44 |
retries: 10 |
| 45 |
delegate_to: "{{ zabbix_api_server_host }}" |
| 46 |
become: false |
| 47 |
tags: |
| 48 |
- api |
| 49 |
|
| 50 |
- name: "API | Create a new host or update an existing host's info" |
| 51 |
community.zabbix.zabbix_host: |
| 52 |
host_name: "{{ zabbix_agent_hostname }}" |
| 53 |
status: "{{ zabbix_host_status }}" |
| 54 |
description: "{{ zabbix_agent_description | default(omit) }}" |
| 55 |
force: "{{ zabbix_agent_host_update }}" |
| 56 |
host_groups: "{{ zabbix_host_groups }}" |
| 57 |
interfaces: "{{ zabbix_agent_interfaces }}" |
| 58 |
inventory_mode: "{{ zabbix_agent_inventory_mode }}" |
| 59 |
inventory_zabbix: "{{ zabbix_agent_inventory_zabbix | default({}) }}" |
| 60 |
ipmi_authtype: "{{ zabbix_agent_ipmi_authtype | default(omit) }}" |
| 61 |
ipmi_password: "{{ zabbix_agent_ipmi_password| default(omit) }}" |
| 62 |
ipmi_privilege: "{{ zabbix_agent_ipmi_privilege | default(omit) }}" |
| 63 |
ipmi_username: "{{ zabbix_agent_ipmi_username | default(omit) }}" |
| 64 |
link_templates: "{{ zabbix_agent_link_templates }}" |
| 65 |
monitored_by: "{{ zabbix_agent_monitored_by | default(omit) }}" |
| 66 |
proxy: "{{ zabbix_agent_proxy }}" |
| 67 |
proxy_group: "{{ zabbix_agent_proxy_group | default(omit) }}" |
| 68 |
state: "{{ zabbix_agent_host_state }}" |
| 69 |
tls_accept: "{{ zabbix_agent_tls_config[zabbix_agent_tlsaccept | default('unencrypted')] }}" |
| 70 |
tls_connect: "{{ zabbix_agent_tls_config[zabbix_agent_tlsconnect | default('unencrypted')] }}" |
| 71 |
tls_issuer: "{{ zabbix_agent_tlsservercertissuer | default(omit) }}" |
| 72 |
tls_psk: "{{ zabbix_agent_tlspsk_secret | default(omit) }}" |
| 73 |
tls_psk_identity: "{{ zabbix_agent_tlspskidentity | default(omit) }}" |
| 74 |
tls_subject: "{{ zabbix_agent_tls_subject | default(omit) }}" |
| 75 |
tags: "{{ zabbix_agent_tags | default(omit) }}" |
| 76 |
visible_name: "{{ zabbix_agent_visible_hostname | default(zabbix_agent_hostname) }}" |
| 77 |
register: zabbix_api_host_created |
| 78 |
until: zabbix_api_host_created is succeeded |
| 79 |
retries: 10 |
| 80 |
delegate_to: "{{ zabbix_api_server_host }}" |
| 81 |
become: false |
| 82 |
changed_when: false |
| 83 |
tags: |
| 84 |
- api |
| 85 |
|
| 86 |
- name: "API | Updating host configuration with macros" |
| 87 |
community.zabbix.zabbix_hostmacro: |
| 88 |
host_name: "{{ zabbix_agent_hostname }}" |
| 89 |
macro_name: "{{ item.macro_key }}" |
| 90 |
macro_value: "{{ item.macro_value }}" |
| 91 |
macro_type: "{{ item.macro_type|default('text') }}" |
| 92 |
with_items: "{{ zabbix_agent_macros | default([]) }}" |
| 93 |
when: |
| 94 |
- item.macro_key is defined |
| 95 |
register: zabbix_api_hostmarcro_created |
| 96 |
until: zabbix_api_hostmarcro_created is succeeded |
| 97 |
retries: 10 |
| 98 |
no_log: "{{ ansible_verbosity < 3 }}" |
| 99 |
delegate_to: "{{ zabbix_api_server_host }}" |
| 100 |
become: false |
| 101 |
tags: |
| 102 |
- api |