简体   繁体   中英

How to use ansible to run a shell file which needs an interrupt action

I'm using ansible 1.9.4 to run a shell bash in server, and I have a trouble. For example, I have a jdk file named jdk-1_5_0_22-linux-amd64.bin and I want to do something like the command below to install java:

./jdk-1_5_0_22-linux-amd64.bin

But when I run this command, the screen shown me the License Agreement and asked me "Press space or Enter" to scroll that, then press "yes" to confirm.

Or when I try to run a shell bash that includes something like:

echo "restart ntp ?[y/n]"
read check

I have tried it in ansible by shell, script, command modules but it didn't run for me, ansible was paused. Look like ansible was waiting for my interrupt action. How can I do this? I can't find it anywhere in the documentation.

You can use either yes (see: man yes ) or expect . Whereas yes continuously sends y answer to any process:

 yes | install.sh

expect will listen for an expect output and then send a reply accordingly.

(install expect , apt-get install expect , yes should be a part of you linux distro already)

#!/usr/bin/expect

set timeout 10
spawn ./jdk-1_5_0_22-linux-amd64.bin

expect "Input your name:"
send "Quoc Lap\r"

interact

This little script will output your name if jdk install process ask for "input your name:" . Note that the name string ends with a return character \\r

interact will make sure to close and send the proper exit code.

man expect will provide you with examples and info on how to deal with more complicated flows.

Finally

You ansible playbook will then need to call the script above

- name: Install Java JDK (automatic install)
  shell: ./jdk_install.sh
  args:
    chdir=/path/to/your/script

If you don't need to run from a specific location just prepend the path to the shell action and remove the args-part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM