120 lines
3.6 KiB
YAML
120 lines
3.6 KiB
YAML
---
|
|
- name: Try/Catch
|
|
block:
|
|
|
|
|
|
- name: Mandatory Variables set
|
|
ansible.builtin.assert:
|
|
that:
|
|
- api_client_name is defined
|
|
- api_path is defined
|
|
- api_token is defined
|
|
- api_url is defined
|
|
|
|
|
|
- name: API Facts
|
|
ansible.builtin.set_fact:
|
|
epoch: "{{ ((('%Y-%m-%d %H:%M:%S' | strftime) | string | to_datetime) - ('1970-01-01 00:00:00' | to_datetime)).total_seconds() | int }}"
|
|
expired: false
|
|
cache_filepath: >-
|
|
{{ nfc_c_path_cache }}/{{ api_path }}
|
|
{%- if api_query_string is defined -%}
|
|
_{{ ((api_query_string | split('&'))[0] | split('='))[1] | lower }}_{{ ((api_query_string | split('&'))[1] | split('='))[1] | lower }}
|
|
{%- endif -%}.json
|
|
|
|
|
|
- name: check Cache Files
|
|
ansible.builtin.stat:
|
|
path: "{{ cache_filepath }}"
|
|
register: cached_file
|
|
|
|
|
|
- name: Expire
|
|
ansible.builtin.set_fact:
|
|
expired: "{{ ((epoch | int + (nfc_c_epoch_time_offset | default(0)) | int) >= ((cached_file.stat.mtime | int) +
|
|
(nofusscomputing_phpipam_scan_agent.cache_expire_time | default(nfc_c_cache_expire_time)) | int) | int ) | bool }}"
|
|
when: cached_file.stat.exists
|
|
|
|
|
|
- name: TRACE - Cached file
|
|
ansible.builtin.debug:
|
|
msg:
|
|
- "exists: {{ cached_file.stat.exists | default('') }}"
|
|
- "mtime: {{ cached_file.stat.mtime | default(0) | int }}"
|
|
- "expire: {{ (cached_file.stat.mtime | int) + (nofusscomputing_phpipam_scan_agent.cache_expire_time | default(nfc_c_cache_expire_time)) | int }}"
|
|
- "epoch: {{ (epoch | int + (nfc_c_epoch_time_offset | default(0)) | int) | int }} [{{
|
|
(nofusscomputing_phpipam_scan_agent.cache_expire_time | default(nfc_c_cache_expire_time)) }}]"
|
|
- "epoch: {{ epoch }}"
|
|
- "expired: {{ expired }}"
|
|
when: cached_file.stat.exists
|
|
|
|
- name: Expire Cache
|
|
ansible.builtin.file:
|
|
path: "{{ cached_file.stat.path }}"
|
|
state: absent
|
|
when: >
|
|
expired
|
|
and
|
|
cached_file.stat.exists
|
|
|
|
|
|
- name: >
|
|
PHPIPAM API Call - {{ api_path }}{%- if api_query_string is defined -%}
|
|
/?{{ api_query_string }}
|
|
{%- endif %}
|
|
ansible.builtin.uri:
|
|
url: >-
|
|
{{ api_url }}/api/{{ api_client_name }}/{{ api_path }}
|
|
{%- if api_query_string is defined -%}
|
|
/?{{ api_query_string }}
|
|
{%- endif %}
|
|
headers:
|
|
token: "{{ api_token }}"
|
|
return_content: true
|
|
status_code:
|
|
- 200
|
|
- 404
|
|
validate_certs: false
|
|
changed_when: api_call.json | default([]) | length | int > 0
|
|
no_log: true
|
|
register: api_call
|
|
when: >
|
|
(
|
|
expired
|
|
and
|
|
cached_file.stat.exists
|
|
)
|
|
or
|
|
not cached_file.stat.exists
|
|
|
|
|
|
- name: Create Cache DIR
|
|
ansible.builtin.file:
|
|
path: "{{ nfc_c_path_cache }}/{{ (api_path | split('/'))[0] }}"
|
|
state: directory
|
|
when: >
|
|
'/' in api_path
|
|
and
|
|
api_call.status | default(0) | int != 404
|
|
|
|
|
|
- name: Cache Data
|
|
ansible.builtin.copy:
|
|
content: "{{ api_call.json.data | to_nice_json }}"
|
|
dest: "{{ cache_filepath }}"
|
|
when: >
|
|
(
|
|
expired
|
|
and
|
|
cached_file.stat.exists
|
|
)
|
|
or
|
|
not cached_file.stat.exists
|
|
and
|
|
api_call.status | default(0) | int != 404
|
|
|
|
- name: check Cache Files
|
|
ansible.builtin.stat:
|
|
path: "{{ cache_filepath }}"
|
|
register: cached_file
|