BHY
BHY

Reputation: 91

Ansible user-interactive module while running shell script

I am new to ansible and trying to learn basic things...

I am trying to execute a below shell script with ansible...This script has case condition included which needs a user input. But while running in ansible, it's not asking anything and completing a task. I want my ansible to ask a i/p during executing and accordingly execute the script.

- hosts: localhost
  tasks:
  - name: Execute the script
    shell: /usr/bin/sh /root/ansible_testing/testing.sh

snippet of shell script is...

 echo "Press 1 for Backup"
 echo "Press 2 for MakeChange"
 echo "Press 3 for Keepsafe"
 echo "*******************************************"

 echo -n "Enter your choice : "
 read choice2

 case $choice2 in
 1)

   echo "Hello"

Upvotes: 9

Views: 26016

Answers (1)

kfreezy
kfreezy

Reputation: 1579

There's a bunch of ways you can do this with just bash (there's a pretty good answer here). However if you want to accomplish this the Ansible way you could use the expect module.

- name: Execute the script
  expect:
    command: /usr/bin/sh /root/ansible_testing/testing.sh
    responses:
      "Enter your choice": "1"

In this example, Enter your choice is the regex ansible will match on to input a "1". If there are multiple prompts that use Enter your choice Ansible will input a "1" for all of them.

Upvotes: 10

Related Questions