如何从ServiceNow中提取特定系统的更改记录?

时间:2019-04-11 18:25:40

标签: python bash ansible servicenow

我正在尝试自动执行补丁,需要找到一种方法从特定服务器的ServiceNow中提取更改记录,并确定是否为该服务器打开了更改窗口。

我尝试在Ansible中这样做,但是到目前为止我还没有运气。我想知道是否有更好的方法可以做到这一点?也许通过Python或只是使用curl和bash的简单方法?我听说有些人对其余的API很幸运,但是对此我知之甚少。

这是我的使用snow_record模块的剧本示例:

 - hosts: localhost
  connection: local
  gather_facts: no
  ignore_errors: True
  tasks:
  - name: Grab a user record
    snow_record:
      username: testuser
      password: somepass
      instance: myinstance
      state: present
      number: CHG9999
      table: change_request
      lookup_field: cmdb_ci.name=someservername

错误输出:

  The full traceback is:
    Traceback (most recent call last):
      File "/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py", line 113, in <module>
        _ansiballz_main()
      File "/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py", line 105, in _ansiballz_main
        invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
      File "/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py", line 48, in invoke_module
        imp.load_module('__main__', mod, module, MOD_DESC)
      File "/tmp/ansible_snow_record_payload_0p5jn8/__main__.py", line 332, in <module>
      File "/tmp/ansible_snow_record_payload_0p5jn8/__main__.py", line 328, in main
      File "/tmp/ansible_snow_record_payload_0p5jn8/__main__.py", line 317, in run_module
    AttributeError: 'module' object has no attribute 'UnexpectedResponse'

    fatal: [localhost]: FAILED! => {
        "changed": false, 
        "module_stderr": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py\", line 113, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py\", line 105, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1553017980.16-56687342300902/AnsiballZ_snow_record.py\", line 48, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_snow_record_payload_0p5jn8/__main__.py\", line 332, in <module>\n  File \"/tmp/ansible_snow_record_payload_0p5jn8/__main__.py\", line 328, in main\n  File \"/tmp/ansible_snow_record_payload_0p5jn8/__main__.py\", line 317, in run_module\nAttributeError: 'module' object has no attribute 'UnexpectedResponse'\n", 
        "module_stdout": "", 
        "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", 
        "rc": 1

我尝试了snow_record_find。我找到了here的模块。

剧本:

- hosts: mytest
  tasks:
  - name: Grab a user record
    snow_record_find:
      username: testuser
      password: somepass
      instance: someinstance
      #state: present
      #number: null??
      table: change_request
      query:
        AND:
          equals:
            active: "True"
            type: "standard"
            u_change_stage: "80"
          contains:
            u_template: "MY-Template"

错误输出:

fatal: []: FAILED! => {
    "changed": false, 
    "module_stderr": "Shared connection to  closed.\r\n", 
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/root/.ansible/tmp/ansible-tmp-1553019300.21-10858374496170/AnsiballZ_snow_record_find.py\", line 113, in <module>\r\n    _ansiballz_main()\r\n  File \"/root/.ansible/tmp/ansible-tmp-1553019300.21-10858374496170/AnsiballZ_snow_record_find.py\", line 105, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/root/.ansible/tmp/ansible-tmp-1553019300.21-10858374496170/AnsiballZ_snow_record_find.py\", line 48, in invoke_module\r\n    imp.load_module('__main__', mod, module, MOD_DESC)\r\n  File \"/tmp/ansible_snow_record_find_payload_j9P62r/__main__.py\", line 260, in <module>\r\n  File \"/tmp/ansible_snow_record_find_payload_j9P62r/__main__.py\", line 257, in main\r\n  File \"/tmp/ansible_snow_record_find_payload_j9P62r/__main__.py\", line 249, in run_module\r\n  File \"/usr/lib/python2.7/site-packages/pysnow/legacy_request.py\", line 91, in _all_inner\r\n    yield self._get_content(response)\r\n  File \"/usr/lib/python2.7/site-packages/pysnow/legacy_request.py\", line 332, in _get_content\r\n    server_error['summary'], server_error['details']\r\npysnow.legacy_exceptions.UnexpectedResponse: Unexpected HTTP GET response code. Expected 200, got 401\r\n", 
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", 

1 个答案:

答案 0 :(得分:2)

不幸的是,您要尝试的并不容易。 ServiceNow中没有现有的API,该API将为给定的配置项(CI / Server)返回下一个可用的维护窗口。

您可以对Maintenance schedule功能进行反向工程,并在ServiceNow平台上创建自己的脚本API来完成,但这可能不值得。

仅供参考,您可以在ServiceNow中使用REST API Explorer来获取各种语言(包括Python)的代码段。

最后,作为ServiceNow开发人员计划的一部分,可以使用一些不错的REST Web Service tutorials,通过它您还可以免费获得一个专用的ServiceNow实例。