2016-04-28 51 views
1

好的..在這裏,我去。這是我的main.yml內容。如何使用命令在命令任務中運行sh腳本

- hosts: web-servers 
    tasks: 
    - name: Run the below script 
     command: sh temp/myscript.sh 

我有一個名爲「myscript.sh」的shell腳本放在名爲'temp'的目錄下。我的目錄層次是:

  • ansible複製角色(目錄)
    • main.yml
    • 臨時(目錄)
      • myscript.sh(我要運行這個shell腳本)

我不想將shell腳本複製到我的主機服務器並從thre運行。我想運行shell腳本,它將隨main.yml文件一起提供。

當我運行這個劇本,我得到這樣一個錯誤:標準錯誤:SH:溫度/ myscript.sh:沒有這樣的文件或目錄

這個請大家幫忙。

回答

3

如果你記住這一點,你會發現Ansible簡單得多:

Ansible模塊在目標主機上運行。

如果你這樣做:

- name: run some command 
    command: foo 

然後該任務將嘗試在主機上運行命令foo到Ansible是連接,不是你正在運行Ansible主機上。

如果你想在本地運行一個任務,你需要明確這一點。例如:

- hosts: web-servers 
    tasks: 
    - name: Run the below script 
     delegate_to: localhost 
     command: sh temp/myscript.sh 

它使用delegate_to指令對 localhost此任務的執行,而不是取其web-servers主機這個任務 通常目標。

有關更多信息,請參見the Ansible documentation on delegation

1

除了@larsks的正確答案,local_actiondelegate_to: localhost的簡寫。因此,這可能變成:

- hosts: web-servers 
    tasks: 
    - name: Run the below script 
     local_action: command sh temp/myscript.sh 

使用same link作爲參考。