0

What I am trying to achieve with this ansible code: Ansible code should be triggered during an interface flapping with cisco nessus device. It should fetch the time of nessus device(show clock) and try to pull the past 15 minutes from the "show logging" output of nessus device.

Ansible experts, I need to help to manipulate the time to get past 15 minutes from show logging. Below s the code written so far.

cat interfaceflapping.yml

---
 - name: Cisco NXOS
   hosts: all
   connection: network_cli
   gather_facts: false
   vars:
     - cmdlist: sh clock 
     - ansible_python_interpreter: /usr/bin/python3
     - ansible_network_os: nxos

   tasks:
     - name: Execute command
       nxos_command:
        commands: "{{ cmdlist }}"
       register: output
     - set_fact:
         arr: "{{ output.stdout_lines[0][1].split() }}"
     - debug:
          msg: "{{ arr[0] }}" 

     - name: print variable
       set_fact: 
         my_string_var: "{{ arr[0].split(':') | default('') }}"  
     - debug:
         msg: "{{my_string_var}}"

     - name: Execute command
       nxos_command:
        commands: sh logging | include 2/3
       register: output1

     - debug:
         msg: "{{ output1 }}"
       when: output1.stdout is search(my_string_var)

```ansible  
==================

output as below


[LABPC@lab-jump-host dow]$ ansible-playbook interfaceflapping.yml -i inventory1.txt --limit nxos --verbose
Using /etc/ansible/ansible.cfg as config file

PLAY [Cisco NXOS] ******************************************************************************************************************************************************************************************

TASK [Execute command] *************************************************************************************************************************************************************************************
ok: [nxos] => {"changed": false, "stdout": ["Time source is NTP\n18:50:15.681 UTC Sat Jul 02 2022"], "stdout_lines": [["Time source is NTP", "18:50:15.681 UTC Sat Jul 02 2022"]]}

TASK [set_fact] ********************************************************************************************************************************************************************************************
ok: [nxos] => {"ansible_facts": {"arr": ["18:50:15.681", "UTC", "Sat", "Jul", "02", "2022"]}, "changed": false}

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [nxos] => {
    "msg": "18:50:15.681"
}

TASK [print variable] **************************************************************************************************************************************************************************************
ok: [nxos] => {"ansible_facts": {"my_string_var": ["18", "50", "15.681"]}, "changed": false}

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [nxos] => {
    "msg": [
        "18", 
        "50", 
        "15.681"
    ]
}

TASK [Execute command] *************************************************************************************************************************************************************************************
ok: [nxos] => {"changed": false, "stdout": [""], "stdout_lines": [[""]]}

TASK [debug] ***********************************************************************************************************************************************************************************************
fatal: [nxos]: FAILED! => {"msg": "The conditional check 'output1.stdout is search(my_string_var)' failed. The error was: Unexpected templating type error occurred on ({% if output1.stdout is search(my_string_var) %} True {% else %} False {% endif %}): unhashable type: 'list'\n\nThe error appears to be in '/home/LABPC/gomathi/dow/root/dow/interfaceflapping.yml': line 32, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n     - debug:\n       ^ here\n"}

PLAY RECAP *************************************************************************************************************************************************************************************************
nxos                       : ok=6    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
==============

PLZ HELP ME TO GET THE PAST 15 MINUTES OUTPUT FROM SHOW LOGGING. I HAVE HARDCODED THE INTERFACE NUMBER AS 2/3  .
  • If you have an option, to make it easy i would suggest to use show logging last 100| in Instead of depending of time. – netv Jul 02 '22 at 20:17
  • @netv ..ur idea s right..however, i was asked to write based on time..Reason: whenever there s a interface flapping n Cisco nxos, a service tool ll trigger an automatic ticket .once ticket s generated, an ansible script should run and extract the past 15 mins data from the current time. requirement s different. So i m looking for a logic something based on time. – Inlovewithansible Jul 03 '22 at 03:45

0 Answers0