diff --git a/inventory/inventory.yml b/inventory/inventory.yml index 29a533a..94f8a01 100644 --- a/inventory/inventory.yml +++ b/inventory/inventory.yml @@ -13,7 +13,7 @@ all: proxmox.localdomain: ansible_host: 192.168.1.162 ansible_user: root - ansible_password: Aase#1234! + ansible_password: Aase#1234! ansible_python_interpreter: /root/proxmoxer_env/bin/python vars: proxmox_host: 192.168.1.162 @@ -46,3 +46,8 @@ all: hosts: localhost-live.localdomain: ansible_host: 192.168.1.10 + ansible_ssh_pass: "Aase#1234!" + ansible_become_pass: "Aase#1234!" + zabbix_host_groups: "Linux servers" + zabbix_link_template: "Template OS Linux" + zabbix_agent_psk: "1d3b2f634a975a68df1ac903daa4d10d5b00a3d95c33463cffb402f5e39be75f" diff --git a/roles/zabbix_agent/defaults/main.yml b/roles/zabbix_agent/defaults/main.yml index 908ebf7..b3474a1 100644 --- a/roles/zabbix_agent/defaults/main.yml +++ b/roles/zabbix_agent/defaults/main.yml @@ -1,12 +1,6 @@ ---- -# defaults file for zabbix_agent -zabbix_server_ip: "192.168.1.24" -zabbix_psk_file: "/etc/zabbix/zabbix_agentd.psk" -zabbix_agent_conf: "/etc/zabbix/zabbix_agentd.conf" -zabbix_log_file: "/var/log/zabbix/zabbix_agentd.log" -zabbix_pid_file: "/var/run/zabbix/zabbix_agentd.pid" -zabbix_include_dir: "/etc/zabbix/zabbix_agentd.d" -zabbix_host_groups: [] -zabbix_link_template: [] -zabbix_port: 10050 -zabbix_proxy: null +zabbix_server: "192.168.1.24" +zabbix_server_url: "http://192.168.1.24/api_jsonrpc.php" +zabbix_login_user: "Admin" +zabbix_login_password: "zabbix" +zabbix_host_group: "2" # Change this to the correct group ID for your setup +zabbix_template_id: "10001" # Change this to the correct template ID for your setup diff --git a/roles/zabbix_agent/tasks/main.yml b/roles/zabbix_agent/tasks/main.yml index a3e960e..f8ccaf7 100644 --- a/roles/zabbix_agent/tasks/main.yml +++ b/roles/zabbix_agent/tasks/main.yml @@ -1,92 +1,90 @@ --- -# tasks file for zabbix-agent - -# Update apt cache - name: Update apt cache apt: - update_cache: yes + update_cache: true + become: true -# Uninstall Zabbix agent (apt version) -- name: Uninstall Zabbix agent - apt: - name: zabbix-agent - state: absent - ignore_errors: true - -# Install Zabbix agent (apt version) - name: Install Zabbix agent apt: name: zabbix-agent state: present + become: true -# Generate Zabbix PSK -- name: Generate Zabbix psk - command: openssl rand -hex 32 - register: generated_psk - -# Write the PSK to a file -- name: Create Zabbix PSK file - copy: - content: "{{ generated_psk.stdout }}" - dest: "{{ zabbix_psk_file }}" - owner: zabbix - mode: '0600' - -# Register Zabbix agent PSK -- name: Register Zabbix agent PSK - set_fact: - zabbix_psk: "{{ generated_psk.stdout }}" - -# Ensure the zabbix-agent configuration is correct -- name: Configure Zabbix agent - template: - src: zabbix-agent.conf.j2 - dest: "{{ zabbix_agent_conf }}" - owner: zabbix - mode: '0644' - notify: - - restart zabbix-agent - -# Ensure that include directory is configured -- name: Ensure that /etc/zabbix/zabbix_agentd.d is included in conf - ini_file: - path: "{{ zabbix_agent_conf }}" - section: "" - option: Include - value: "{{ zabbix_include_dir }}/*.conf" - backup: true - notify: - - restart zabbix-agent - -# Create a new host or update an existing host's info -- name: Create a new host or update an existing host's info - community.zabbix.zabbix_host: - server_url: "https://{{ zabbix_server_ip }}" - login_user: "Admin" # Change as per your Zabbix server configuration - login_password: "zabbix" # Change as per your Zabbix server configuration - host_name: "{{ inventory_hostname }}" - visible_name: "{{ inventory_hostname }}" - groups: - - "{{ zabbix_host_groups }}" - templates: - - "{{ zabbix_link_template }}" - status: 0 - interfaces: - - type: 1 - main: 1 - useip: 1 - ip: "{{ ansible_default_ipv4.address }}" - dns: "{{ inventory_hostname }}" - port: "{{ zabbix_port | default(10050) }}" - tls_connect: 2 - tls_accept: 2 - tls_psk_identity: "PSK-{{ inventory_hostname }}" - tls_psk: "{{ zabbix_psk }}" - state: present - become: false - -# Enable Zabbix on Reboot -- name: Enable Zabbix on Reboot +- name: Ensure Zabbix agent is running and enabled service: name: zabbix-agent - enabled: yes + state: started + enabled: true + become: true + +- name: Configure Zabbix agent + template: + src: zabbix_agentd.conf.j2 + dest: /etc/zabbix/zabbix_agentd.conf + become: true + +- name: Generate PSK for Zabbix agent + shell: echo -n "4e616d656461706c617965722072616e646f6d6b65792067656e657261746564" > /etc/zabbix/zabbix_agentd.psk + become: true + +- name: Restart Zabbix agent + service: + name: zabbix-agent + state: restarted + become: true + +- name: Authenticate with Zabbix API + uri: + url: "http://192.168.1.24/api_jsonrpc.php" + method: POST + body_format: json + headers: + Content-Type: "application/json" + body: + jsonrpc: "2.0" + method: "user.login" + params: + username: "Admin" + password: "zabbix" + id: 1 + register: auth_response + delegate_to: localhost + +- name: Create or update a host in Zabbix + uri: + url: "http://192.168.1.24/api_jsonrpc.php" + method: POST + body_format: json + headers: + Content-Type: "application/json" + body: + jsonrpc: "2.0" + method: "host.create" + params: + host: "{{ inventory_hostname }}" + interfaces: + - type: 1 + main: 1 + useip: 1 + ip: "{{ ansible_default_ipv4.address }}" + dns: "" + port: "10050" + groups: + - groupid: "2" # Group ID for "Linux servers" + templates: + - templateid: "10001" # Template ID for "Template OS Linux" + tls_connect: 2 + tls_accept: 2 + tls_psk_identity: "PSK-{{ inventory_hostname }}" + tls_psk: "4e616d656461706c617965722072616e646f6d6b65792067656e657261746564" # Example PSK + auth: "{{ auth_response.json.result }}" + id: 1 + register: zabbix_host_response + delegate_to: localhost + when: auth_response.json.result is defined + +- name: Check Zabbix agent status + service: + name: zabbix-agent + state: started + become: true diff --git a/roles/zabbix_agent/templates/zabbix-agent.conf.j2 b/roles/zabbix_agent/templates/zabbix-agent.conf.j2 deleted file mode 100644 index 61e4b7c..0000000 --- a/roles/zabbix_agent/templates/zabbix-agent.conf.j2 +++ /dev/null @@ -1,10 +0,0 @@ -Server={{ zabbix_server_ip }} -TLSConnect=psk -TLSAccept=psk -TLSPSKIdentity=PSK-{{ inventory_hostname }} -TLSPSKFile={{ zabbix_psk_file }} - -# Additional configuration options -LogFile={{ zabbix_log_file }} -PidFile={{ zabbix_pid_file }} -Include={{ zabbix_include_dir }}/*.conf diff --git a/roles/zabbix_agent/templates/zabbix_agentd.conf.j2 b/roles/zabbix_agent/templates/zabbix_agentd.conf.j2 new file mode 100644 index 0000000..be5bc5a --- /dev/null +++ b/roles/zabbix_agent/templates/zabbix_agentd.conf.j2 @@ -0,0 +1,13 @@ +# Zabbix agent configuration file + +PidFile=/var/run/zabbix/zabbix_agentd.pid +LogFile=/var/log/zabbix/zabbix_agentd.log +LogFileSize=0 +Server=192.168.1.24 +ServerActive=192.168.1.24 +Hostname={{ inventory_hostname }} +Include=/etc/zabbix/zabbix_agentd.d/*.conf +TLSConnect=psk +TLSAccept=psk +TLSPSKIdentity=PSK-{{ inventory_hostname }} +TLSPSKFile=/etc/zabbix/zabbix_agentd.psk diff --git a/vault.yml b/vault.yml index af77879..e8203ec 100644 --- a/vault.yml +++ b/vault.yml @@ -1,27 +1,30 @@ $ANSIBLE_VAULT;1.1;AES256 -33386562613236623035653666303166393132653532653937653930383937303438646633376230 -6535666430376161373438356436323265336366663363610a636365623636343932666661373430 -65333131343533646338396530303165333063663536313639373537333766323062326631653265 -6261663032323130350a636531663565336139663936663266313737613539613438656363623061 -31663465316233353764373561323132636663313036396133366164613765626536363863316333 -62343834383164316666303937343035363630353737363237333739663563366235336330633261 -66396563616639626665333332363634633438613231613663353339373336626265646665366536 -33366532353733633562323237646166303831323763653465326636636461616237306230303634 -65363266373266303166333238633532336234343039656164636639343064346330633438613639 -33663733316134396266306132303665373461316539326465633265366637356233316531616639 -64613536343266336436613662623531663665333561623135626338656435653739303564613537 -35316636343466383534313165396630323363373466633334333337353762306535303332376337 -31306665313736396330663737383836643331336539373134363430663362643332363261373733 -36326163336631303430313739656332633065346463393639656134633861393336393263623631 -63633230626331626461386131336237353963323735366233623833313236616466353665643539 -30306337303236323664646135303663343538623462623637366635373335373363303638326237 -31326134623233323565306230356634313565333263363230333538646139643231633761633136 -38376164363933373461333237633631646666343539363963383030343434323965336232356263 -31303231626666333833356634313433343332643063303731316362366361393132656437346534 -33613637376630616664373939343831363231613939303064383733663335666135643735383864 -34336439613237343031393862373166313833376366376464636237376333653838396331356531 -61353963656466626135323666306430383531313439376265626132656565366639313237633338 -62353165623833393631646530346266376432313935393863356435633861653038303363323537 -38343231353062643866336265646431353262633834323366353764373439666134656338626266 -61343364313737386666643565393237303464306436353566393535623835653961376536386638 -6235343135306666393230653431303661376663663030356563 +37396134343831373366656239333433393164656337666531626564343733326662303631653366 +3262353332666436383665323939313866653934663730390a396132383264613434366664616566 +37393530336637313933326565346438356166333534393434343534613531653231313437396431 +6137646361663135340a353335623235343732303330636334646335636261326231363962303565 +66613765666363663164613566353664353033656663326365306335343962393664356263653564 +64316166393164616163663431643832303064323064363037313534333638376330653464343664 +30313335666162363864616466626265353130636561626265353665666234333666393962393937 +63353464333431643834323532353465306339343839373934333732356163356261633265653565 +39393933363365373134663835623063636361376366343765366236316561336362633232333936 +36353264613866373531393335346637386461643337363365326631343433333138366331336564 +39316233323739373233343666303335626265623965313831373436623965366365613363303331 +62616630346136303238623161613938653466393365663530663439353862376232316433643837 +35383931666263633836326530393364373831306238373139316462323163303134653437323564 +39633038376566303161353361663764623662623561663730633363656265333732396539663836 +34386438393837656139643430653166323866353834323662663632643833353333626336656334 +61343532313665353939353139346563636539343964316632353362623334656537623036373631 +64666633653365373139633963373563306335303764396664396663383764666463383436666138 +33643037636430366439666463653339373239383733326432626366363730326631376133353065 +38303331613135613563346131363238333563663231646663343762376263623935663264303661 +38343932366161323132373964373566383036316532383562363365666538386133386638363837 +61633465646561653437653839323962363533333565653434633938333835663162643634323235 +37663031376235646162656538613339303131646631663465396163366237666462353461323730 +62616162393735326137316634353033303937313834313432643561343962366434316363376232 +36626235303630643866636661326532653764656437373736376430303834613861663836323433 +61643933633331316134326666663031656131353232336266656634373639393136666632353235 +39383730633634343561386436356636303339633463373463373032323739333338346562653138 +34613763303538326637383239383638386632343730633934383365613034316631393763646137 +36343561303639666663313233383436373464643233336262633437623266323263333062323732 +3632 diff --git a/zabbix-agent.yml b/zabbix-agent.yml index 931642f..1e2f3bf 100644 --- a/zabbix-agent.yml +++ b/zabbix-agent.yml @@ -1,6 +1,6 @@ --- -- name: Configure Zabbix Agent +- name: Ensure Zabbix agent is installed and configured hosts: all - become: true + gather_facts: yes roles: - zabbix_agent diff --git a/zabbix-host.yml b/zabbix-host.yml new file mode 100644 index 0000000..6996d63 --- /dev/null +++ b/zabbix-host.yml @@ -0,0 +1,57 @@ +--- +- name: Create or update a Zabbix host + hosts: all + gather_facts: yes + tasks: + - name: Authenticate with Zabbix API + uri: + url: "http://192.168.1.24/api_jsonrpc.php" + method: POST + body_format: json + headers: + Content-Type: "application/json" + body: + jsonrpc: "2.0" + method: "user.login" + params: + username: "Admin" + password: "zabbix" + id: 1 + register: auth_response + delegate_to: localhost + + - name: Debug auth response + debug: + var: auth_response + + - name: Create or update a host in Zabbix + uri: + url: "http://192.168.1.24/api_jsonrpc.php" + method: POST + body_format: json + headers: + Content-Type: "application/json" + body: + jsonrpc: "2.0" + method: "host.create" + params: + host: "{{ inventory_hostname }}" + interfaces: + - type: 1 + main: 1 + useip: 1 + ip: "{{ ansible_default_ipv4.address }}" + dns: "" + port: "10050" + groups: + - groupid: "2" # Group ID for "Linux servers" + templates: + - templateid: "10001" # Template ID for "Template OS Linux" + tls_connect: 2 + tls_accept: 2 + tls_psk_identity: "PSK-{{ inventory_hostname }}" + tls_psk: "4e616d656461706c617965722072616e646f6d6b65792067656e657261746564" # Example PSK + auth: "{{ auth_response.json.result }}" + id: 1 + delegate_to: localhost + when: auth_response.json.result is defined