diff options
| author | Matthieu Pignolet <matthieu@puffer.fish> | 2025-05-26 15:23:49 +0400 | 
|---|---|---|
| committer | Matthieu Pignolet <matthieu@puffer.fish> | 2025-05-26 15:23:49 +0400 | 
| commit | 136ccd6acdc1bbabf633efd55bfccdab4eb7049a (patch) | |
| tree | 9cc1ca85dd11938a73e32289ef35f911b03db9f9 /tasks/main.yml | |
initial commit
Diffstat (limited to 'tasks/main.yml')
| -rw-r--r-- | tasks/main.yml | 87 | 
1 files changed, 87 insertions, 0 deletions
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  | 
