ansible - 用ansible发送mailconfig命令,回答问题
问题描述
我需要在 Ansible 任务中执行“sendmailconfig”命令并回答是。
我用这段代码试试:
- name: Exec sendmailconfig
expect:
command: sendmailconfig
responses:
Question:
- Configure sendmail with the existing /etc/mail/sendmail.conf? [Y]: y
- Configure sendmail with the existing /etc/mail/sendmail.mc? [Y]: y
- Reload the running sendmail now with the new configuration? [Y]: y
timeout: 30
但任务以错误结束:
任务 [执行 sendmailconfig] ************************************************ ****************************************************** ************************************************ 致命:[demoHostAnsible ]: 失败的!=> {“更改”:true,“cmd”:“sendmailconfig”,“delta”:“0:00:30.133671”,“end”:“2019-10-04 14:55:34.398377”,“msg”: “命令超出超时”,“rc”:null,“start”:“2019-10-04 14:55:04.264706”,“stdout”:“使用现有的 /etc/mail/sendmail.conf 配置 sendmail?[Y ] ", "stdout_lines": ["用现有的 /etc/mail/sendmail.conf 配置 sendmail?[Y] "]}
问题是什么?还有其他方法可以从 Ansible 执行 sendmailconfig 命令吗?
解决方案
你可能有两个,如果不是三个问题:
混合语法
该expect
模块有两种语法,您可以混合使用。
您必须使用它,只需提供后续响应:
responses:
Questions:
- response 1
- response 2
- response 3
您必须将它与(问题/响应)一起用作(键/值)对
responses:
question 1: response 1
question 2: response 2
question 3: response 3
当你做的是:
# Author disclaimer, this is a bad syntax!
responses:
Questions:
- question 1: response 1
- question 2: response 2
- question 3: response 3
提供给的问题expect
被解析为正则表达式
如果您使用问题/响应对,则问题将被解析为正则表达式。在您的问题中,您有 5 个符号需要转义才能被视为文字而不是正则表达式语法:/
、.
、?
和.[
]
因此,您的回复必须如下所示:
responses:
Configure sendmail with the existing \/etc\/mail\/sendmail\.conf\? \[Y\] : y
Configure sendmail with the existing \/etc\/mail\/sendmail\.mc\? \[Y\] : y
Reload the running sendmail now with the new configuration\? \[Y\] : y
您可以通过在正则表达式解释器中提出问题来轻松识别此类问题,例如regex101
- 它可以很容易地看到解析器将什么视为令牌:https ://regex101.com/r/vucKjy/1/
- 然后您只需要转义它们并验证您的问题是否与正则表达式匹配https://regex101.com/r/uEjrio/1/
你的超时时间可能太低了
你timeout
可能太低了,即使设置60
它仍然超过我的 docker 测试环境的超时时间,我不得不将它提高到120
它才能工作。
接近解决方案的两种可能方法
- 问题/响应作为键/值:
---
- hosts: localhost
connection: local
tasks:
- name: Exec sendmailconfig
expect:
command: sendmailconfig
responses:
Configure sendmail with the existing \/etc\/mail\/sendmail\.conf\? \[Y\] : y
Configure sendmail with the existing \/etc\/mail\/sendmail\.mc\? \[Y\] : y
Reload the running sendmail now with the new configuration\? \[Y\] : y
timeout: 120
- 回复列表
---
- hosts: localhost
connection: local
tasks:
- name: Exec sendmailconfig
expect:
command: sendmailconfig
responses:
Questions:
- y
- y
- y
timeout: 120
这两种语法都给我带来了这个回顾:
PLAY [localhost] ****************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]
TASK [Exec sendmailconfig] ******************************************************************************************************************************************
changed: [localhost]
PLAY RECAP **********************************************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
推荐阅读
- python - 错误的 str.join 实现行为?
- java - 进程在 Linux 中通过与 putty 的 ssh 连接被杀死
- java - 方形网格上的原始方程是发散的(计算流体动力学)
- python - 循环遍历字符串python中的json数组
- amazon-cloudformation - 如何在 AWS codestar 中设置环境属性?
- c# - C# WPF - 自定义控件拖放(Visual Studio 风格)
- go - 通过导入父目录,使用 golang 导入子包?
- laravel - Laravel sortBy()无法对集合进行排序
- git - 使用 Git,让人们看到同一项目的不同版本的最佳方式是什么?
- c# - Linq 语句查询,可能的组