如何将ansible的“期望"模块用于多个不同的响应? [英] How to use ansible 'expect' module for multiple different responses?

查看:68
本文介绍了如何将ansible的“期望"模块用于多个不同的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我试图测试我的bash脚本是否提示四次.

Here I am trying to test my bash script where it is prompting four times.

#!/bin/bash
date >/opt/prompt.txt
read -p "enter one: " one
echo $one
echo $one >>/opt/prompt.txt
read -p "enter two: " two
echo $two
echo $two >>/opt/prompt.txt
read -p "enter three: " three
echo $three
echo $three >>/opt/prompt.txt
read -p "enter password: " password
echo $password
echo $password >>/opt/prompt.txt

对于该脚本,我编写了以下代码,并且工作正常

for this script I wrote the code below, and it is working fine

- hosts: "{{ hosts }}"
  tasks:
  - name: Test Script
    expect:
      command: sc.sh
      responses:
        enter one: 'one'
        enter two: 'two'
        enter three: 'three'
        enter password: 'pass'
      echo: yes

但是,如果我对mysql_secure_installation命令执行相同操作,它将无法正常工作

But if I am doing the same for mysql_secure_installation command it not working

- hosts: "{{ hosts }}"
  tasks:
  - name: mysql_secure_installation Command Test
    expect:
      command: mysql_secure_installation
      responses:
        'Enter current password for root (enter for none):': "\n"
        'Set root password? [Y/n]:': 'y'
        'New password:': '123456'
        'Re-enter new password:': '123456'
        'Remove anonymous users? [Y/n]:': 'y'
        'Disallow root login remotely? [Y/n]:': 'y'
        'Remove test database and access to it? [Y/n]:': 'y'
        'Reload privilege tables now? [Y/n]:': 'y'

      echo: yes

及其引用在这里:

PLAY [S1] **********************************************************************

TASK [setup] *******************************************************************
ok: [S1]

TASK [mysql_secure_installation Command Test] **********************************
fatal: [S1]: FAILED! => {"changed": true, "cmd": "mysql_secure_installation", "delta": "0:00:30.139266", "end": "2016-07-15 15:36:32.549415", "failed": true, "rc": 1, "start": "2016-07-15 15:36:02.410149", "stdout": "\r\n\r\n\r\n\r\nNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL\r\n      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!\r\n\r\n\r\nIn order to log into MySQL to secure it, we'll need the current\r\npassword for the root user.  If you've just installed MySQL, and\r\nyou haven't set the root password yet, the password will be blank,\r\nso you should just press enter here.\r\n\r\nEnter current password for root (enter for none): ", "stdout_lines": ["", "", "", "", "NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL", "      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!", "", "", "In order to log into MySQL to secure it, we'll need the current", "password for the root user.  If you've just installed MySQL, and", "you haven't set the root password yet, the password will be blank,", "so you should just press enter here.", "", "Enter current password for root (enter for none): "]}

NO MORE HOSTS LEFT *************************************************************
    to retry, use: --limit @/home/jackson/AnsibleWorkSpace/AnsibleTest/example1.retry

PLAY RECAP *********************************************************************
S1                         : ok=1    changed=0    unreachable=0    failed=1  

我也尝试用空白''代替"\n"作为第一个答案,但是它也不起作用.我还访问了Ansible expect 文档,但它们仅显示了非常简单的示例和说明.我还在尝试对多个不同的响应进行正则表达式匹配,但也无法正常工作.
请不要推荐我使用Ansible的mysql模块,因为我的目的是学习此模块以备将来使用.

I have also tried blank '' instead of "\n" for the first answer but it is not working either. I also visited Ansible expect doc but they show only very simple example and explanation. I am also trying regex match for multiple different responses but it is also not working.
Please do not recommend me to use mysql module of Ansible, because here my purpose is to learn this module for future use.

推荐答案

原因是问题被解释为正则表达式.因此,您必须在正则表达式中转义具有特殊含义的字符,例如-()[] \?*.等.

The reason is that the questions are interpreted as regexps. Hence you must escape characters with a special meaning in regular expressions, such as -()[]\?*. et cetara.

因此:

'Enter current password for root (enter for none):'

应该是:

'Enter current password for root \(enter for none\):'

祝你好运!

这篇关于如何将ansible的“期望"模块用于多个不同的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆