@ -7,13 +7,11 @@
from __future__ import absolute_import , division , print_function
from __future__ import absolute_import , division , print_function
__metaclass__ = type
__metaclass__ = type
ANSIBLE_METADATA = { ' metadata_version ' : ' 1.1 ' ,
ANSIBLE_METADATA = { ' metadata_version ' : ' 1.1 ' ,
' status ' : [ ' preview ' ] ,
' status ' : [ ' preview ' ] ,
' supported_by ' : ' community ' }
' supported_by ' : ' community ' }
DOCUMENTATION = r '''
DOCUMENTATION = '''
- - -
- - -
module : serverless
module : serverless
short_description : Manages a Serverless Framework project
short_description : Manages a Serverless Framework project
@ -22,78 +20,86 @@ description:
version_added : " 2.3 "
version_added : " 2.3 "
options :
options :
state :
state :
choices : [ ' present ' , ' absent ' ]
description :
description :
- Goal state of given stage / project
- Goal state of given stage / project .
required : false
type : str
choices : [ absent , present ]
default : present
default : present
serverless_bin_path :
serverless_bin_path :
description :
description :
- The path of a serverless framework binary relative to the ' service_path ' eg . node_module / . bin / serverless
- The path of a serverless framework binary relative to the ' service_path ' eg . node_module / . bin / serverless
required : false
type : path
version_added : " 2.4 "
version_added : " 2.4 "
service_path :
service_path :
description :
description :
- The path to the root of the Serverless Service to be operated on .
- The path to the root of the Serverless Service to be operated on .
type : path
required : true
required : true
stage :
stage :
description :
description :
- The name of the serverless framework project stage to deploy to . This uses the serverless framework default " dev " .
- The name of the serverless framework project stage to deploy to .
required : false
- This uses the serverless framework default " dev " .
type : str
functions :
functions :
description :
description :
- A list of specific functions to deploy . If this is not provided , all functions in the service will be deployed .
- A list of specific functions to deploy .
required : false
- If this is not provided , all functions in the service will be deployed .
type : list
default : [ ]
default : [ ]
region :
region :
description :
description :
- AWS region to deploy the service to
- AWS region to deploy the service to .
required : false
- This parameter defaults to C ( us - east - 1 ) .
default : us - east - 1
type : str
deploy :
deploy :
description :
description :
- Whether or not to deploy artifacts after building them . When this option is ` false ` all the functions will be built , but no stack update will be
- Whether or not to deploy artifacts after building them .
run to send them out . This is mostly useful for generating artifacts to be stored / deployed elsewhere .
- When this option is C ( false ) all the functions will be built , but no stack update will be run to send them out .
required : false
- This is mostly useful for generating artifacts to be stored / deployed elsewhere .
default : true
type : bool
type : bool
default : yes
force :
force :
description :
description :
- Whether or not to force full deployment , equivalent to serverless ` - - force ` option .
- Whether or not to force full deployment , equivalent to serverless C ( - - force ) option .
required : false
type : bool
default : false
default : no
version_added : " 2.7 "
version_added : " 2.7 "
verbose :
verbose :
description :
description :
- Shows all stack events during deployment , and display any Stack Output .
- Shows all stack events during deployment , and display any Stack Output .
required : false
type : bool
default : false
default : no
version_added : " 2.7 "
version_added : " 2.7 "
notes :
notes :
- Currently , the ` serverless ` command must be in the path of the node executing the task . In the future this may be a flag .
- Currently , the C ( serverless ) command must be in the path of the node executing the task .
requirements : [ " serverless " , " yaml " ]
In the future this may be a flag .
author : Ryan Scott Brown ( @ryansb )
requirements :
- serverless
- yaml
author :
- Ryan Scott Brown ( @ryansb )
'''
'''
EXAMPLES = """
EXAMPLES = r'''
# Basic deploy of a service
- name : Basic deploy of a service
- serverless :
serverless :
service_path : ' {{ project_dir }} '
service_path : ' {{ project_dir }} '
state : present
state : present
# Deploy specific functions
- name : Deploy specific functions
- serverless :
serverless :
service_path : ' {{ project_dir }} '
service_path : ' {{ project_dir }} '
functions :
functions :
- my_func_one
- my_func_one
- my_func_two
- my_func_two
# deploy a project, then pull its resource list back into Ansible
- name : Deploy a project , then pull its resource list back into Ansible
- serverless :
serverless :
stage : dev
stage : dev
region : us - east - 1
region : us - east - 1
service_path : ' {{ project_dir }} '
service_path : ' {{ project_dir }} '
register : sls
register : sls
# The cloudformation stack is always named the same as the full service, so the
# The cloudformation stack is always named the same as the full service, so the
# cloudformation_facts module can get a full list of the stack resources, as
# cloudformation_facts module can get a full list of the stack resources, as
# well as stack events and outputs
# well as stack events and outputs
@ -102,15 +108,15 @@ EXAMPLES = """
stack_name : ' {{ sls.service_name }} '
stack_name : ' {{ sls.service_name }} '
stack_resources : true
stack_resources : true
# Deploy a project but use a locally installed serverless binary instead of the global serverless binary
- name : Deploy a project using a locally installed serverless binary
- serverless :
serverless :
stage : dev
stage : dev
region : us - east - 1
region : us - east - 1
service_path : ' {{ project_dir }} '
service_path : ' {{ project_dir }} '
serverless_bin_path : node_modules / . bin / serverless
serverless_bin_path : node_modules / . bin / serverless
"""
'''
RETURN = """
RETURN = r'''
service_name :
service_name :
type : str
type : str
description : The service name specified in the serverless . yml that was just deployed .
description : The service name specified in the serverless . yml that was just deployed .
@ -125,10 +131,9 @@ command:
description : Full ` serverless ` command run by this module , in case you want to re - run the command outside the module .
description : Full ` serverless ` command run by this module , in case you want to re - run the command outside the module .
returned : always
returned : always
sample : serverless deploy - - stage production
sample : serverless deploy - - stage production
"""
'''
import os
import os
import traceback
try :
try :
import yaml
import yaml
@ -147,7 +152,7 @@ def read_serverless_config(module):
config = yaml . safe_load ( sls_config . read ( ) )
config = yaml . safe_load ( sls_config . read ( ) )
return config
return config
except IOError as e :
except IOError as e :
module . fail_json ( msg = " Could not open serverless.yml in {0} . err: {1} " . format ( path , str ( e ) ) , exception = traceback . format_exc ( ) )
module . fail_json ( msg = " Could not open serverless.yml in {0} . err: {1} " . format ( path , str ( e ) ) )
module . fail_json ( msg = " Failed to open serverless config at {0} " . format (
module . fail_json ( msg = " Failed to open serverless config at {0} " . format (
os . path . join ( path , ' serverless.yml ' ) ) )
os . path . join ( path , ' serverless.yml ' ) ) )
@ -167,15 +172,15 @@ def get_service_name(module, stage):
def main ( ) :
def main ( ) :
module = AnsibleModule (
module = AnsibleModule (
argument_spec = dict (
argument_spec = dict (
service_path = dict ( required = True , type = ' path ' ) ,
service_path = dict ( type = ' path ' , required = True ) ,
state = dict ( default = ' present ' , choices = [ ' present' , ' absent ' ] , required = False ) ,
state = dict ( type = ' str ' , default = ' present ' , choices = [ ' absent' , ' present ' ] ) ,
functions = dict ( type = ' list ' , required = False ),
functions = dict ( type = ' list ' ),
region = dict ( default = ' ' , required = False ) ,
region = dict ( type = ' str ' , default = ' ' ) ,
stage = dict ( default = ' ' , required = False ) ,
stage = dict ( type = ' str ' , default = ' ' ) ,
deploy = dict ( default = True , type = ' bool ' , required= Fals e) ,
deploy = dict ( type = ' bool ' , default= Tru e) ,
serverless_bin_path = dict ( required = False , type = ' path ' ) ,
serverless_bin_path = dict ( type = ' path ' ) ,
force = dict ( default = False , required = False ) ,
force = dict ( type = ' bool ' , default = False ) ,
verbose = dict ( default = False , required = False )
verbose = dict ( type = ' bool ' , default = False ) ,
) ,
) ,
)
)