2016-11-29 178 views
1

我有一個Django項目託管在遠程服務器上。這包含一個名爲tmp_file.py的文件。該文件中有一個名爲fetch_data()的功能。通常我會按照下面的方法來運行該功能。從Ansible腳本運行Python函數

# Inside Django Project 

$ python manage.py shell 

[Shell] from tmp_file import feth_data 

[Shell] fetch_data() 

此外該文件不包含__name__部分。所以不能作爲獨立的腳本運行。使用Ansible來執行此任務的最佳方式是什麼?我從Ansible文檔找不到任何有用的東西。

回答

2

There's --command switch for shell django-admin command

所以,你可以嘗試在Ansible:

- name: Fetch data 
    command: "django-admin shell --command='from tmp_file import feth_data; fetch_data()'" 
    args: 
    chdir: /path/to/tmp_file