0

I am running the following script inside 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()

However, I get the following error: failed=True msg='boto required for this module'

However, according to this comment(https://github.com/ansible/ansible/issues/5734#issuecomment-33135727), it works.

But, I'm not understanding how do I mention that in my script? Or, can I have a separate hosts file, and include it in the script, like how I call my playbook?

If so, then how?

[EDIT - 1] I have added the line inventory=ansible.inventory.Inventory('hosts') with hosts file as:

[localhost]
127.0.0.1 ansible_python_interpreter=/usr/local/bin/python

But, I get this error: /bin/sh: /usr/local/bin/python: No such file or directory

So, where is python located inside AWS Lambda?

I installed boto just like I installed other packages in the Lambda's deployment package: pip install boto -t <folder-name>

Dawny33
  • 10,543
  • 21
  • 82
  • 134
  • Why is there no mention of how did you install boto? Instead you refer to an article regarding the default directory structure of the Homebrew package manager on macOS? What's the connection to AWS Lambda here? – techraf Mar 06 '17 at 04:36
  • @techraf Updated the answer. In AWS Lambda, all the packages and the scripts should reside in the same root folder. [The script is working well in my local]. So, there must be some problem with where Lambda takes in the boto package from. – Dawny33 Mar 06 '17 at 04:39

1 Answers1

0

The bash command which python will usually give the location of the Python binary. There's an example of how to call a bash script from AWS Lambda here.

Community
  • 1
  • 1
Willem van Ketwich
  • 5,666
  • 7
  • 49
  • 57