如何通过Ansible远程执行多个模块Python?
How to remotely execute Python with more than one module through Ansible?
简介
我想使用 Ansible 在远程主机上 运行 Python 代码 (test.py
)。在我将一些功能提取到另一个模块之前,这一直不是问题。
当 运行使用 ansible-playbook test.yml
编写剧本时(内容如下,删除了一些细节):
- name: Perform tests
hosts: ...
vars_files:
...
tasks:
- name: run test script
script: "{{ playbook_dir }}/scripts/test.py
我收到以下错误:
Traceback (most recent call last):
File "/home/<removed>/.ansible/tmp/ansible-tmp-1515175369.61-59422518925475/test.py", line 11, in <module>
import utils
ImportError: No module named utils
但是,运行ning python scripts/test.py
在目标机器上 运行s 很顺利。
详情
这是我的目录布局:
+ scripts/
- __init__.py
- test.py
- utils.py
在 test.py
里面,我有这一行:
import utils
问题
如何从 test.py 中导入 utils
模块?
How can I import the utils module from inside test.py?
您需要将所有文件传输到目标机器(带有copy
模块)并在目标机器上执行python
命令(带有command
模块和chdir
选项)。
你不能使用 script
模块来做到这一点——它不会传输参数中给出的文件以外的文件。
简介
我想使用 Ansible 在远程主机上 运行 Python 代码 (test.py
)。在我将一些功能提取到另一个模块之前,这一直不是问题。
当 运行使用 ansible-playbook test.yml
编写剧本时(内容如下,删除了一些细节):
- name: Perform tests
hosts: ...
vars_files:
...
tasks:
- name: run test script
script: "{{ playbook_dir }}/scripts/test.py
我收到以下错误:
Traceback (most recent call last):
File "/home/<removed>/.ansible/tmp/ansible-tmp-1515175369.61-59422518925475/test.py", line 11, in <module>
import utils
ImportError: No module named utils
但是,运行ning python scripts/test.py
在目标机器上 运行s 很顺利。
详情
这是我的目录布局:
+ scripts/
- __init__.py
- test.py
- utils.py
在 test.py
里面,我有这一行:
import utils
问题
如何从 test.py 中导入 utils
模块?
How can I import the utils module from inside test.py?
您需要将所有文件传输到目标机器(带有copy
模块)并在目标机器上执行python
命令(带有command
模块和chdir
选项)。
你不能使用 script
模块来做到这一点——它不会传输参数中给出的文件以外的文件。