From 70a48341a4f1ec510626b0db212a6c6d3fd60826 Mon Sep 17 00:00:00 2001 From: Jon Date: Fri, 28 Jul 2023 15:22:54 +0930 Subject: [PATCH] feat(config): Configure GLPI from json config files !1 --- docs/projects/nfc_glpi/config_from_code.md | 74 +++++++++++++++++++++ docs/projects/nfc_glpi/index.md | 3 +- mkdocs.yml | 2 + tasks/api/api.yaml | 6 ++ tasks/api/append-create-item.yaml | 77 ++++++++++++++++++++++ 5 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 docs/projects/nfc_glpi/config_from_code.md create mode 100644 tasks/api/append-create-item.yaml diff --git a/docs/projects/nfc_glpi/config_from_code.md b/docs/projects/nfc_glpi/config_from_code.md new file mode 100644 index 0000000..9092675 --- /dev/null +++ b/docs/projects/nfc_glpi/config_from_code.md @@ -0,0 +1,74 @@ +--- +title: Config as code +description: How to use No Fuss Computings Ansible role to configure GLPI from configuration as code. +date: 2023-07-28 +template: project.html +about: https://gitlab.com/nofusscomputing/projects/ansible/nfc_glpi +--- + +To configure GLPI using this role, the config/settings are saved as json files. The workflow to create the json files, is to (preferably within a test environment) make the required setting changes in GLPI using the GUI. one the changes have been made, conduct an API `GET` query to GLPI for the item you changed. The JSON body that is returned, is placed within the Config file (detailed below) under path `.body`. You must however, remove any fields that are not required, for example dynamic fields (fields that update themselves or are auto created) for example date fields. + + + +## Config files Example + +Each tab provides an example of the JSON file layout, including any additional variables. Any variable listed at path `.` that are empty strings, you would complete with the items name as it is exactly within GLPI. These fields are used to fetch the itm_id of the field by name. this enables having the config saved in a more human readable format. For example, for the field `entities_id` you would complete it as `"entities_id": "My entity name"`. + +=== "AuthLDAP" + + ``` json title="example.json" linenums="1 + { + "api_path": "AuthLDAP", + "body": { + // JSON from API Query + } + } + ``` +=== "Entity" + + ``` json title="example.json" linenums="1 + { + "api_path": "Entity", + "entities_id": "", + "body": { + // JSON from API Query + } + } + ``` + +=== "ITILCategory" + + ``` json title="example.json" linenums="1 + { + "api_path": "ITILCategory", + "users_id": "", + "itilcategories_id": "", + "entities_id": "", + "tickettemplates_id_demand": "", + "body": { + // JSON from API Query + } + } + ``` + +=== "Profile" + + ``` json title="example.json" linenums="1 + { + "api_path": "Profile", + "body": { + // JSON from API Query + } + } + ``` + +With the config files structured as per the examples, within your playbook(s), load the json files in a list of json objects using variable `glpi_config_as_code_json` + +example + +``` yaml title="my_vars.yaml" linenums="1 +glpi_config_as_code_json: + - "{{ lookup('file', '/workdir/files/glpi/ITILCategory-new-ldap-user.json') | from_json }}" + - "{{ lookup('file', '/workdir/files/glpi/LDAPAuth-local_ldap.json') | from_json }}" + +``` diff --git a/docs/projects/nfc_glpi/index.md b/docs/projects/nfc_glpi/index.md index 558380a..53f0682 100644 --- a/docs/projects/nfc_glpi/index.md +++ b/docs/projects/nfc_glpi/index.md @@ -1,5 +1,5 @@ --- -title: Ansible Role nfc_glpi +title: nfc_glpi - Ansible Role description: How to use No Fuss Computings Ansible role to manage GLPI from configuration as code. date: 2023-07-28 template: project.html @@ -13,6 +13,7 @@ This Ansible role is designed to manage GLPI specifically installation and confi - install GLPI using our [docker container](https://gitlab.com/nofusscomputing/projects/docker-glpi) +- [Configure using Config from Code](config_from_code.md) ## Default Variables diff --git a/mkdocs.yml b/mkdocs.yml index b717883..7bfdd1c 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -23,6 +23,8 @@ nav: - projects/nfc_glpi/index.md + - projects/nfc_glpi/config_from_code.md + - Operations: - operations/index.md diff --git a/tasks/api/api.yaml b/tasks/api/api.yaml index d24253d..f5e43f5 100644 --- a/tasks/api/api.yaml +++ b/tasks/api/api.yaml @@ -9,6 +9,12 @@ when: glpi.api.session | default('') == '' + - name: Append/Create Item + ansible.builtin.include_tasks: + file: append-create-item.yaml + loop: "{{ glpi_config_as_code_json }}" + + always: diff --git a/tasks/api/append-create-item.yaml b/tasks/api/append-create-item.yaml new file mode 100644 index 0000000..a862917 --- /dev/null +++ b/tasks/api/append-create-item.yaml @@ -0,0 +1,77 @@ +--- + +- name: Create item_body + ansible.builtin.set_fact: + item_body: "{{ item.body }}" + no_log: true + + +- name: Search item_id + ansible.builtin.include_tasks: + file: api/search/item_id.yaml + when: not item.body.id is defined + + +- name: Search entities_id + ansible.builtin.include_tasks: + file: api/search/entities_id.yaml + when: item.entities_id is defined + + +- name: Search tickettemplates_id_demand + ansible.builtin.include_tasks: + file: api/search/tickettemplates_id_demand.yaml + when: item.tickettemplates_id_demand is defined + + +- name: Search itilcategories_id + ansible.builtin.include_tasks: + file: api/search/itilcategories_id.yaml + when: item.itilcategories_id is defined + + +- name: Search users_id + ansible.builtin.include_tasks: + file: api/search/users_id.yaml + when: item.users_id is defined + + +- name: Show Body + ansible.builtin.debug: + msg: "{{ item_body }}" + when: item_body is defined + + +- name: Create Item + ansible.builtin.uri: + url: "http://{{ glpi.host }}/apirest.php/{{ item.api_path }}" + method: "POST" + return_content: true + body: "{\"input\": {{ item_body | from_yaml | to_json }} }" + status_code: [200, 201] + headers: + App-Token: "{{ glpi.app_token }}" + Session-Token: "{{ glpi.session.valid_id }}" + body_format: json + no_log: true + when: not item_body.id is defined + + +- name: Update Item + ansible.builtin.uri: + url: "http://{{ glpi.host }}/apirest.php/{{ item.api_path }}/{{ item_body.id }}" + method: "PATCH" + return_content: true + body: "{\"input\": {{ item_body | from_yaml | to_json }} }" + status_code: [200, 201] + headers: + App-Token: "{{ glpi.app_token }}" + Session-Token: "{{ glpi.session.valid_id }}" + body_format: json + no_log: true + when: item_body.id is defined + + +- name: Clear temp vars + ansible.builtin.set_fact: + item_body: {}