summaryrefslogtreecommitdiff
path: root/tasks/main.yml
blob: 74a17d487f78716167eab078d12ae372abcfe192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
    )