微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Ansible在远程服务器上执行shell脚本?

我打算在远程服务器上使用可执行的安装脚本来执行一个 shell脚本.

test.sh:

touch test.txt

剧本:

---
- 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

当我运行该手册时,传输成功发生,但脚本不执行.

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

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

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

它应该这样做.

您不需要在命令行中重复sudo,因为您已经在playbook中定义了sudo.

根据Ansible Intro to Playbooks用户参数在Ansys 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

原文地址:https://www.jb51.cc/bash/385192.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐