user1857287
user1857287

Reputation: 51

How do I import a python file using Ansible Playbook?

This is on a Linux machine. I have a run.yml like this

---
- 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'. How should I do this?

Upvotes: 2

Views: 688

Answers (1)

jwodder
jwodder

Reputation: 57500

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

Upvotes: 1

Related Questions