2016-07-15 4 views
9

Hier versuche ich meine Bash Scripy zu testen, wo es vier Mal aufgefordert wird.Wie kann das ansible 'expect' Modul für mehrere verschiedene Antworten verwendet werden?

#!/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 

für dieses Skript i unten schrieb Code, und es funktioniert gut,

- 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 

Aber wenn ich es nicht für mich für mysql_secure_installation Befehl gleiche tue,

Arbeits
- 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 

und sein Trackback ist hier:

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 

Ich habe auch versucht '' anstelle von "\n" für die erste Antwort auszuprobieren, aber es funktioniert auch nicht. Ich besuchte auch ansible expect doc, aber hier sehr einfaches Beispiel und Erklärung. Ich versuche auch Regex-Match für mehrere verschiedene Antworten, aber es funktioniert auch nicht. Bitte empfehlen Sie mir nicht, Mysql-Modul von Ansible zu verwenden, weil hier mein Zweck ist, dieses Modul für zukünftige Verwendung zu lernen.

+0

Haben Sie eine Lösung dafür gefunden? – syodage

+0

Nein, ich habe keine Lösung gefunden. –

Antwort

14

Der Grund ist, dass die Fragen als Regexps interpretiert werden. Daher müssen Sie Zeichen mit einer speziellen Bedeutung in regulären Ausdrücken wie -() [] \? * Entziehen. et cetara. Daher

:

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

sollte stattdessen sein:

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

Viel Glück!

Verwandte Themen