diff options
| -rw-r--r-- | defaults/main.yml | 16 | ||||
| -rw-r--r-- | tasks/main.yml | 87 |
2 files changed, 103 insertions, 0 deletions
diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..e4e083a --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,16 @@ +--- +# LibreNMS API credentials and endpoint +api_key: "" +endpoint: "" + +# Device configuration +hostname: "" +display: "" + +# SNMPv3 credentials +auth_algo: "" +auth_name: "" +auth_secret: "" + +crypto_algo: "" +crypto_secret: ""
\ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..74a17d4 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,87 @@ +--- +- name: Define common headers + ansible.builtin.set_fact: + common_headers: + X-Auth-Token: "{{ api_key }}" + Content-Type: "application/json" + +- name: Get existing device information + ansible.builtin.uri: + url: "{{ endpoint }}/api/v0/devices/{{ hostname }}" + method: GET + headers: "{{ common_headers }}" + return_content: true + status_code: [200, 404] + register: get_device + +- name: Print rest data + debug: + msg: "Rest Data: {{ get_device.json }}" + +- name: Set device state facts + ansible.builtin.set_fact: + device_data: "{{ get_device.json }}" + device_exists: "{{ get_device.json.status == 'ok' }}" + +- name: Print device exists + debug: + msg: "Exists : {{ device_exists }}" + +- name: Create the device if it doesn't exist + ansible.builtin.uri: + url: "{{ endpoint }}/api/v0/devices" + method: POST + headers: "{{ common_headers }}" + body_format: json + body: + hostname: "{{ hostname }}" + display: "{{ display }}" + snmpver: "v3" + authlevel: "authPriv" + authname: "{{ auth_name }}" + transport: "udp" + authpass: "{{ auth_secret }}" + authalgo: "{{ auth_algo }}" + cryptopass: "{{ crypto_secret }}" + cryptoalgo: "{{ crypto_algo }}" + force_add: true + status_code: 200 + when: not device_exists + +- name: Update device if it exists and differs + ansible.builtin.uri: + url: "{{ endpoint }}/api/v0/devices/{{ hostname }}" + method: PATCH + headers: "{{ common_headers }}" + body_format: json + body: + field: + - authlevel + - snmpver + - authname + - authpass + - authalgo + - cryptopass + - cryptoalgo + - display + data: + - "authPriv" + - "v3" + - "{{ auth_name }}" + - "{{ auth_secret }}" + - "{{ auth_algo }}" + - "{{ crypto_secret }}" + - "{{ crypto_algo }}" + - "{{ display }}" + status_code: 200 + when: > + device_exists and ( + device_data.devices[0].authlevel != 'authPriv' or + device_data.devices[0].snmpver != 'v3' or + device_data.devices[0].authname != auth_name or + device_data.devices[0].authpass != auth_secret or + device_data.devices[0].authalgo != auth_algo or + device_data.devices[0].cryptopass != crypto_secret or + device_data.devices[0].cryptoalgo != crypto_algo or + device_data.devices[0].display != display + )
\ No newline at end of file |
