运行shell脚本时安装用户交互模块

问题描述 投票:1回答:1

我是ansible的新手并且正在尝试学习基本的东西......

我正在尝试使用ansible执行以下shell脚本...此脚本包含需要用户输入的案例条件。但是当在ansible中运行时,它不会询问任何事情并完成任务。我希望我的ansible在执行期间询问i / p并相应地执行脚本。

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

shell脚本片段是......

 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"
ansible
1个回答
1
投票

有很多方法你可以用bash做这个(有一个非常好的答案here)。但是,如果你想用Ansible方式完成这个,你可以使用expect module

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

在这个例子中,Enter your choice是正则表达式,它将匹配输入“1”。如果有多个提示使用Enter your choice Ansible将为所有提示输入“1”。

© www.soinside.com 2019 - 2024. All rights reserved.