Utilize Ansible for opening and closing tickets with ServiceNow - Part 3.

Here are links to Part 1 or Part 2

This article is part three in a series covering the role Ansible can play in ticket automation. This time we’ll cover parsing the command output data your adding to your tickets.

In the last blog we had a lot of text from the show version command end up in our ticket.

notes

We can filter this output and only record the specific data points we choose. This can be done with the ansible-network.network-engine role and custom parsers. In this blog we are going to use this specific parser show_version.yaml from the network-engine github repo.

You will structure your repo like this tree

On line 48 of show_version.yaml you can find the name of the dictionary we need to use in our debug.

system facts

To test this out we can run this playbook.

---
- name: parser example
  hosts: ios
  gather_facts: no
  connection: network_cli
  roles:
    - ansible-network.network-engine

  tasks:
  - name: show version
    ios_command:
      commands:
        - show version
    register: version

  - name: parse show version
    command_parser:
      file: "parsers/show_version.yaml"
      content: "{{ version.stdout.0 }}"

  - debug: var=system_facts

The parser takes the command output and creates a dictionary. The data points you want to use later in your playbook, are now parameters of system_facts.

example

What I would like to do is add some fields in the ServiceNow change_request template, then add the version and uptime to every change request Ansible creates.

In ServiceNow this is done under Configure > Form Layout.

layout

You can see below that I have added two new fields IOS Version & Device Up Time.

fields

If we take a look at a snippet of the record dictionary the ServiceNow API sends back, it now has the the two new fields. "u_device_up_time" & "u_ios_version"

api

We can use these new fields in the data section of our snow_record module. Here is a complete playbook that will run the show version command, parse the output and add the parameters into the new fields for us.

Now lets try the example.yml playbook with all of our tasks.

example.yml

---
- name: create ticket with notes
  hosts: ios
  gather_facts: no
  connection: network_cli
  roles:
    - ansible-network.network-engine

  tasks:
  - name: include vars
    include_vars: incident_vars.yml

  - name: show version
    ios_command:
      commands:
        - show version
    register: version

  - name: parse show version
    command_parser:
      file: "parsers/show_version.yaml"
      content: "{{ version.stdout.0 }}"

  - name: create an incident
    snow_record:
      state: present
      table: incident
      username: "{{ sn_username }}"
      password: "{{ sn_password }}"
      instance: "{{ sn_instance }}"
      data:
        priority: "{{ sn_priority}}"
        u_device_up_time: "{{ system_facts.uptime }}"
        u_ios_version: "{{ system_facts.version }}"
        short_description: "This ticket was created by Ansible"
    register: new_incident

  - debug: var=new_incident.record

The new fields are now populated in our ServiceNow change request.

ticketvars

If you’re interested in learning more, stay tuned for Part 4 of this series. We will continue to build a framework for fully automating our network tickets.

TheNetwork.Engineer - July 8 2018 - Colin McCarthy

Free DevOps Resources


Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we don't like spam.