通过ansible运行Python脚本 [英] Running Python script via ansible
问题描述
我正在尝试从 ansible 脚本运行 python 脚本.我认为这将是一件容易的事情,但我无法弄清楚.我有一个这样的项目结构:
I'm trying to run a python script from an ansible script. I would think this would be an easy thing to do, but I can't figure it out. I've got a project structure like this:
playbook-folder
roles
stagecode
files
mypythonscript.py
tasks
main.yml
release.yml
我正在尝试在 main.yml 中的任务中运行 mypythonscript.py(这是在 release.yml 中使用的角色).任务如下:
I'm trying to run mypythonscript.py within a task in main.yml (which is a role used in release.yml). Here's the task:
- name: run my script!
command: ./roles/stagecode/files/mypythonscript.py
args:
chdir: /dir/to/be/run/in
delegate_to: 127.0.0.1
run_once: true
我也试过../files/mypythonscript.py.我认为 ansible 的路径与剧本有关,但我猜不是?
I've also tried ../files/mypythonscript.py. I thought the path for ansible would be relative to the playbook, but I guess not?
我还尝试调试以找出我在脚本中间的位置,但也没有运气.
I also tried debugging to figure out where I am in the middle of the script, but no luck there either.
- name: figure out where we are
stat: path=.
delegate_to: 127.0.0.1
run_once: true
register: righthere
- name: print where we are
debug: msg="{{righthere.stat.path}}"
delegate_to: 127.0.0.1
run_once: true
那只是打印出.".很有帮助...
That just prints out ".". So helpful ...
推荐答案
尽量使用 script 指令,它对我有用
try to use script directive, it works for me
我的 main.yml
my main.yml
---
- name: execute install script
script: get-pip.py
和get-pip.py文件应该在文件中,作用相同
and get-pip.py file should be in files in the same role
这篇关于通过ansible运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!