python脚本test.py:
class Test:
def __init__(self):
pass
def addition(self, a, b):
return a + b
下面是执行Python脚本的ansible playbook。
---
- name: execute install script
script: test.py
是否可以在ansible playbook执行期间仅调用test.py中的添加方法,而不是调用完整的python脚本
最佳答案
我认为这是不可能的。如果 test.py
包含许多类,您可以执行以下操作,这只会定义一个类,并且不会实例化它。
from test import Test
print(Test.addition(None, 3, 4)) # prints 7
如果您想单独导入函数,为什么要把它作为类方法?
您还可以执行以下操作:
测试.py
class Test:
def __init__(self):
pass
# Define function outside class so that it can be imported
def addition(self, a, b):
return a + b
# Add function to class
Test.addition = addition
然后执行from test importadd
关于python - 在ansible中运行python函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53022815/