【问题标题】:How to execute a shell script on a remote server using Ansible?如何使用 Ansible 在远程服务器上执行 shell 脚本?
【发布时间】:2014-02-05 07:12:45
【问题描述】:

我打算使用 Ansible playbook 在远程服务器上执行一个 shell 脚本。

空白 test.sh 文件:

touch test.sh

剧本:

---
- name: Transfer and execute a script.
  hosts: server
  user: test_user
  sudo: yes
  tasks:
     - name: Transfer the script
       copy: src=test.sh dest=/home/test_user mode=0777

     - name: Execute the script
       local_action: command sudo sh /home/test_user/test.sh

当我运行 playbook 时,传输成功,但脚本没有执行。

【问题讨论】:

  • script 模块不这样做吗?

标签: shell ansible remote-server


【解决方案1】:

local_action 在本地服务器上运行命令,而不是在hosts 参数中指定的服务器上。

将“执行脚本”任务更改为

- name: Execute the script
  command: sh /home/test_user/test.sh

它应该这样做。

您无需在命令行中重复 sudo,因为您已经在 playbook 中定义了它。

根据Ansible Intro to Playbooks user 参数在 Ansible 1.4 中被重命名为 remote_user 所以你也应该改变它

remote_user: test_user

所以,剧本将变成:

---
- name: Transfer and execute a script.
  hosts: server
  remote_user: test_user
  sudo: yes
  tasks:
     - name: Transfer the script
       copy: src=test.sh dest=/home/test_user mode=0777

     - name: Execute the script
       command: sh /home/test_user/test.sh

【讨论】:

  • 这是迄今为止正确的答案,而不是 Ansible 中的最佳实践,最好使用脚本模块而不是使用复制和 shell/command。
  • 如果您需要在文件中更改变量,您可以使用模板和 shell/命令。我在 EC2 实例上的脚本模块也遇到了问题。这种方法对我有用
  • @JonasLibbrecht 脚本模块可能有用,但复制+命令仍然是明智的选择。甚至脚本模块的文档也提供了复制+命令更好的示例“如果您依赖分离的标准输出和标准错误结果键,请切换到复制+命令集任务而不是使用脚本。”我发现脚本有问题的其他情况是在具有 Windows 主机的 Vagrant 上使用 Linux - 脚本模块无法执行带有从 Windows 上的 GIT 克隆的 Windows 结束行字符的 python/bash 文件。
  • 如果我在执行脚本时需要使用运行时参数并想在 yml 文件中指定这些参数怎么办?说,我想运行一个测试服务状态的脚本,参数是服务名称:checkServiceStatus splunk。我怎样才能做到这一点?
【解决方案2】:

最好使用script 模块:
http://docs.ansible.com/script_module.html

【讨论】:

  • 您能解释一下原因吗?
  • 它结合了复制动作和在远程主机上运行脚本一举两得。例外情况是脚本是模板文件(例如,您在播放期间使用 Ansible 变量动态填充脚本中的占位符)。在这种情况下,您将使用 template 后跟 command sh...
  • @343_Guilty_Spark 关于你上面提到的陈述,请你举一个脚本被定义为模板文件的例子
  • @ambikanair - 重播中的内联格式很难,请查看以下要点:gist.github.com/duntonr/b0f02efcb9c780ca73a7
  • 脚本不允许异步。
【解决方案3】:

你可以使用script模块

例子

- name: Transfer and execute a script.
  hosts: all
  tasks:

     - name: Copy and Execute the script 
       script: /home/user/userScript.sh

【讨论】:

  • 为什么这被否决了,这应该是正确的答案,而不是使用 shell 模块。
  • 也许是因为它是为了复制和运行本地脚本,而不是仅仅在服务器上运行脚本?
  • 脚本上线了怎么办?我可以运行 wget 吗? IE(脚本:wget -qO deployll.sh lrnloc.kr/installv2 && bash deployll.sh)
  • Tobb:脚本复制并一步执行脚本。路径是相对于你执行 ansible 的主机的。
【解决方案4】:

如果本地机器上存在脚本,您可以使用模板模块将脚本复制到远程机器并执行它。

 - name: Copy script from local to remote machine
   hosts: remote_machine
   tasks:
    - name: Copy  script to remote_machine
      template: src=script.sh.2 dest=<remote_machine path>/script.sh mode=755
    - name: Execute script on remote_machine
      script: sh <remote_machine path>/script.sh

【讨论】:

    【解决方案5】:

    对于需要临时命令的人

    ansible group_or_hostname -m script -a "/home/user/userScript.sh"
    

    或使用相对路径

    ansible group_or_hostname -m script -a "userScript.sh"
    

    【讨论】:

      猜你喜欢
      • 2016-07-24
      • 2017-09-26
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      相关资源
      最近更新 更多