UNSOLVED

wetnose88

updated

3 years ago

Closed

W

wetnose88

6 Posts

0

613

July 14th, 2023 09:00

How to filter Powermax alerts for most recent use Ansible playbook

---
- name: Get alerts for powermax
  hosts: localhost
  connection: local

  vars:
    unispherehost: "10.x.x.x"
    universion: "100"
    verifycert: False
    user: "smc"
    password: "securePassword123#"
    serial_no: "000120001234"
    days_back: 7

  collections:
    - dellemc.powermax

  tasks:
    - name: Get alerts for powermax
      dellemc.powermax.info:
        unispherehost: "{{unispherehost}}"
        serial_no:  "{{serial_no}}"
        universion: "{{universion}}"
        verifycert: "{{verifycert}}"
        user: "{{user}}"
        password: "{{password}}"
        gather_subset:
          - health
          - alert
    - name: Filter alerts for the last 7 days
      set_fact:
        filtered_alerts: "{{ alert_output.powermax_info.alert | rejectattr('timestamp', 'lt', days_back*24*3600) | list }}"

    - name: Display filtered alerts
      debug:
        var: filtered_alerts

fatal: [localhost]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'alert'. 'dict object' has no attribute 'alert'\n\nThe error appears to be in '/home/jchang/.ansible/playbooks/powermax_get_health_alert_xdays_be.yml'

  • jennifer_john

    1 Rookie

    •

    79 Posts

    217

    0

    Posted July 18th, 2023 01:00

    Hi @wetnose88 

    I have modified the task as below. The filter can be on the Alerts dict from the response. The response includes date output fields created_date, created_date_milliseconds for the filter conditions. 

     
    - name : Get alerts for powermax
          register : alert_output
          dellemc.powermax.info :
            unispherehost : " { {unispherehost}} "
            serial_no :   " { {serial_no}} "
            universion : " { {universion}} "
            verifycert : " { {verifycert}} "
            user : " { {user}} "
            password : " { {password}} "
            gather_subset :
              - health
              - alert
        - name : Filter alerts for the last 7 days
          set_fact :
            filtered_alerts : " { { alert_output.Alerts | rejectattr('created_date_milliseconds', 'lt', days_back*24*3600) | list }} "

        - name : Display filtered alerts
          debug :
            var : filtered_alerts
     
    Thanks,
    Jennifer
  • wetnose88

    6 Posts

    210

    0

    Posted July 18th, 2023 08:00

    Thanks a lot Nennifer_john,

    The playbook now working, but after I checked the filters, I will not be able to get any filtered alerts, even after I x1000 convert it to milliseconds

    filtered_alerts: "{ { alert_output.Alerts | rejectattr('created_date_milliseconds', 'lt', days_back*24*3600*1000) | list }}"

    Because the created_date_milliseconds is a 13 digital number, the days_back*24*3600*1000 calculation 7* *24*3600*1000 is 604,800,000

    the following ansible output

    "created_date": "Jun-19-2023 19:03:33.000",
    "created_date_milliseconds": 1687201413000,

    now the question is how to related these two "created_date" and "created_date_milliseconds", otherwise the comparing is meaningless.

     

  • jennifer_john

    1 Rookie

    •

    79 Posts

    171

    0

    Posted July 20th, 2023 06:00

    Hi @wetnose88 

    created_date_milliseconds is EPCOH time in milliseconds and created_date is the local time of the Unisphere server

    Thanks,

    Jennifer