ansibleを使用し、Expectツールを使用してスクリプトの実行を続行します。

ansibleを使用し、Expectツールを使用してスクリプトの実行を続行します。

私の予想スクリプト:

#!/usr/bin/expect -f

set timeout 1    
spawn /opt/install.sh

expect  "\[input\] Are you installing the application at the central data center? \[yes/no default: yes\]? \[yes\]\r"
send    "yes\r"

expect  "\[input\] What is the code of central data center \[default: 01\]? \[01\]\r"
send    "01\r"

expect  "What is ip or hostname of your server \[default: localhost\]? \[localhost\]\r"
send     "portal\r"

interact

mkdir /opt/dir1
mkdir /opt/dir2
cp /root/file1 /opt/dir1

Ansibleを介してこのスクリプトを実行すると、問題が終了した後にスクリプトが停止し、他のコマンドmkdir /opt/dir1などは実行されません。

ベストアンサー1

interactエスケープ条件が作成されないと終了しない対話型ループです。ssh -e escape_charこれをサポートするために適切なコードを書く必要があることを除いて、この関数に似ています。

#!/usr/bin/env expect
spawn expect
expect "> "
interact {
  "~" { return }
}
send "exit\r"
expect eof

実行時間:

% ./interact
spawn expect
expect1.1> puts hi         # this was typed under interact
hi
expect1.2> exit            # this is where ~ was pressed
% 

おすすめ記事