使用自定义过滤器时发生错误

时间:2019-01-23 12:04:00

标签: filter ansible

我有一个字符串列表,我必须找到一些错误不等于零的字符串,然后打印出来

这是我的剧本:

  tasks:


   - name: Show and register errors counters
     ios_command:
       provider: "{{ cli }}"
       commands:
         - show interface counters error 
     register: sh_int_err_count

   - debug: var=sh_int_err_count.stdout_lines
     when: sh_int_err_count != ""

   - set_fact:
       test_def: "{{ sh_int_err_count.stdout_lines | getErrorList }}"

   - debug: var=test_def
     when: sh_int_err_count != ""

如果我调试sh_int_err_count变量,则会看到以下输出:

ok: [172.17.0.191] => {
    "sh_int_err_count.stdout_lines": [
        [
            "Port        Align-Err     FCS-Err    Xmit-Err     Rcv-Err  UnderSize  OutDiscards ", 
            "Gi0/1               0           0           0           0          0       212143 ", 
            "Gi0/2               0           0           0           0          0       184846 ", 
            "Gi0/3               0           0           0           0          0       315909 ", 
            "Gi0/4               0           0           0           0          0       253439 ", 
            "Gi0/5               0           0           0           0          0       135034 ", 
            "Gi0/6               0           0           0           0          0       132617 ", 
            "Gi0/7               0           0           0           0          0         5816 ", 
            "Gi0/8               0           0           0           0          0        12266 ", 
            "Gi0/9               0           0           0           0          0       573217 ", 
            "Gi0/10              0           0           0           0          0       844880 ", 
            "Gi0/11              0           0           0           0          0       264822 ", 
            "Gi0/12              0           0           0           0          0       197520 ", 
            "Gi0/13              0           0           0           0          0            0 ", 
            "Gi0/14              0           0           0           0          0       372725 ", 
            "Gi0/15              0           0           0           0          0     14260573 ", 
            "Gi0/16              0         515           0         515          0          786 ", 
            "Gi0/17              0           0           0           0          0      6906352 ", 
            "Gi0/18              0           0           0           0          0       333387 ", 
            "Po1                 0      232863           0      232863          0     21332550 ", 
            "", 
            "Port      Single-Col  Multi-Col   Late-Col  Excess-Col  Carri-Sen      Runts     Giants ", 
            "Gi0/1              0          0          0           0          0          0          0 ", 
            "Gi0/2              0          0          0           0          0          0          0 ", 
            "Gi0/3              0          0          0           0          0          0          0 ", 
            "Gi0/4              0          0          0           0          0          0          0 ", 
            "Gi0/5              0          0          0           0          0          0          0 ", 
            "Gi0/6              0          0          0           0          0          0          0 ", 
            "Gi0/7              0          0          0           0          0          0          0 ", 
            "Gi0/8              0          0          0           0          0          0          0 ", 
            "Gi0/9              0          0          0           0          0          0          0 ", 
            "Gi0/10             0          0          0           0          0          0          0 ", 
            "Gi0/11             0          0          0           0          0          0          0 ", 
            "Gi0/12             0          0          0           0          0          0          0 ", 
            "Gi0/13             0          0          0           0          0          0          0 ", 
            "Gi0/14             0          0          0           0          0          0          0 ", 
            "Gi0/15             0          0          0           0          0          0          0 ", 
            "Gi0/16             0          0          0           0          0          0          0 ", 
            "Gi0/17             0          0          0           0          0          0          0 ", 
            "Gi0/18             0          0          0           0          0          0          0 ", 
            "Po1                0          0          0           0          0          0          0"
        ]
    ]
}

但是,如果我尝试应用自定义过滤器getErrorList,则会发生错误: getErrorList()恰好接受1个参数(给定2个参数)

这是我的自定义过滤器。它找到错误不等于零的字符串。

 def getErrorList(array):
        return [i for i in array if any(x!="0" for x in i.split()[1:-2])] #check the range [1:-2] carefully. Use it as per your need.

为什么会发生此错误?当我尝试在python沙箱中应用它时,它将为我提供所需的结果。

0 个答案:

没有答案