Directory Structure:
|-- Script | |-- Name.py | |-- Name.pyc | |-- Test.py | |-- __init__.py | `-- __pycache__ | `-- Name.cpython-36.pyc |-- SimplePlaybook.yml |-- ansible.cfg |-- ansible_log.log |-- hosts
Script Folder:
Name.py
student_name = "Jhon Doe"
Test.py
import Name print("My Name is %s \n" % Name.student_name) sys.exit({"rc": 0, "changed": True})
Playbook:
--- - hosts: HOST gather_facts: True tasks: - name: Print Gathered Facts debug: var: hostvars - debug: msg: "{{ playbook_dir }}" - name: Run Test Script script: "{{ playbook_dir }}/Script/Test.py" register: result args: executable: python3 - debug: var: result ...
Issue:
import error of Name.py, module not found
Solution ??
Couple of things, your Python path is getting lost so you need to set an environment variable
--- - hosts: localhost gather_facts: True tasks: - name: Run Test Script script: "{{ playbook_dir }}/Script/Test.py" register: result args: executable: /usr/local/bin/python3 environment: - PYTHONPATH: "{{ playbook_dir }}/Script/"
And I'm not sure what you are trying to do with sys.exit. sys.exit should just take an integer. Exiting with a 0 means that program ran successfully.
import Name print("My Name is %s \n" % Name.student_name) print('{"rc": 0, "changed": True}') exit(0)
Couple of things, your Python path is getting lost so you need to set an environment variable
--- - hosts: localhost gather_facts: True tasks: - name: Run Test Script script: "{{ playbook_dir }}/Script/Test.py" register: result args: executable: /usr/local/bin/python3 environment: - PYTHONPATH: "{{ playbook_dir }}/Script/"
And I'm not sure what you are trying to do with sys.exit. sys.exit should just take an integer. Exiting with a 0 means that program ran successfully.
import Name print("My Name is %s \n" % Name.student_name) print('{"rc": 0, "changed": True}') exit(0)
when ansible excute Test.py , it thoughs an error in first line as it unble to find Name module.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.