--- - 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: Set device state facts ansible.builtin.set_fact: device_data: "{{ get_device.json }}" device_exists: "{{ get_device.json.status == 'ok' }}" - 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 )