首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible响应中的空字符串

Ansible响应中的空字符串
EN

Stack Overflow用户
提问于 2018-06-01 23:54:31
回答 1查看 375关注 0票数 0

我正在为Ansible 2.5开发RouterOS网络模块。

RouterOS外壳程序可以打印一些在on_open_shell()事件中应该检测到的消息,并自动跳过或清除。这些是Do you want to see the software license? [Y/n]:和其他几个,所有这些都是有很好文档的here in the MikroTik Wiki

我是这样做的:

代码语言:javascript
复制
def on_open_shell(self):
    try:
        if not prompt.strip().endswith(b'>'):
            self._exec_cli_command(b' ')
    except AnsibleConnectionFailure:
        raise AnsibleConnectionFailure('unable to bypass license prompt')

它确实绕过了许可提示。但是,来自RouterOS设备的\n响应似乎被视为对随后的实际命令的回复。因此,如果我的行动手册中有两个任务,如下所示:

代码语言:javascript
复制
---
- hosts: routeros
  gather_facts: no
  connection: network_cli
  tasks:
    - routeros_command:
        commands:
          - /system resource print
          - /system routerboard print
      register: result

    - name: Print result
      debug: var=result.stdout_lines

这是我得到的输出:

代码语言:javascript
复制
ok: [example] => {
    "result.stdout_lines": [
        [
            ""
        ],
        [
            "uptime: 12h33m29s",
            "                  version: 6.42.1 (stable)",
            "               build-time: Apr/23/2018 10:46:55",
            "              free-memory: 231.0MiB",
            "             total-memory: 249.5MiB",
            "                      cpu: Intel(R)",
            "                cpu-count: 1",
            "            cpu-frequency: 2700MHz",
            "                 cpu-load: 2%",
            "           free-hdd-space: 943.8MiB",
            "          total-hdd-space: 984.3MiB",
            "  write-sect-since-reboot: 7048",
            "         write-sect-total: 7048",
            "        architecture-name: x86",
            "               board-name: x86",
            "                 platform: MikroTik"
        ]
    ]
}

如你所见,输出似乎偏移了1。我应该怎么做才能纠正这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-01 23:54:31

原来问题出在定义shell提示符的正则表达式中。我把它定义成这样:

代码语言:javascript
复制
terminal_stdout_re = [
    re.compile(br"\[\w+\@[\w\-\.]+\] ?>"),
    # other cases
]

它与提示符的结尾不匹配,这导致Ansible认为在实际命令输出之前有一个换行符。下面是正确的regexp:

代码语言:javascript
复制
terminal_stdout_re = [
    re.compile(br"\[\w+\@[\w\-\.]+\] ?> ?$"),
    # other cases
]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50647173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档