Observium reporting with Ansible

Observium has a nice GUI, and you can find lots of information but it does not have a reports feature so you can save it to a file. This is usually useful when some other team needs some info for auditing purposes for example.
Observium has an API so I was thinking to use it to get the info I want. I could do it in Python but I’ve chosen Ansible this time. And to make it really easy for others to run it I’ve used AWX.
Also, I wanted to be able to display the info on screen, save it to a local file but also post the info in a Gitlab repository. This way I could share easily the info with other teams.

Starting with all these in mind I’ve written a small script which I am sharing with you.

First I’ve used Observium API to get the information for all devices that have an os type.

    - name: Get OS devices
        url: "https://{{ansible_host}}/api/v0/devices/?os={{os}}"
        method: GET
        validate_certs: no
        return_content: yes
        user: "{{observium_user}}"
        password: "{{observium_password}}"
        force_basic_auth: yes
        body_format: json
      register: result

The os variable is defining what type of os I am searching for and it can be found in the vars file (folder group_vars):

#os: arista_eos
os: ios
#os: asa
#os: iosxe
#os: iosxr
#os: nxos
#os: cumulus-os
#os: fortigate

Next, I am building a list:

 - name: Add new JSON Objects to List
         devices: "{{ devices +
                       [{ 'Hostname': item.value.hostname,
                          'Hardware': item.value.hardware,
                          'Vendor': item.value.vendor,
                          'Version': item.value.version,
                          'Features': item.value.features,
                          'Serial': item.value.serial  }] }}"

      loop: "{{ devices_list|dict2items }}"

Next, I am writing to a local file and display it on screen:

 - name: file
        dest: devices.txt
        line: "{{item.Hostname}},{{item.Vendor}},{{item.Hardware}},{{item.Version}},{{item.Features}},{{item.Serial}}"
      loop: "{{ devices }}"

    - name: display file
      command: cat devices.txt
      register: result

    - name: print file
        msg: "{{result.stdout_lines}}"

And finally, I am posting the info to a Gitlab Repository:

  - name: post a file in gitlab
        url: "https://git.website.com/api/v4/projects/11/repository/commits"
        method: POST
        validate_certs: no
        body_format: json
        status_code: 201
          PRIVATE-TOKEN: "{{git_token}}"
        body: {
                "branch": "master",
                "commit_message": "ansible report on devices with {{os}} commit message",
                "actions": [
                   "action": "create",
                   "file_path": "devices_{{os}}_{{day}}_{{time}}.txt",
                   "content": "{{result.stdout_lines | to_nice_json}}"

For example, the result for ios xe devices looks like this:

where each line represents: Device Name,Vendor,Hardware,Version,Features,Serial

This project/script is just an example. You can change the API request you make to Observium and build different reports.

Source project:

Observium API:

About the author

Mihai is a Network Aficionado with more than 10 years experience