Files
nfc_glpi/tasks/api/search/item_id.yaml

123 lines
3.2 KiB
YAML

---
- name: Find Item ID
ansible.builtin.uri:
url: "{{ http_protocol | default('https') | string }}://{{ glpi.host }}/apirest.php/{{ item.api_path }}{% if item.sub_path is defined %}/{{ item.sub_path }}{% endif%}{% if search_items | length | int > 0%}?{% for search in search_items %}searchText[{{ search.name }}]={{ search.value | urlencode }}&{% endfor %}{% else%}?searchText[name]={{ item.body.name | urlencode }}{% endif %}"
method: "GET"
return_content: true
body: ''
# status_code: "{{ item.status_code | from_yaml | list }}"
status_code: [200, 201]
headers:
App-Token: "{{ glpi.app_token }}"
Session-Token: "{{ glpi.session.valid_id }}"
body_format: json
validate_certs: "{{ glpi.validate_certs | default(true) | bool }}"
register: glpi_search
no_log: true
- name: Set id
ansible.builtin.set_fact:
new_data:
id: "{{ glpi_search.json[0].id | int }}"
when: glpi_search.json | length | int == 1
- name: Iterate over results for TicketTemplateMandatoryField id/TicketTemplateHiddenField
ansible.builtin.shell:
cmd: |
cat <<EOF
{
{%- for json in glpi_search.json -%}
{%- if
json.tickettemplates_id | int == item_body.tickettemplates_id | int
and
json.num | int == item_body.num | int
-%}
"id": {{ json.id | int }}
{%- endif -%}
{%- endfor -%}
}
EOF
args:
executable: bash
register: itarate_id
changed_when: false
no_log: true
when: >
glpi_search.json | length | int > 1
and
(
'TicketTemplateMandatoryField' in item.sub_path | default('')
or
'TicketTemplateHiddenField' in item.sub_path | default('')
)
- name: Set iterate id
ansible.builtin.set_fact:
new_data: "{{ itarate_id.stdout | from_yaml }}"
when: itarate_id.stdout is defined
- name: Iterate over results for TicketTemplatePredefinedField id
ansible.builtin.shell:
cmd: |
cat <<EOF
{
{%- for json in glpi_search.json -%}
{%- if
json.num | int == 13
and
json.tickettemplates_id | int == item_body.tickettemplates_id | int
and
json.num | int == item_body.num | int
and
json.value == item_body.value
-%}
"id": {{ json.id | int }}
{%- elif
json.num | int != 13
and
json.tickettemplates_id | int == item_body.tickettemplates_id | int
and
json.num | int == item_body.num | int
-%}
"id": {{ json.id | int }}
{%- endif -%}
{%- endfor -%}
}
EOF
args:
executable: bash
register: itarate_id
changed_when: false
no_log: true
when: >
glpi_search.json | length | int > 1
and
'TicketTemplatePredefinedField' in item.sub_path | default('')
- name: Set iterate id
ansible.builtin.set_fact:
new_data: "{{ itarate_id.stdout | from_yaml }}"
when: itarate_id.stdout is defined
- name: Item_id fact Cleanup
ansible.builtin.set_fact:
item_body: "{{ item_body | ansible.builtin.combine(new_data | default({})) }}"
glpi_search: {}
itarate_id: {}
new_data: {}
no_log: true