Ansible使用expect模块安装Elasticsearch插件无法响应确认提示问题
调整方案
你遇到的问题是Ansible的expect模块响应规则配置错误导致的,可选择以下任意一种方案解决:
方案1:修正expect模块配置
Ansible的expect模块responses配置项的键需要匹配命令输出的提示文本,不能固定写Question,同时提示文本里的[]是正则特殊字符,需要转义处理。
修改后的任务配置如下:
- name: Install Elasticsearch Plugins for AWS expect: command: "/usr/share/elasticsearch/bin/elasticsearch-plugin install discovery-ec2" responses: # 直接匹配确认提示文本,转义正则特殊字符[] 'Continue with installation\? \[y/N\]': 'Y' echo: yes timeout: 30 # 注意:expect模块依赖目标主机的pexpect库,可提前安装 pre_tasks: - name: Install pexpect dependency for expect module yum: name: python3-pexpect state: present
方案2(更推荐):使用elasticsearch-plugin自带的非交互参数
Elasticsearch插件安装命令本身支持--batch参数,可以跳过所有交互确认直接安装,不需要引入expect模块,稳定性更高:
- name: Install Elasticsearch Plugins for AWS command: "/usr/share/elasticsearch/bin/elasticsearch-plugin install --batch discovery-ec2" # 可添加changed_when规则优化Ansible状态输出 changed_when: "'Installed discovery-ec2' in plugin_install_result.stdout" register: plugin_install_result
内容的提问来源于stack exchange,提问作者Stefan Movila
相关产品推荐
相关产品推荐

