I need to run an interactive script, to get the application client installed on my servers using Ansible playbook. During the installation it asks for IP address, port number, server name, username and password.
- name: Install application client
hosts: all
tasks: Run the script
- name: Execute the user interactive script
command: /home/ansible/install.sh
Below prompts for the responses
Enter IP: **1.2.3.4**
Enter Port: **440**
Enter Server Name: **AppServerName**
Connectivity Succeeded
Enter Username: **UserName**
Enter Password: **xxxx**
I would like to know how we can predefine these responses in playbook itself and pick it when it prompts for?
Thanks, Jean Thomas
Adding this as an answer. As the shell script you are trying to run "expects" some responses, we need to supply those responses using Linux expect.
Let's say we have a simple shell script test.sh like below. It takes IP address and Port, then runs the nc command:
#!/bin/bash
echo "IP address:"
read ip_addr
echo "Port:"
read port
nc -vz $ip_addr $port
To run this script from Ansible with expect, then we would have a simple playbook as below:
- hosts: localhost
vars:
send_ip_addr: "1.2.3.4"
send_port: "22"
tasks:
- shell: |
spawn ./test.sh
expect "IP address:"
send -- "{{ send_ip_addr }}\n"
expect "Port:"
send -- "{{ send_port }}\n"
expect eof
args:
executable: /usr/bin/expect
Linux expect is a scripting language in itself, and what we have above is a simple .exp script within the Ansible shell task. I think we only can set timeout at the beginning. See the manpage for all supported options.
There is also a useful autoexpect command that will create a script.exp script for us. Example:
autoexpect test.sh
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With