python - 如何使用 Ansible Playbook 导入 python 文件?
问题描述
这是在 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 时它仍然给我一个“导入错误:无法导入名称助手”。我该怎么做?
解决方案
将两者复制test.py
并复制helper.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}}"
推荐阅读
- c++ - 我可以使用winapi以非常特殊的方式映射网络驱动器吗?
- python - 获取行中数字的平均值 SQL (Sqlite3)
- r - 如何使用 R 计算数据集中不同组的平均值
- enums - 有没有办法将枚举文字与 UML 类图中的类相关联?
- python - 在python中的另一个类中创建具有随机参数的类的实例
- r - 防止在使用 ggsave() 的函数时生成 Rplot.emf
- reactjs - Reactjs - /dist 文件夹只有 2 个文件 - bundle.js 和 bundle.js.map - 不包含任何其他文件
- c# - 即使我遵循教程代码,我的屏幕绘图 Xamarin 代码也无法绘制
- ruby - 将 Swagger UI 集成到我的石板生成网站
- go - 给定一个端口,在 Golang 中返回相应的服务(例如 http、ssh、...)