69 lines
2.2 KiB
YAML
69 lines
2.2 KiB
YAML
---
|
||
- name: Extract SNMP community string from snmpd.conf using shell
|
||
# become: true
|
||
shell: |
|
||
awk '/^com2sec\s+readonly/ {print $4}' /etc/snmp/snmpd.conf | head -1
|
||
register: snmp_comm_out
|
||
changed_when: false
|
||
|
||
- name: Set fact with SNMP community
|
||
set_fact:
|
||
snmp_community: "{{ snmp_comm_out.stdout | trim }}"
|
||
|
||
- name: Debug found community
|
||
debug:
|
||
msg: "SNMP community: {{ snmp_community }}"
|
||
|
||
# 2️⃣ Check if this host is already in Observium — runs on the Observium server
|
||
- name: Check if host is already in Observium database
|
||
become: false
|
||
delegate_to: "{{ observium_server }}"
|
||
shell: |
|
||
docker exec {{ observium_container }} bash -c '
|
||
MYHOST={{ inventory_hostname }};
|
||
echo "SELECT device_id FROM devices WHERE hostname = '\''$MYHOST'\'';" | \
|
||
mysql -u"$OBSERVIUM_DB_USER" -p"$OBSERVIUM_DB_PASS" \
|
||
-h "$OBSERVIUM_DB_HOST" "$OBSERVIUM_DB_NAME"
|
||
'
|
||
register: observium_check
|
||
changed_when: false
|
||
|
||
- name: Determine if host is already present in Observium
|
||
set_fact:
|
||
observium_device_id: >-
|
||
{{
|
||
observium_check.stdout_lines
|
||
| select('match', '^[0-9]+')
|
||
| list
|
||
| first
|
||
| default('')
|
||
}}
|
||
|
||
- name: Show if host is already present
|
||
debug:
|
||
msg: "✅ Host {{ inventory_hostname }} is already in Observium (Device ID: {{ observium_device_id }})"
|
||
when: observium_device_id != ""
|
||
|
||
# 3️⃣ Add the host to Observium if it does not exist
|
||
- name: Add host to Observium
|
||
become: false
|
||
delegate_to: "{{ observium_server }}"
|
||
shell: |
|
||
docker exec {{ observium_container }} bash -c './add_device.php {{ inventory_hostname }} {{ snmp_community }} v2c'
|
||
register: add_result
|
||
when: observium_device_id == ""
|
||
|
||
- name: Check if add_device.php output shows success
|
||
set_fact:
|
||
observium_add_success: "{{ add_result.stdout is search('Added device') }}"
|
||
when: observium_device_id == ""
|
||
|
||
- name: Show result of Observium addition
|
||
debug:
|
||
msg: >-
|
||
{% if observium_add_success %}
|
||
✅ Host {{ inventory_hostname }} was successfully added to Observium.
|
||
{% else %}
|
||
⚠️ Host {{ inventory_hostname }} was NOT added to Observium. Check manually!
|
||
{% endif %}
|
||
when: observium_device_id == "" |