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

时间:2022-05-08 14:37:56

I am planning to execute a shell script on a remote server using Ansible playbook.

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

test.sh:

test.sh:

touch test.txt

Playbook:

剧本:

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

When I run the playbook, the transfer successfully occurs but the script is not executed.

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

3 个解决方案

#1


22  

local_action runs the command on the local server, not on the servers you specify in hosts parameter.

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

Change your "Execute the script" task to

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

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

and it should do it.

它应该这样做。

You don't need to repeat sudo in the command line because you have defined it already in the playbook.

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

According to Ansible Intro to Playbooks user parameter was renamed to remote_user in Ansible 1.4 so you should change it, too

根据Ansible Intro to Playbooks,用户参数在Ansible 1.4中被重命名为remote_user,因此您也应该更改它

remote_user: test_user

So, the playbook will become:

所以,剧本将成为:

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

#2


56  

you can use script module

你可以使用脚本模块

Example

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

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

#3


17  

It's better to use script module for that:
http://docs.ansible.com/script_module.html

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

#1


22  

local_action runs the command on the local server, not on the servers you specify in hosts parameter.

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

Change your "Execute the script" task to

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

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

and it should do it.

它应该这样做。

You don't need to repeat sudo in the command line because you have defined it already in the playbook.

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

According to Ansible Intro to Playbooks user parameter was renamed to remote_user in Ansible 1.4 so you should change it, too

根据Ansible Intro to Playbooks,用户参数在Ansible 1.4中被重命名为remote_user,因此您也应该更改它

remote_user: test_user

So, the playbook will become:

所以,剧本将成为:

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

#2


56  

you can use script module

你可以使用脚本模块

Example

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

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

#3


17  

It's better to use script module for that:
http://docs.ansible.com/script_module.html

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