简体   繁体   English

从Ansible脚本运行Python函数

[英]Running a Python function from Ansible script

I have a Django project hosted on a remote server. 我在远程服务器上托管了一个Django项目。 This contains a file called tmp_file.py . 它包含一个名为tmp_file.py的文件。 There's a function called fetch_data() inside that file. 该文件中有一个名为fetch_data()的函数。 Usually I follow the below approach to run that function. 通常,我遵循以下方法来运行该功能。

# Inside Django Project

$ python manage.py shell

[Shell] from tmp_file import feth_data

[Shell] fetch_data()

Also the file doesn't contain __name__ section. 而且该文件不包含__name__部分。 So can't run as a stand alone script. 因此不能作为独立脚本运行。 What's the best way to perform this task using Ansible. 使用Ansible执行此任务的最佳方法是什么? I couldn't find anything useful from Ansible docs. 我从Ansible文档中找不到任何有用的东西。

There's --command switch for shell django-admin command . shell django-admin command--command开关。

So you can try in Ansible: 因此,您可以尝试使用Ansible:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM