首页 > 解决方案 > Ansible cli_command 提示未按预期工作

问题描述

我正在尝试从 Cisco 设备执行 ping 命令。我必须使用扩展的 ping 命令,我不能只在一行中指定参数。

这是路由器所期望的:

ping
Protocol [ip]: 
Target IP address: <IP HERE>
Repeat count [5]:
Datagram size [100]:
Timeout in seconds [2]:
Extended commands [n]: y
Ingress ping [n]:
Source address or interface: <INT HERE>
DSCP Value [0]:
Type of service [0]:
Set DF bit in IP header? [no]:
Validate reply data? [no]:
Data pattern [0x0000ABCD]:
Loose, Strict, Record, Timestamp, Verbose[none]:
Sweep range of sizes [n]:

如您所见,我只定义了目标 IP、扩展命令标志和源接口。

这就是我的任务现在的样子:

- name: Run Cisco command (ping)
  cli_command:
    command: ping
    prompt:
      - "Protocol [ip]:"
      - "Target IP address:"
      - "Repeat count [5]:"
      - "Datagram size [100]:"
      - "Timeout in seconds [2]:"
      - "Extended commands [n]:"
      - "Ingress ping [n]:"
      - "Source address or interface:"
      - "DSCP Value [0]:"
      - "Type of service [0]:"
      - "Set DF bit in IP header? [no]:"
      - "Validate reply data? [no]:"
      - "Data pattern [0x0000ABCD]:"
      - "Loose, Strict, Record, Timestamp, Verbose[none]:"
      - "Sweep range of sizes [n]:"
    answer:
      - ''
      - "{{ item }}"
      - "{{ count }}" (I'm specifying the number of packets here but its optional, same for the size)
      - "{{ size }}"
      - ''
      - y
      - ''
      - loopback1
      - ''
      - ''
      - ''
      - ''
      - ''
      - ''
      - ''
  with_lines: cat "{{ dest_file }}"
  ignore_errors: yes
  when: ansible_network_os == 'ios'

这就是 ansible 返回的内容:

"results":[
      {
         "ansible_loop_var":"item",
         "item": first target here,
         "changed":false,
         "failed":true,
         "invocation":{
            "module_args":{
               "sendonly":false,
               "prompt":[
                  "Protocol [ip]",
                  "Target IP address",
                  "Repeat count [5]",
                  "Datagram size [100]:",
                  "Timeout in seconds [2]:",
                  "Extended commands [n]",
                  "Ingress ping [n]",
                  "Source address or interface",
                  "DSCP Value [0]",
                  "Type of service [0]",
                  "Set DF bit in IP header? [no]",
                  "Validate reply data? [no]",
                  "Data pattern [0x0000ABCD]",
                  "Loose, Strict, Record, Timestamp, Verbose[none]",
                  "Sweep range of sizes [n]"
               ],
               "check_all":true,
               "newline":true,
               "command":"ping",
               "answer":[
                  "",
                  "192.168.1.1", (first target here)
                  "5",
                  "72",
                  "",
                  "y",
                  "",
                  "loopback 1",
                  "",
                  "",
                  "",
                  "",
                  "",
                  "",
                  ""
               ]
            }
         },
         "ansible_facts":{
            "discovered_interpreter_python":"/usr/bin/python"
         },
         **"msg":"timeout value 30 seconds reached while trying to send command: ping"**
      },
      {
         "ansible_loop_var":"item",
         **"stdout":"% Unknown protocol - \"ping\", type \"ping ?\" for help",**
         "failed":false,
         "changed":false,
         "item":"second target here",
         "invocation":{
            "module_args":{
               "sendonly":false,
               "prompt":[
                  "Protocol [ip]",
                  "Target IP address",
                  "Repeat count [5]",
                  "Datagram size [100]:",
                  "Timeout in seconds [2]:",
                  "Extended commands [n]",
                  "Ingress ping [n]",
                  "Source address or interface",
                  "DSCP Value [0]",
                  "Type of service [0]",
                  "Set DF bit in IP header? [no]",
                  "Validate reply data? [no]",
                  "Data pattern [0x0000ABCD]",
                  "Loose, Strict, Record, Timestamp, Verbose[none]",
                  "Sweep range of sizes [n]"
               ],
               "check_all":true,
               "newline":true,
               "command":"ping",
               "answer":[
                  "",
                  "192.168.1.2", (second target here)
                  "5",
                  "72",
                  "",
                  "y",
                  "",
                  "loopback 1",
                  "",
                  "",
                  "",
                  "",
                  "",
                  "",
                  ""
               ]
            }
         },
         "stdout_lines":[
            **"% Unknown protocol - \"ping\", type \"ping ?\" for help"**
         ]
      }

首先,每对 IP 中的第一个总是超时,即使它可以访问。不管是 30 秒还是 120 秒超时。

二、为什么第一个提示(协议)取值“ping”而不是值“”?"% 未知协议 - "ping",键入 "ping ?" 寻求帮助"

我将空答案更改为: "", '', \n, "ip" 但没有任何效果,结果始终相同。

我还将提示更改为:“协议 [ip]:”、“协议 [ip]”、“协议 [ip]:”、协议 [ip]、协议 [ip],但没有一个有效。

这个想法是 15 个答案按顺序回答 15 个提示。

知道我做错了什么吗?

标签: ansiblepingpromptcisco

解决方案


推荐阅读