From e61808a890f5eeae51a75fc433ed91842e46040c Mon Sep 17 00:00:00 2001 From: Abir Majumdar Date: Fri, 7 Oct 2022 09:58:14 -0400 Subject: [PATCH] Initial commit --- .gitattributes | 2 + .gitignore | 2 + LICENSE | 15 +++++ README.md | 139 +++++++++++++++++++++++++++++++++++++++++++++++ action.yml | 93 +++++++++++++++++++++++++++++++ playbook.yml | 5 ++ requirements.yml | 3 + 7 files changed, 259 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 action.yml create mode 100644 playbook.yml create mode 100644 requirements.yml diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2111fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +.vscode \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..97379be --- /dev/null +++ b/LICENSE @@ -0,0 +1,15 @@ +ISC License + +Copyright (c) 2022, Abir Majumdar (https://github.com/abirismyname) + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b2444a3 --- /dev/null +++ b/README.md @@ -0,0 +1,139 @@ +# ServiceNow ITSM Actions + +ServiceNow ITSM Actions powered by Ansible. + + ![](https://github.com/abirismyname/create-discussion/workflows/tests/badge.svg) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) + +## About + +This action provides a wrapper around [Ansible Collection for ServiceNow ITSM](https://galaxy.ansible.com/servicenow/itsm) to perform operations on a ServiceNow instance. The Ansible Collection for ServiceNow IT Service Management (ITSM) includes a variety of Ansible content to help automate the management of ServiceNow IT Service Management. + +## Usage + +In your workflow, to perform a new operation, include a step like this: + +```yaml + - name: Let it SNOW + uses: shundor/servicenow-actions@main + with: + sn_host: "${{ secrets.SN_HOST }}" + sn_username: "${{ secrets.SN_USERNAME }}" + sn_password: "${{ secrets.SN_PASSWORD }}" + sn_client_id: "${{ secrets.SN_CLIENT_ID }}" + sn_client_secret: "${{ secrets.SN_CLIENT_SECRET }}" + sn_module: "${{ secrets.SN_MODULE }}" + sn_module_params: "${{ secrets.SN_MODULE_PARAMS }}" +``` + +## Inputs + +The following inputs are _required_: + +- `sn_host`: The ServiceNow instance hostname +- `sn_username`: The ServiceNow username +- `sn_password`: The ServiceNow password +- `sn_client_id`: The ServiceNow client ID +- `sn_client_secret`: The ServiceNow client secret +- `sn_module`: The Ansible module to use +- `sn_module_params`: The parameters to pass to the Ansible module + +Optional: +- `debug`: When set to `true`, the action save the playbook to a workflow artifact + +### Obtaining the `sn_client_id` and `sn_client_secret` +You can create `sn_client_id` and `sn_client_secret` via the Application Registry under System OAuth in ServiceNow. +[![Screenshot](https://developer.servicenow.com/api/x_snc_devblog/v1/vfs/file?p=/post/connections-and-credentials/application-registry-record.png)](https://developer.servicenow.com/blog.do?p=/post/connections-and-credentials/) + +### SN_MODULE +The module parameter must one of the following: +|Name|Description| +|:----|:----| +|[servicenow.itsm.api](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.api_module.rst)|Manage ServiceNow POST, PATCH and DELETE requests| +|[servicenow.itsm.api_info](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.api_info_module.rst)|Manage ServiceNow GET requests| +|[servicenow.itsm.attachment](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.attachment_module.rst)|a module that users can use to download attachment using sys_id| +|[servicenow.itsm.change_request](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.change_request_module.rst)|Manage ServiceNow change requests| +|[servicenow.itsm.change_request_info](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.change_request_info_module.rst)|List ServiceNow change requests| +|[servicenow.itsm.change_request_task](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.change_request_task_module.rst)|Manage ServiceNow change request tasks| +|[servicenow.itsm.change_request_task_info](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.change_request_task_info_module.rst)|List ServiceNow change request tasks| +|[servicenow.itsm.configuration_item](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.configuration_item_module.rst)|Manage ServiceNow configuration items| +|[servicenow.itsm.configuration_item_batch](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.configuration_item_batch_module.rst)|Manage ServiceNow configuration items in batch mode| +|[servicenow.itsm.configuration_item_info](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.configuration_item_info_module.rst)|List ServiceNow configuration item| +|[servicenow.itsm.incident](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.incident_module.rst)|Manage ServiceNow incidents| +|[servicenow.itsm.incident_info](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.incident_info_module.rst)|List ServiceNow incidents| +|[servicenow.itsm.problem](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.problem_module.rst)|Manage ServiceNow problems| +|[servicenow.itsm.problem_info](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.problem_info_module.rst)|List ServiceNow problems| +|[servicenow.itsm.problem_task](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.problem_task_module.rst)|Manage ServiceNow problem tasks| +|[servicenow.itsm.problem_task_info](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.problem_task_info_module.rst)|List ServiceNow problem tasks| + +### SN_MODULE_PARAMS + +### Example: Creating an Incident + +To create an incident, you can use the `servicenow.itsm.incident` module. The following example creates an incident with a short description of "Test Incident" and a description of "This is a test incident created by the ServiceNow Actions GitHub Action": + +```yaml + - name: Create An Incident + uses: shundor/servicenow-actions@main + with: + sn_host: "${{ secrets.SN_HOST }}" + sn_username: "${{ secrets.SN_USERNAME }}" + sn_password: "${{ secrets.SN_PASSWORD }}" + sn_client_id: "${{ secrets.SN_CLIENT_ID }}" + sn_client_secret: "${{ secrets.SN_CLIENT_SECRET }}" + sn_module: "servicenow.itsm.incident" + sn_module_params: "${{ secrets.SN_MODULE_PARAMS }}" +``` + +The value for the SN_MODULE_PARAMS secret should be: +```yaml +caller: admin +short_description: Test Incident +description: This is a test incident created by the ServiceNow Actions GitHub Action +impact: low +urgency: low +``` + +For all the different parameters that can be passed to the `servicenow.itsm.incident` module, see the [docs](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.incident_module.rst) on GitHub. + +### Example: Get Info on a specific Incident + +To get info on a specific incident, you can use the `servicenow.itsm.incident_info` module. The following example gets details about Incident incident_info: + +```yaml + - name: Retrieve Incident INC0000039 + uses: shundor/servicenow-actions@main + id: snow + with: + sn_host: "${{ secrets.SN_HOST }}" + sn_username: "${{ secrets.SN_USERNAME }}" + sn_password: "${{ secrets.SN_PASSWORD }}" + sn_client_id: "${{ secrets.SN_CLIENT_ID }}" + sn_client_secret: "${{ secrets.SN_CLIENT_SECRET }}" + sn_module: "servicenow.itsm.incident_info" + sn_module_params: "${{ secrets.SN_MODULE_PARAMS }}" + - name: Write to file + uses: frdrwrt/write-to-file@v1.3 + with: + filepath: output.json + mode: 0777 + content: ${{ steps.snow.outputs.output }} + - name: Display output + shell: bash + run: | + jq '.plays[].tasks[].hosts.localhost.records[]' output.json +``` + +The value for the SN_MODULE_PARAMS secret should be: +```yaml +number: INC0000039 +``` + +For all the different parameters that can be passed to the `servicenow.itsm.incident_info_module` module, see the [docs](https://github.com/ansible-collections/servicenow.itsm/blob/main/docs/servicenow.itsm.incident_info_module.rst) on GitHub. The `output` parameter will contain all the information about the incident. +## Outputs + +This action provides the following outputs: +- `output`: The captured output as JSON +## Credits + +- :bow: Based on [action-ansible-playbook](https://github.com/marketplace/actions/run-ansible-playbook) by [dawidd6](https://github.com/dawidd6) +- :bow: This action relies on [Ansible Collection for ServiceNow ITSM](https://galaxy.ansible.com/servicenow/itsm) diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..469b82d --- /dev/null +++ b/action.yml @@ -0,0 +1,93 @@ +name: 'ServiceNow ITSM Actions' +description: 'ServiceNow ITSM Actions powered by Ansible' +branding: + icon: cloud-snow + color: orange +inputs: + sn_host: + description: 'ServiceNow instance host' + required: true + sn_username: + description: 'ServiceNow username' + required: true + sn_password: + description: 'ServiceNow password' + required: true + sn_client_id: + description: 'ServiceNow client ID' + required: true + sn_client_secret: + description: 'ServiceNow client secret' + required: true + sn_module: + description: 'ServiceNow module name' + required: true + sn_module_params: + description: 'ServiceNow module parameters' + required: true + debug: + description: 'Enable debug mode' + required: false + default: 'false' +outputs: + output: + description: "Playbook output" + value: ${{ steps.run-playbook.outputs.output }} +runs: + using: composite + steps: + - name: Validate input + id: validate + shell: bash + run: | + WORD_LIST="servicenow.itsm.api servicenow.itsm.api_info servicenow.itsm.attachment servicenow.itsm.change_request servicenow.itsm.change_request_info servicenow.itsm.change_request_task servicenow.itsm.change_request_task_info servicenow.itsm.configuration_item servicenow.itsm.configuration_item_batch servicenow.itsm.configuration_item_info servicenow.itsm.incident servicenow.itsm.incident_info servicenow.itsm.problem servicenow.itsm.problem_info servicenow.itsm.problem_task servicenow.itsm.problem_task_info" + MATCH="${{ inputs.sn_module }}" + + if echo $WORD_LIST | grep -w -v $MATCH > /dev/null; then + exit 1 + fi + - name: The validate step has failed + if: ${{ failure() && steps.validate.conclusion == 'failure' }} + shell: bash + run: echo '${{ inputs.sn_module }} is not a valid module name' + - uses: actions/checkout@v3 + with: + repository: shundor/servicenow-actions + - name: Append to playbook + run: | + echo " - name: \"${{ inputs.sn_module }}\"" >> playbook.yml + echo " ${{ inputs.sn_module }}:" >> playbook.yml + echo "${{ inputs.sn_module_params }}" >> params.txt + sed -i 's/^/ /g' params.txt + cat params.txt >> playbook.yml + cat playbook.yml + shell: bash + - name: Upload artifact + if: ${{ inputs.debug != 'false' }} + uses: actions/upload-artifact@main + with: + name: playbook.yml + path: playbook.yml + - name: Run playbook + id: run-playbook + uses: dawidd6/action-ansible-playbook@v2 + with: + playbook: playbook.yml + directory: ./ + requirements: requirements.yml + no_color: true + inventory: | + [local] + localhost ansible_connection=local + options: | + --limit local + env: + SN_HOST: ${{ inputs.sn_host }} + SN_USERNAME: ${{ inputs.sn_username }} + SN_PASSWORD: ${{ inputs.sn_password }} + SN_CLIENT_ID: ${{ inputs.sn_client_id }} + SN_CLIENT_SECRET: ${{ inputs.sn_client_secret }} + SN_MODULE: ${{ inputs.sn_module }} + SN_MODULE_PARAMS: ${{ inputs.sn_module_params }} + ANSIBLE_LOAD_CALLBACK_PLUGINS: "true" + ANSIBLE_STDOUT_CALLBACK: "json" diff --git a/playbook.yml b/playbook.yml new file mode 100644 index 0000000..40d4db4 --- /dev/null +++ b/playbook.yml @@ -0,0 +1,5 @@ +--- +- name: Playbook + hosts: all + gather_facts: false + tasks: diff --git a/requirements.yml b/requirements.yml new file mode 100644 index 0000000..b6817a1 --- /dev/null +++ b/requirements.yml @@ -0,0 +1,3 @@ +--- +collections: + - name: servicenow.itsm \ No newline at end of file