簡體   English   中英

如何使用 Ansible 在遠程服務器上執行 shell 腳本?

[英]How to execute a shell script on a remote server using Ansible?

我計划使用 Ansible 劇本在遠程服務器上執行 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

當我運行劇本時,傳輸成功,但腳本沒有執行。

您可以使用腳本模塊

例子

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

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

local_action在本地服務器上運行命令,而不是在您在hosts參數中指定的服務器上運行。

將“執行腳本”任務更改為

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

它應該這樣做。

您不需要在命令行中重復 sudo ,因為您已經在劇本中定義了它。

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

最好為此使用script模塊:
http://docs.ansible.com/script_module.html

對於想要臨時命令的人

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

或使用相對路徑

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

與所有其他答案和評論相反,使用script模塊有一些缺點。 特別是當您在遠程(不是本地主機)主機上運行它時。 這是官方 ansible 文檔的片段:

通常最好編寫 Ansible 模塊而不是推送腳本。 將您的腳本轉換為 Ansible 模塊以獲得獎勵積分!

ssh 連接插件將在執行腳本時通過 -tt 強制分配偽 tty。 偽 tty 沒有標准錯誤通道,所有標准錯誤都發送到標准輸出。 如果您依賴分離的 stdout 和 stderr 結果鍵,請切換到復制+命令任務集,而不是使用腳本。

如果本地腳本的路徑包含空格,則需要加引號。

Windows 目標也支持此模塊。

例如,使用script模塊為localhost 之外的任何主機運行此腳本,並注意腳本的stdoutstderr

#!/bin/bash


echo "Hello from the script"

nonoexistingcommand

echo "hello again"

你會得到類似下面的東西; 請注意stdout已合並所有stderr 。(理想情況下, line 6: nonoexistingcommand: command not found應該在stderr中)因此,如果您在腳本 output 的標准輸出中搜索一些 substring。 您可能會得到不正確的結果。:

 ok: [192.168.122.83] => {
    "script_out": {
        "changed": true,
        "failed": false,
        "rc": 0,
        "stderr": "Shared connection to 192.168.122.83 closed.\r\n",
        "stderr_lines": [
            "Shared connection to 192.168.122.83 closed."
        ],
        "stdout": "Hello from the script\r\n/home/ps/.ansible/tmp/ansible-tmp-1660578527.4335434-35162-230921807808160/my_script.sh: line 6: nonoexistingcommand: command not found\r\nhello again\r\n",
        "stdout_lines": [
            "Hello from the script",
            "/home/ps/.ansible/tmp/ansible-tmp-1660578527.4335434-35162-230921807808160/my_script.sh: line 6: nonoexistingcommand: command not found",
            "hello again"
        ]
    }
}

該文檔不鼓勵用戶使用腳本模塊; 考慮將您的腳本轉換為 ansible 模塊; 這是我的一個 簡單帖子,解釋了如何將您的腳本轉換為 ansible 模塊。

您可以在 ansible 執行本地腳本,而無需將文件傳輸到遠程服務器,這樣:

ansible my_remote_server -m shell -a "`cat /localpath/to/script.sh`"

由於沒有關於“腳本”的任何定義,意味着復雜性、內容、運行時、運行時環境、大小、要執行的任務等都是未知的,因此可以使用不推薦的方法,例如“如何復制命令中提供的內容使用 Ansible 在文件中使用特殊字符提示?

---
- hosts: test
  become: false
  gather_facts: false

  tasks:

  - name: Exec sh script on Remote Node
    shell:
      cmd: |
        date
        ps -ef | grep ssh
        echo "That's all folks"
    register: result

  - name: Show result
    debug:
      msg: "{{ result.stdout }}"

這是一個多行 shell 命令,並導致 output

TASK [Show result] ****************************************************
ok: [test.example.com] =>
  msg: |-
    Sat Sep 3 21:00:00 CEST 2022
    root        709      1  0 Aug11 ?        00:00:00 /usr/sbin/sshd -D
    root     123456    709 14 21:00 ?        00:00:00 sshd: user [priv]
    user     123456 123456  1 21:00 ?        00:00:00 sshd: user@pts/0
    root     123456 123456  0 21:00 pts/0    00:00:00 grep ssh
    That's all folks

可以添加更多行、復雜性、必要的 output 等。


由於script模塊 - 傳輸后在遠程節點上運行本地腳本 - 注釋

通常最好編寫 Ansible 模塊而不是推送腳本。

我還建議熟悉編寫自己的模塊,正如P.... 的答案中已經提到的那樣

您可以使用模板模塊將本地機器上是否存在腳本復制到遠程機器並執行它。

 - 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM