我在AWS Lambda中运行以下脚本:
#!/usr/bin/python
from __future__ import print_function
import json
import os
import ansible.inventory
import ansible.playbook
import ansible.runner
import ansible.constants
from ansible import utils
from ansible import callbacks
print('Loading function')
def run_playbook(**kwargs):
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(
stats, verbose=utils.VERBOSITY)
# use /tmp instead of $HOME
ansible.constants.DEFAULT_REMOTE_TMP = '/tmp/ansible'
out = ansible.playbook.PlayBook(
callbacks=playbook_cb,
runner_callbacks=runner_cb,
stats=stats,
**kwargs
).run()
return out
def lambda_handler(event, context):
return main()
def main():
out = run_playbook(
playbook='little.yml',
inventory=ansible.inventory.Inventory(['localhost'])
)
return(out)
if __name__ == '__main__':
main()
但是,我收到以下错误:failed=True msg='boto required for this module'
但是,根据此评论(https://github.com/ansible/ansible/issues/5734#issuecomment-33135727),它可行。
但是,我不明白如何在我的脚本中提及它?或者,我可以有一个单独的hosts
文件,并将其包含在脚本中,就像我调用我的剧本一样吗?
如果是,那怎么样?
[编辑 - 1]
我添加了行inventory=ansible.inventory.Inventory('hosts')
使用hosts文件:
[localhost]
127.0.0.1 ansible_python_interpreter=/usr/local/bin/python
但是,我收到此错误:/bin/sh: /usr/local/bin/python: No such file or directory
那么,python位于AWS Lambda中的哪个位置?
我安装了boto
,就像我在Lambda的部署包中安装了其他软件包一样:pip install boto -t <folder-name>