在Ansible中使用脚本模块运行python脚本时导入错误?

问题描述

目录结构:

    |-- Script
    |   |-- Name.py
    |   |-- Name.pyc
    |   |-- Test.py
    |   |-- __init__.py
    |   `-- __pycache__
    |       `-- Name.cpython-36.pyc
    |-- SimplePlaybook.yml
    |-- ansible.cfg
    |-- ansible_log.log
    |-- hosts

脚本文件夹:

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

剧本:

---

-

  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

...

我收到导入错误。

问题:

Name.py导入错误,找不到模块

调试观察:

当ansible运行Test.py文件时,它将从以下位置运行 ansible/tmp/ansible-xxxx 我试图在python路径中添加该路径,但仍然出现Name.py

的导入错误

此处需要输入以解决此问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...