From 36f0ecaad1ca1be695c069fcf2bb8367442a8a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Moreno=20Gim=C3=A9nez?= Date: Fri, 26 Aug 2022 08:55:46 +0200 Subject: [PATCH] configuration_item_info: add return_fields parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Allow user to specify return fields for the specifed configuration item info Signed-off-by: Toni Moreno Giménez Signed-off-by: Abhijeet Kasurde --- .../configuration_item_info_return_fields.yml | 3 ++ plugins/modules/configuration_item_info.py | 44 +++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/configuration_item_info_return_fields.yml diff --git a/changelogs/fragments/configuration_item_info_return_fields.yml b/changelogs/fragments/configuration_item_info_return_fields.yml new file mode 100644 index 00000000..13f96912 --- /dev/null +++ b/changelogs/fragments/configuration_item_info_return_fields.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - configuration_item_info - allow user to specify limited return fields for the specified configuration item (https://github.com/ansible-collections/servicenow.itsm/pull/208). diff --git a/plugins/modules/configuration_item_info.py b/plugins/modules/configuration_item_info.py index 9d5ae781..50dfa314 100644 --- a/plugins/modules/configuration_item_info.py +++ b/plugins/modules/configuration_item_info.py @@ -23,7 +23,7 @@ short_description: List ServiceNow configuration item description: - - Retrieve information about ServiceNow configuration item. + - Retrieve information about the ServiceNow configuration items and also attachments related to this CI. - For more information, refer to the ServiceNow configuration item management documentation at U(https://docs.servicenow.com/bundle/tokyo-servicenow-platform/page/product/configuration-management/concept/c_ITILConfigurationManagement.html). version_added: 1.0.0 @@ -53,6 +53,15 @@ - If this parameter is unset when a configuration item info is queried, the default value C(cmdb_ci) will be used. type: str + return_fields: + description: + - A list of fields to return. + - If defined you need to add "attachments" as a field to return if you wish also get related attachments data. + - If C(return_fields) is not defined, all fields and also attachments will be returned. + type: list + elements: str + required: false + version_added: 2.3.0 """ EXAMPLES = r""" @@ -64,6 +73,14 @@ servicenow.itsm.configuration_item_info: sys_id: 01a9ec0d3790200044e0bfc8bcbe5dc3 register: result + +- name: Retrieve a specific configuration item by sys_id with limited return fields + servicenow.itsm.configuration_item_info: + sys_id: 01a9ec0d3790200044e0bfc8bcbe5dc3 + return_fields: + - name + - sys_id + register: result - name: Retrieve a specific configuration item by name servicenow.itsm.configuration_item_info: @@ -256,12 +273,27 @@ def run(module, table_client, attachment_client): module.params, "sys_id", "name", "sysparm_query", "sysparm_display_value" ) + # default yes, only disabled if not selected in return_fields + query_attachments = True + + if "return_fields" in module.params and module.params["return_fields"] is not None: + query["sysparm_fields"] = ",".join(module.params["return_fields"]) + if "attachments" not in module.params["return_fields"]: + query_attachments = False + + if query_attachments: + return [ + dict( + mapper.to_ansible(record), + attachments=attachment_client.list_records( + dict(table_name=cmdb_table, table_sys_id=record["sys_id"]), + ), + ) + for record in table_client.list_records(cmdb_table, query) + ] return [ dict( mapper.to_ansible(record), - attachments=attachment_client.list_records( - dict(table_name=cmdb_table, table_sys_id=record["sys_id"]), - ), ) for record in table_client.list_records(cmdb_table, query) ] @@ -285,6 +317,10 @@ def main(): sys_class_name=dict( type="str", ), + return_fields=dict( + type="list", + elements="str", + ), ), mutually_exclusive=[("sys_id", "query", "name", "sysparm_query")], )