无法在Ansible中使用参数执行sh脚本

时间:2019-04-17 11:34:19

标签: shell ansible sh

我正在尝试使用ansible剧本启动远程主机上已经存在的安装脚本。还需要一些参数。

- hosts: myhost
  become: yes
  gather_facts: no
  tasks:
    - name: Running an installation script
      command: sh /home/user/install.sh --param1 'param1' --param2 'param2' --param3 'param3' --param4 'param4'

但是它不起作用。当我尝试启动剧本时,得到以下输出:

fatal: [myhost]: FAILED! => {"changed": true, "cmd": ["sh", "/home/user/install.sh", "--param1", "param1", "----param1", "--param1", "--param3", "param3", "--param4", "param4"], "delta": "0:00:00.002254", "end": "2019-04-17 11:27:13.063837", "msg": "non-zero return code", "rc": 2, "start": "2019-04-17 11:27:13.061583", "stderr": "/home/user/install.sh: 4: set: Illegal option -o pipefail", "stderr_lines": ["/home/user/install.sh: 4: set: Illegal option -o pipefail"], "stdout": "", "stdout_lines": []}

3 个答案:

答案 0 :(得分:1)

脚本失败,并显示以下消息:

  

“ stderr_lines”:[“ /home/user/install.sh:4:设置:非法选项-o pipefail”]

尝试从字符串中读取命令。参见sh

command: sh -c "/home/user/install.sh --param1 'param1' ..."

从man shQing

  

-c选项导致从字符串操作数中读取命令        而不是来自标准输入。请记住,仅此选项        接受单个字符串作为其参数,因此必须包含多个单词的字符串        引用。

答案 1 :(得分:0)

根据文档建议(https://docs.ansible.com/ansible/latest/modules/command_module.html),您可以使用如下命令:

- name: Running an installation script
      command: /home/user/install.sh --param1 'param1' --param2 'param2' --param3 'param3' --param4 'param4'

如果脚本中包含shabang,则应该没问题。

另一种解决方案是像这样使用脚本(https://docs.ansible.com/ansible/latest/modules/script_module.html):

- name: Running an installation script
  script: /home/user/install.sh --param1 param1 --param2 param2 --param3 param3 --param4 param4

让我听听是否有帮助

答案 2 :(得分:0)

您可以使用shell模块而不是命令模块来将参数传递给专门针对脚本ansible的linux命令的

  • 名称:仅在远程节点上存在file.txt时运行脚本 脚本:/some/local/remove_file.sh --some-argument 1234 args: 删除:/the/removed/file.txt

和用于linux命令

like:shell:“ curl --socks5 localhost:9000 http://www.ansible.com

有关更多详细信息,请参见

https://docs.ansible.com/ansible/latest/modules/shell_module.html

相关问题