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

时间:2018-11-13 22:10:29

标签: python linux ansible

这是在Linux机器上。我有这样的run.yml

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

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

test.py通过'import helper'使用python文件(helper.py),该文件与ansible-playbook处于同一路径,并且在运行playbook.yml时,仍然给我一个'导入错误:无法导入名称帮手'。我该怎么办?

1 个答案:

答案 0 :(得分:1)

test.pyhelper.py复制到远程计算机上的同一目录(可能复制到临时目录)中,并将python test.py作为command任务运行。像这样:

- 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}}"