2017-08-11 87 views
1

我想執行一個使用腳本模塊來運行自定義python腳本的ansible劇本。 這個自定義的python腳本正在導入另一個python腳本。 執行劇本時,在嘗試導入util腳本時ansible命令失敗。我是新人,請幫助!Ansible劇本與嵌套的python腳本

helloWorld.yaml:

- hosts: all 
    tasks: 
    - name: Create a directory 
    script: /ansible/ems/ansible-mw-tube/modules/createdirectory.py "{{arg1}}" 

createdirectory.py - 在YAML劇本配置

#!/bin/python 

import sys 
import os 
from hello import HelloWorld 


class CreateDir: 
     def create(self, dirName,HelloWorldContext): 
       output=HelloWorld.createFolder(HelloWorldContext,dirName) 
       print output 
       return output 
def main(dirName, HelloWorldContext): 
     c = CreateDir() 
     c.create(dirName, HelloWorldContext) 
if __name__ == "__main__": 
     HelloWorldContext = HelloWorld() 
     main(sys.argv[1],HelloWorldContext) 
     HelloWorldContext = HelloWorld() 

hello.py腳本 - util的,其在導入的腳本上面寫的主要腳本

#!/bin/python 

import os 
import sys 

class HelloWorld: 

     def createFolder(self, dirName): 
      print dirName 
      if not os.path.exists(dirName): 
       os.makedirs(dirName) 
       print dirName 
       if os.path.exists(dirName): 
        return "sucess" 
       else: 
        return "failure" 

Ansible可執行命令

ansible-playbook -v -i /ansible/ems/ansible-mw-tube/inventory/helloworld_host /ansible/ems/ansible-mw-tube/playbooks/helloWorld.yml -e "arg1=/opt/logs/helloworld" 

Ansible版本

ansible --version 
[WARNING]: log file at /opt/ansible/ansible.log is not writeable and we cannot create it, aborting 

ansible 2.2.0.0 
    config file = /etc/ansible/ansible.cfg 
    configured module search path = Default w/o overrides 

回答

1

script模塊拷貝腳本到遠程服務器,並使用shell命令執行它。它找不到util腳本,因爲它不會傳輸該文件 - 它不知道它需要這樣做。

您有幾個選項,例如使用copy將兩個文件都移動到服務器並使用shell來執行它們。但是因爲你似乎在創建一個目錄,file模塊可以爲你做到這一點,而無需腳本。