|
| 1 | +--- |
| 2 | +- name: Get existing adlists |
| 3 | + ansible.builtin.uri: |
| 4 | + url: "{{ pihole.api_url }}/api/lists/adlist" |
| 5 | + method: GET |
| 6 | + headers: |
| 7 | + X-FTL-SID: "{{ pihole.api_key }}" |
| 8 | + return_content: true |
| 9 | + register: existing_adlists |
| 10 | + |
| 11 | +- name: Create lookup dict for existing adlists |
| 12 | + ansible.builtin.set_fact: |
| 13 | + existing_adlists_dict: "{{ dict(existing_adlists.json.adlists | default([]) | |
| 14 | + map(attribute='address') | |
| 15 | + zip(existing_adlists.json.adlists | default([]))) }}" |
| 16 | + |
| 17 | +- name: Create lookup dict for desired adlists |
| 18 | + ansible.builtin.set_fact: |
| 19 | + desired_adlists_dict: "{{ dict(pihole.adlists | |
| 20 | + map(attribute='url') | |
| 21 | + zip(pihole.adlists)) }}" |
| 22 | + |
| 23 | +- name: Get list of existing adlist URLs |
| 24 | + ansible.builtin.set_fact: |
| 25 | + existing_adlist_urls: "{{ existing_adlists.json.adlists | default([]) | map(attribute='address') | list }}" |
| 26 | + |
| 27 | +- name: Get list of desired adlist URLs |
| 28 | + ansible.builtin.set_fact: |
| 29 | + desired_adlist_urls: "{{ pihole.adlists | map(attribute='url') | list }}" |
| 30 | + |
| 31 | +- name: Identify adlists to add |
| 32 | + ansible.builtin.set_fact: |
| 33 | + adlists_to_add: "{{ desired_adlist_urls | difference(existing_adlist_urls) }}" |
| 34 | + |
| 35 | +- name: Identify adlists to remove |
| 36 | + ansible.builtin.set_fact: |
| 37 | + adlists_to_remove: "{{ existing_adlist_urls | difference(desired_adlist_urls) }}" |
| 38 | + |
| 39 | +- name: Identify adlists to update |
| 40 | + ansible.builtin.set_fact: |
| 41 | + adlists_to_update: [] |
| 42 | + |
| 43 | +- name: Check for adlists needing updates |
| 44 | + ansible.builtin.set_fact: |
| 45 | + adlists_to_update: "{{ adlists_to_update + [item] }}" |
| 46 | + loop: "{{ desired_adlist_urls | intersect(existing_adlist_urls) }}" |
| 47 | + when: |
| 48 | + - existing_adlists_dict[item].enabled != (desired_adlists_dict[item].enabled | default(true) | int) or |
| 49 | + existing_adlists_dict[item].comment != (desired_adlists_dict[item].comment | default('')) |
| 50 | + |
| 51 | +- name: Add new adlists |
| 52 | + ansible.builtin.uri: |
| 53 | + url: "{{ pihole.api_url }}/api/lists/adlist" |
| 54 | + method: POST |
| 55 | + headers: |
| 56 | + X-FTL-SID: "{{ pihole.api_key }}" |
| 57 | + Content-Type: "application/json" |
| 58 | + body_format: json |
| 59 | + body: |
| 60 | + address: "{{ desired_adlists_dict[item].url }}" |
| 61 | + enabled: "{{ desired_adlists_dict[item].enabled | default(true) }}" |
| 62 | + comment: "{{ desired_adlists_dict[item].comment | default('') }}" |
| 63 | + status_code: [200, 201] |
| 64 | + loop: "{{ adlists_to_add }}" |
| 65 | + when: adlists_to_add | length > 0 |
| 66 | + register: adlists_add_result |
| 67 | + |
| 68 | +- name: Update existing adlists |
| 69 | + ansible.builtin.uri: |
| 70 | + url: "{{ pihole.api_url }}/api/lists/adlist/{{ existing_adlists_dict[item].id }}" |
| 71 | + method: PUT |
| 72 | + headers: |
| 73 | + X-FTL-SID: "{{ pihole.api_key }}" |
| 74 | + Content-Type: "application/json" |
| 75 | + body_format: json |
| 76 | + body: |
| 77 | + enabled: "{{ desired_adlists_dict[item].enabled | default(true) }}" |
| 78 | + comment: "{{ desired_adlists_dict[item].comment | default('') }}" |
| 79 | + status_code: [200, 204] |
| 80 | + loop: "{{ adlists_to_update }}" |
| 81 | + when: adlists_to_update | length > 0 |
| 82 | + register: adlists_update_result |
| 83 | + |
| 84 | +- name: Remove obsolete adlists |
| 85 | + ansible.builtin.uri: |
| 86 | + url: "{{ pihole.api_url }}/api/lists/adlist/{{ existing_adlists_dict[item].id }}" |
| 87 | + method: DELETE |
| 88 | + headers: |
| 89 | + X-FTL-SID: "{{ pihole.api_key }}" |
| 90 | + status_code: [200, 204] |
| 91 | + loop: "{{ adlists_to_remove }}" |
| 92 | + when: adlists_to_remove | length > 0 |
| 93 | + register: adlists_remove_result |
| 94 | + |
| 95 | +- name: Set flag for gravity update |
| 96 | + ansible.builtin.set_fact: |
| 97 | + adlists_changed: true |
| 98 | + when: (adlists_to_add | length > 0) or (adlists_to_update | length > 0) or (adlists_to_remove | length > 0) |
| 99 | + |
| 100 | +- name: Display adlists changes summary |
| 101 | + ansible.builtin.debug: |
| 102 | + msg: |
| 103 | + - "Adlists added: {{ adlists_to_add | length }}" |
| 104 | + - "Adlists updated: {{ adlists_to_update | length }}" |
| 105 | + - "Adlists removed: {{ adlists_to_remove | length }}" |
| 106 | + when: (adlists_to_add | length > 0) or (adlists_to_update | length > 0) or (adlists_to_remove | length > 0) |
0 commit comments