You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Ansible向脚本传递输入参数?

解决Ansible向脚本传递参数的问题

你的问题出在对Ansible script模块的responses参数用法理解有误:responses是用来自动回复脚本运行时的交互式输入请求(比如脚本里用read命令等待输入),而不是传递命令行参数。如果你的test.sh需要的是命令行位置参数,直接在脚本路径后追加参数即可。

正确写法

修改script任务,把portale作为命令行参数传给脚本:

- name: Transfer and execute a script.
  hosts: all
  vars_prompt:
    - name: "portale"   
      prompt: "Seleziona il portale?\n1- portal\n2- portal2\n"
      private: no

  tasks:
     - name: Copy and Execute the script
       script: /home/ansible/scripts/test.sh arg1 {{ portale }}

补充说明

如果你的test.sh确实需要交互式输入(比如脚本内有类似read -p "Seleziona il portale?" portal的代码),那才需要用responses参数,格式是键值对,键为脚本输出的提示文本,值为要回复的内容:

- name: Copy and Execute the script with interactive input
  script: /home/ansible/scripts/test.sh arg1
  args:
    responses:
      "Seleziona il portale?": "{{ portale }}"

内容的提问来源于stack exchange,提问作者Lealu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 23:58:15