Ansible期望模块不工作
Ansible expect module not working
在过去的一个小时里这让我很恼火,我使用 Ansible 的 expect 模块来回答命令提示符,即:
Re-format filesystem in Storage Directory /mnt/ephemeral-hdfs/dfs/name ? (Y or N)
我要回复
Y
这应该根据标准正则表达式匹配和 this other Whosebug 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
我面临的问题是,当它到达预期键盘输入的位置时,它什么也得不到,因此挂起。没有这样的错误输出;它只是保持静止。
有什么解决办法吗?
Ansible documentation for expect 示例中的正则表达式没有引号。
# Case insensitve password string match
- expect:
command: passwd username
responses:
(?i)password: "MySekretPa$$word"
也许试试:
Re-format\sfilesystem: "Y"
问题中的任务在问题中包含的数据上正常工作:
---
- 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
我知道这已经过时了,但我在使用这个模块时遇到了同样的问题,这些答案没有帮助,但我最终确实找到了自己的解决方案,并认为我可以节省一些时间。
首先发帖者示例中的超时是 10 分钟。虽然这对于重新格式化很有意义,但这意味着您需要等待 10 分钟,脚本才会失败。例如如果它停止等待对 "Are you sure?" 的响应。调试时保持超时时间低,如果不能则耐心等待。
其次,响应中的字段按字母顺序列出,因此
responses:
"Test a specific string": "Specific"
"Test": "General"
将始终使用 General 响应所有包含 Test 的消息,因为这是响应映射中按字母顺序排列的第一个。
第三(后续)这引起了我的注意,因为在我的例子中,expect 只是在提示符下按下回车键,脚本再次询问有效数据。那么问题是我的超时永远不会触发并且没有任何返回所以我没有看到模块的任何响应,它只是挂起。这种情况下的解决方案是转到您使用 Ansible 配置的服务器,找到命令 Ansible is 运行 ps 并终止它。这允许 Ansible 收集输出并向您显示它陷入无限循环的位置。
在过去的一个小时里这让我很恼火,我使用 Ansible 的 expect 模块来回答命令提示符,即:
Re-format filesystem in Storage Directory /mnt/ephemeral-hdfs/dfs/name ? (Y or N)
我要回复
Y
这应该根据标准正则表达式匹配和 this other Whosebug 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
我面临的问题是,当它到达预期键盘输入的位置时,它什么也得不到,因此挂起。没有这样的错误输出;它只是保持静止。
有什么解决办法吗?
Ansible documentation for expect 示例中的正则表达式没有引号。
# Case insensitve password string match
- expect:
command: passwd username
responses:
(?i)password: "MySekretPa$$word"
也许试试:
Re-format\sfilesystem: "Y"
问题中的任务在问题中包含的数据上正常工作:
---
- 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
我知道这已经过时了,但我在使用这个模块时遇到了同样的问题,这些答案没有帮助,但我最终确实找到了自己的解决方案,并认为我可以节省一些时间。
首先发帖者示例中的超时是 10 分钟。虽然这对于重新格式化很有意义,但这意味着您需要等待 10 分钟,脚本才会失败。例如如果它停止等待对 "Are you sure?" 的响应。调试时保持超时时间低,如果不能则耐心等待。
其次,响应中的字段按字母顺序列出,因此
responses:
"Test a specific string": "Specific"
"Test": "General"
将始终使用 General 响应所有包含 Test 的消息,因为这是响应映射中按字母顺序排列的第一个。
第三(后续)这引起了我的注意,因为在我的例子中,expect 只是在提示符下按下回车键,脚本再次询问有效数据。那么问题是我的超时永远不会触发并且没有任何返回所以我没有看到模块的任何响应,它只是挂起。这种情况下的解决方案是转到您使用 Ansible 配置的服务器,找到命令 Ansible is 运行 ps 并终止它。这允许 Ansible 收集输出并向您显示它陷入无限循环的位置。