Create a list of Failed tasks

Hello,

I am trying to get a list of devices that are out of compliance with Cisco licensing. I would like to take failed out of compliance devices and add them to a list. Then send it to Microsoft Teams.

What I am looking for is a list to just send to Teams once, instead of trying to send multiple calls for each failure. Teams does not like it when you make multiple calls in quick succession.

The way that I am currently doing it is taking the host name and splitting each letter and joining with a comma. This was just my last attempt. I also tried with_items but it seems to just be tied to each host and not a global variable. Which makes sense in most cases… Any help would be appreciated.

  • name: Get License Information
    cisco.ios.ios_command:
    commands: “show license summary”
    register: license3

  • name: Assert that it is registered and authorized
    block:

  • name: Asset that it is authorized
    assert:
    that:

  • “‘Status: AUTHORIZED’ in license3.stdout[0]”
    fail_msg: “Device is not Authorized”
    success_msg: “Device Authorized”
    rescue:

  • name: Add the device to the OOC list
    set_fact: #### This is the piece that does not work.
    OOC: “{{ inventory_hostname | join(', ') }}”

  • name: Send a team message of OUT OF COMPLIANCE devices
    uri:
    url: “webhook”
    method: POST
    status_code: 200
    body_format: json
    body: |
    {
    “title”: “Smart License Out of Compliance Devices”,
    “text”: “{{ OOC }},”
    }
    run_once: True
    tags: notifications

  • name: Debug
    debug:
    var: OOC
    verbosity: 0
    run_once: True

Set variable OOC_status in the block and use it to select the list of
the hosts. See the inline code below (not tested)

I am trying to get a list of devices that are out of compliance with Cisco
licensing...

  - name: Get License Information
    cisco.ios.ios_command:
      commands: "show license summary"
    register: license3

  - name: Assert that it is registered and authorized
    block:

        - name: Set status omit host
          set_fact:
            OOC_status: false

      - name: Asset that it is authorized
        assert:
          that:
            - "'Status: AUTHORIZED' in license3.stdout[0]"
          fail_msg: "Device is not Authorized"
          success_msg: "Device Authorized"
    rescue:

        - name: Set status add host
          set_fact:
            OOC_status: true

    - name: Add devices to the OOC list
      set_fact:
        OOC: "{{ hostvars|dict2items|
                 json_query('[?value.OOC_status].key') }}"
      run_once: true

    - name: Send a team message ...

Worked - Just had to add {{ OOC | join(', ') }} to clean up the output. Thank you Vladimir.