Ansible期望模块不工作

时间:2017-03-10 16:56:38

标签: regex ansible expect

过去一小时这让我感到恼火,我使用Ansible的expect模块来回答命令提示符,即:

Re-format filesystem in Storage Directory /mnt/ephemeral-hdfs/dfs/name ? (Y or N)

我要回复

Y

这应该根据标准正则表达式匹配和this other stackoverflow question

工作
 - name: Run Spark Cluster script
    expect:
       command: /home/ubuntu/cluster_setup/scripts/shell/utils-cluster_launcher-start_spark.sh
       responses:
          "Re-format filesystem": "Y"
       timeout: 600
       echo: yes

我面临的问题是,当它达到预期键盘输入的程度时,它不会得到任何东西,因此它会挂起。这样没有错误输出;它只是保持不动。

任何想法如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

问题中的任务可以正确处理问题中包含的数据:

---
- hosts: localhost
  gather_facts: no
  connection: local
  tasks:
    - name: Run script producing the same prompt as Spark Cluster script
      expect:
        command: ./prompt.sh
        responses:
          "Re-format filesystem": "Y"
        timeout: 600
        echo: yes
      register: prompt
    - debug:
        var: prompt.stdout_lines

./prompt.sh的内容:

#!/bin/bash

read -p "Re-format filesystem in Storage Directory /mnt/ephemeral-hdfs/dfs/name ? (Y or N) " response
echo pressed: $response

结果:

PLAY [localhost] ***************************************************************

TASK [Run script producing the same prompt as Spark Cluster script] ************
changed: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "prompt.stdout_lines": [
        "Re-format filesystem in Storage Directory /mnt/ephemeral-hdfs/dfs/name ? (Y or N) Y",
        "pressed: Y"
    ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0

答案 1 :(得分:1)

Ansible documentation for expect示例中的正则表达式周围没有引号。

TimeSpan ts = DateTime.Now - new DateTime(2017, 1, 1);

也许试试:

# Case insensitve password string match
- expect:
    command: passwd username
    responses:
      (?i)password: "MySekretPa$$word"

答案 2 :(得分:0)

我知道这很旧了,但是我在此模块上遇到了同样的麻烦,这些答案也无济于事,但是我确实找到了自己的解决方案,并以为我可以节省一些时间。

首先,张贴者示例中的超时为10分钟。尽管这对于重新格式化很有用,但是这意味着您需要等待10分钟才能使脚本失败。例如如果停止,则等待响应“确定吗?”。调试时,请将超时时间设置得较低,如果您无法忍耐,请耐心等待。

第二个答复字段按字母顺序列出

responses:
  "Test a specific string": "Specific"
  "Test": "General"

将始终以常规响应所有包含测试的消息,因为这是响应图中的第一个字母顺序。

第三(随后)使我失望,因为在我的情况下,期望只是在提示符下按Enter键,脚本再次要求输入有效数据。然后的问题是我的超时永不触发,什么也没有返回,所以我看不到模块的任何响应,它只是挂起了。在这种情况下,解决方案是转到使用Ansible进行配置的服务器,找到命令Ansible与ps一起运行并杀死它。这使Ansible可以收集输出并向您显示它陷入无限循环的位置。

相关问题