简体   繁体   English

如何使用 Ansible Playbook 导入 python 文件?

[英]How do I import a python file using Ansible Playbook?

This is on a Linux machine.这是在 Linux 机器上。 I have a run.yml like this我有一个像这样的 run.yml

---
- name: Appspec
  hosts: localhost
  become: true

  tasks:
  - name: test 1
    script: test.py

test.py uses a python file (helper.py) by 'import helper' which is in the same path as the ansible-playbook and while running the playbook.yml it still gives me a 'Import Error: cannot import name helper'. test.py 使用“import helper”的python文件(helper.py),该文件与ansible-playbook位于同一路径,并且在运行playbook.yml时它仍然给我一个“导入错误:无法导入名称助手”。 How should I do this?我该怎么做?

Copy both test.py and helper.py over to same directory on the remote machine (possibly to a temporary directory) and run python test.py as a command task.test.pyhelper.py复制到远程机器上的同一目录(可能到临时目录)并运行python test.py作为command任务。 Something like this:像这样的东西:

- name: Create temporary directory
  tempfile:
    state: directory
  register: tmpdir

- name: Copy test.py
  copy:
    src: /wherever/test.py
    dest: "{{tmpdir.path}}/test.py"

- name: Copy helper.py
  copy:
    src: /wherever/helper.py
    dest: "{{tmpdir.path}}/helper.py"

- name: Run test.py
  command: python test.py
  args:
    chdir: "{{tmpdir.path}}"

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

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