0

I'm currently testing to retrieve firmware version lists on HP/Aruba switches using arubanetworks.aos_switch collection. Basically i'm just doing :

  collections:
    - arubanetworks.aos_switch
  
  tasks:
    - name: Launching "show flash" CLI
      arubaoss_command:
        commands:
          - "show flash"
 
      register: output_version
 
 
    - name: Firmware Display
      debug:
         msg: "{{ output_version.stdout_lines }}"

Here's what i retrieve when i launch my playbook with -vvv option from stdout & stdout_lines perspective :

    "stdout": [
        "show flashImage             Size (bytes) Date     Version \n----------------- ------------ -------- --------------\nPrimary Image    :    14184498 04/14/17 YA.16.03.0004        \nSecondary Image  :    14184498 04/14/17 YA.16.03.0004       \n\nBoot ROM Version \n----------------\nPrimary Boot ROM Version   : YA.15.19\b\nDefault Boot Image   : Primary"
    ],
    "stdout_lines": [
        [
            "show flashImage             Size (bytes) Date     Version ",
            "----------------- ------------ -------- --------------",
            "Primary Image    :    14184498 04/14/17 YA.16.03.0004        ",
            "Secondary Image  :    14184498 04/14/17 YA.16.03.0004       ",
            "",
            "Boot ROM Version ",
            "----------------",
            "Primary Boot ROM Version   : YA.15.19",
            "",
            "Default Boot Image   : Primary"
        ]
    ]
}

If i use the following regex msg: "{{ output_version.stdout | regex_findall('(Primary Image[^\\\\n]*)')}}" on stdout, i succeed to retrieve a one liner result like : "Primary Image : 14184498 04/14/17 YA.16.03.0004 "

My main question is, as it's not a JSON format output, what should be the best and simplest way to retrieve those data correctly ? In the future, i'd like to store it in a file basically something like hostname;flash image;Size;Date;Version

Thanks a lot for your advice. Gael

motorbass
  • 303
  • 2
  • 12

1 Answers1

1

You can extract individual lines from stdout_lines that match a regular expression using the select filter and the match test. For example:

- hosts: localhost
  gather_facts: false
  vars:
    output_version:
      stdout: "show flashImage             Size (bytes) Date     Version \n----------------- ------------ -------- --------------\nPrimary Image    :    14184498 04/14/17 YA.16.03.0004        \nSecondary Image  :    14184498 04/14/17 YA.16.03.0004       \n\nBoot ROM Version \n----------------\nPrimary Boot ROM Version   : YA.15.19\b\nDefault Boot Image   : Primary"
      stdout_lines: [
          [
              "show flashImage             Size (bytes) Date     Version ",
              "----------------- ------------ -------- --------------",
              "Primary Image    :    14184498 04/14/17 YA.16.03.0004        ",
              "Secondary Image  :    14184498 04/14/17 YA.16.03.0004       ",
              "",
              "Boot ROM Version ",
              "----------------",
              "Primary Boot ROM Version   : YA.15.19",
              "",
              "Default Boot Image   : Primary"
          ]
      ]

  tasks:
    - name: extract primary image
      set_fact:
        primary_image: >-
          {{ (output_version.stdout_lines[0] | select("match", "Primary Image") | first | split(":"))[1].strip().split() }}
        secondary_image: >-
          {{ (output_version.stdout_lines[0] | select("match", "Secondary Image") | first | split(":"))[1].strip().split() }}

    - debug:
        msg:
          - "{{ primary_image }}"
          - "{{ secondary_image }}"

If we run the above playbook, we get as output:

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        [
            "14184498",
            "04/14/17",
            "YA.16.03.0004"
        ],
        [
            "14184498",
            "04/14/17",
            "YA.16.03.0004"
        ]
    ]
}

Not that you have the data in a structured form you can store it however you see fit.

larsks
  • 43,623
  • 14
  • 121
  • 180
  • thanks a lot that's truly awesome and effective. To be honest i wasn't aware ansible got these methods for parsing. In the meanwhile i found this guide that list a lot of stuff https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html. Now i'll convert this into a json or csv format and i'll be done! Thanks again a lot !!! – motorbass Sep 28 '22 at 06:31