@ -20,7 +20,7 @@ DOCUMENTATION = """
- - -
- - -
module : asa_config
module : asa_config
version_added : " 2.2 "
version_added : " 2.2 "
author : " Peter Sprygada (@privateip) & Patrick Ogenstad (@ogenstad)"
author : " Peter Sprygada (@privateip) , Patrick Ogenstad (@ogenstad)"
short_description : Manage Cisco ASA configuration sections
short_description : Manage Cisco ASA configuration sections
description :
description :
- Cisco ASA configurations use a simple block indent file sytanx
- Cisco ASA configurations use a simple block indent file sytanx
@ -34,9 +34,11 @@ options:
- The ordered set of commands that should be configured in the
- The ordered set of commands that should be configured in the
section . The commands must be the exact same commands as found
section . The commands must be the exact same commands as found
in the device running - config . Be sure to note the configuration
in the device running - config . Be sure to note the configuration
command synta n x as some commands are automatically modified by the
command synta x as some commands are automatically modified by the
device config parser .
device config parser .
required : true
required : false
default : null
aliases : [ ' commands ' ]
parents :
parents :
description :
description :
- The ordered set of parents that uniquely identify the section
- The ordered set of parents that uniquely identify the section
@ -45,6 +47,15 @@ options:
level or global commands .
level or global commands .
required : false
required : false
default : null
default : null
src :
description :
- Specifies the source path to the file that contains the configuration
or configuration template to load . The path to the source file can
either be the full path on the Ansible control host or a relative
path from the playbook or role root directory . This argument is mutually
exclusive with I ( lines ) .
required : false
default : null
before :
before :
description :
description :
- The ordered set of commands to push on to the command stack if
- The ordered set of commands to push on to the command stack if
@ -57,7 +68,7 @@ options:
after :
after :
description :
description :
- The ordered set of commands to append to the end of the command
- The ordered set of commands to append to the end of the command
stack if a change d needs to be made . Just like with I ( before ) this
stack if a change needs to be made . Just like with I ( before ) this
allows the playbook designer to append a set of commands to be
allows the playbook designer to append a set of commands to be
executed after the command set .
executed after the command set .
required : false
required : false
@ -68,11 +79,13 @@ options:
the set of commands against the current device config . If
the set of commands against the current device config . If
match is set to I ( line ) , commands are matched line by line . If
match is set to I ( line ) , commands are matched line by line . If
match is set to I ( strict ) , command lines are matched with respect
match is set to I ( strict ) , command lines are matched with respect
to position . Finally if match is set to I ( exact ) , command lines
to position . If match is set to I ( exact ) , command lines
must be an equal match .
must be an equal match . Finally , if match is set to I ( none ) , the
module will not attempt to compare the source configuration with
the running configuration on the remote device .
required : false
required : false
default : line
default : line
choices : [ ' line ' , ' strict ' , ' exact ' ]
choices : [ ' line ' , ' strict ' , ' exact ' , ' none ' ]
replace :
replace :
description :
description :
- Instructs the module on the way to perform the configuration
- Instructs the module on the way to perform the configuration
@ -84,35 +97,84 @@ options:
required : false
required : false
default : line
default : line
choices : [ ' line ' , ' block ' ]
choices : [ ' line ' , ' block ' ]
force :
update :
description :
- The I ( update ) argument controls how the configuration statements
are processed on the remote device . Valid choices for the I ( update )
argument are I ( merge ) and I ( check ) . When the argument is set to
I ( merge ) , the configuration changes are merged with the current
device running configuration . When the argument is set to I ( check )
the configuration updates are determined but not actually configured
on the remote device .
required : false
default : merge
choices : [ ' merge ' , ' check ' ]
commit :
description :
description :
- The force argument instructs the module to not consider the
- This argument specifies the update method to use when applying the
current devices running - config . When set to true , this will
configuration changes to the remote node . If the value is set to
cause the module to push the contents of I ( src ) into the device
I ( merge ) the configuration updates are merged with the running -
without first checking if already configured .
config . If the value is set to I ( check ) , no changes are made to
the remote host .
required : false
required : false
default : false
default : merge
choices : [ ' merge ' , ' check ' ]
backup :
description :
- This argument will cause the module to create a full backup of
the current C ( running - config ) from the remote device before any
changes are made . The backup file is written to the C ( backup )
folder in the playbook root directory . If the directory does not
exist , it is created .
required : false
default : no
choices : [ ' yes ' , ' no ' ]
choices : [ ' yes ' , ' no ' ]
config :
config :
description :
description :
- The module , by default , will connect to the remote device and
- The C ( config ) argument allows the playbook desginer to supply
retrieve the current running - config to use as a base for comparing
the base configuration to be used to validate configuration
against the contents of source . There are times when it is not
changes necessary . If this argument is provided , the module
desirable to have the task get the current running - config for
will not download the running - config from the remote node .
every task in a playbook . The I ( config ) argument allows the
implementer to pass in the configuruation to use as the base
config for comparision .
required : false
required : false
default : null
default : null
default :
description :
- This argument specifies whether or not to collect all defaults
when getting the remote device running config . When enabled ,
the module will get the current config by issuing the command
C ( show running - config all ) .
required : false
default : no
choices : [ ' yes ' , ' no ' ]
save :
description :
- The C ( save ) argument instructs the module to save the running -
config to the startup - config at the conclusion of the module
running . If check mode is specified , this argument is ignored .
required : false
default : no
choices : [ ' yes ' , ' no ' ]
"""
"""
EXAMPLES = """
EXAMPLES = """
# Note: examples below use the following provider dict to handle
# transport and authentication to the node.
vars :
cli :
host : " {{ inventory_hostname }} "
username : cisco
password : cisco
authorize : yes
auth_pass : cisco
transport : cli
- asa_config :
- asa_config :
lines :
lines :
- network - object host 10.80 .30 .18
- network - object host 10.80 .30 .18
- network - object host 10.80 .30 .19
- network - object host 10.80 .30 .19
- network - object host 10.80 .30 .20
- network - object host 10.80 .30 .20
parents : [ ' object-group network OG-MONITORED-SERVERS ' ]
parents : [ ' object-group network OG-MONITORED-SERVERS ' ]
provider : " {{ cli }} "
- asa_config :
- asa_config :
host : " {{ inventory_hostname }} "
host : " {{ inventory_hostname }} "
@ -128,12 +190,11 @@ EXAMPLES = """
context : ansible
context : ansible
- asa_config :
- asa_config :
provider : " {{ cli }} "
host : " {{ inventory_hostname }} "
show_command : ' more system:running-config '
show_command : ' more system:running-config '
lines :
lines :
- ikev1 pre - shared - key MyS3cretVPNK3y
- ikev1 pre - shared - key MyS3cretVPNK3y
parents : tunnel - group 1.1 .1 .1 ipsec - attributes
parents : tunnel - group 1.1 .1 .1 ipsec - attributes
provider : " {{ cli }} "
"""
"""
@ -143,79 +204,140 @@ updates:
returned : always
returned : always
type : list
type : list
sample : [ ' ... ' , ' ... ' ]
sample : [ ' ... ' , ' ... ' ]
backup_path :
description : The full path to the backup file
returned : when backup is yes
type : path
sample : / playbooks / ansible / backup / config .2016 - 07 - 16 @ 22 : 28 : 34
responses :
responses :
description : The set of responses from issuing the commands on the device
description : The set of responses from issuing the commands on the device
retured : when not check_mode
retur n ed: when not check_mode
type : list
type : list
sample : [ ' ... ' , ' ... ' ]
sample : [ ' ... ' , ' ... ' ]
"""
"""
import re
from ansible . module_utils . basic import get_exception
from ansible . module_utils . asa import NetworkModule , NetworkError
from ansible . module_utils . netcfg import NetworkConfig , dumps
from ansible . module_utils . netcli import Command
def invoke ( name , * args , * * kwargs ) :
func = globals ( ) . get ( name )
if func :
return func ( * args , * * kwargs )
def get_config ( module ) :
def check_args ( module , warnings ) :
config = module . params [ ' config ' ] or dict ( )
if module . params [ ' parents ' ] :
if not config and not module . params [ ' force ' ] :
if not module . params [ ' lines ' ] or module . params [ ' src ' ] :
config = module . config
warnings . append ( ' ignoring unnecessary argument parents ' )
return config
if module . params [ ' match ' ] == ' none ' and module . params [ ' replace ' ] :
warnings . append ( ' ignoring unnecessary argument replace ' )
def get_config ( module , result ) :
defaults = module . params [ ' default ' ]
if defaults is True :
key = ' __configall__ '
else :
key = ' __config__ '
contents = module . params [ ' config ' ] or result . get ( key )
if not contents :
contents = module . config . get_config ( include_defaults = defaults )
result [ key ] = contents
return NetworkConfig ( indent = 1 , contents = contents )
def get_candidate ( module ) :
candidate = NetworkConfig ( indent = 1 )
if module . params [ ' src ' ] :
candidate . load ( module . params [ ' src ' ] )
elif module . params [ ' lines ' ] :
parents = module . params [ ' parents ' ] or list ( )
candidate . add ( module . params [ ' lines ' ] , parents = parents )
return candidate
def load_config ( module , commands , result ) :
if not module . check_mode and module . params [ ' update ' ] != ' check ' :
module . config ( commands )
result [ ' changed ' ] = module . params [ ' update ' ] != ' check '
result [ ' updates ' ] = commands . split ( ' \n ' )
def run ( module , result ) :
match = module . params [ ' match ' ]
replace = module . params [ ' replace ' ]
candidate = get_candidate ( module )
if match != ' none ' :
config = get_config ( module , result )
configobjs = candidate . difference ( config , match = match , replace = replace )
else :
config = None
configobjs = candidate . items
if configobjs :
commands = dumps ( configobjs , ' commands ' )
if module . params [ ' before ' ] :
commands [ : 0 ] = module . params [ ' before ' ]
if module . params [ ' after ' ] :
commands . extend ( module . params [ ' after ' ] )
# send the configuration commands to the device and merge
# them with the current running config
load_config ( module , commands , result )
if module . params [ ' save ' ] and not module . check_mode :
module . config . save_config ( )
def main ( ) :
def main ( ) :
argument_spec = dict (
argument_spec = dict (
lines = dict ( aliases = [ ' commands ' ] , required = True , type = ' list ' ) ,
lines = dict ( aliases = [ ' commands ' ] , type = ' list ' ) ,
parents = dict ( type = ' list ' ) ,
parents = dict ( type = ' list ' ) ,
src = dict ( type = ' path ' ) ,
before = dict ( type = ' list ' ) ,
before = dict ( type = ' list ' ) ,
after = dict ( type = ' list ' ) ,
after = dict ( type = ' list ' ) ,
match = dict ( default = ' line ' , choices = [ ' line ' , ' strict ' , ' exact ' ] ) ,
replace = dict ( default = ' line ' , choices = [ ' line ' , ' block ' ] ) ,
force = dict ( default = False , type = ' bool ' ) ,
config = dict ( )
)
module = get_module ( argument_spec = argument_spec ,
match = dict ( default = ' line ' , choices = [ ' line ' , ' strict ' , ' exact ' , ' none ' ] ) ,
supports_check_mode = True )
replace = dict ( default = ' line ' , choices = [ ' line ' , ' block ' ] ) ,
lines = module . params [ ' lines ' ]
parents = module . params [ ' parents ' ] or list ( )
before = module . params [ ' before ' ]
update = dict ( choices = [ ' merge ' , ' check ' ] , default = ' merge ' ) ,
after = module . params [ ' after ' ]
backup = dict ( type = ' bool ' , default = False ) ,
match = module . params [ ' match ' ]
config = dict ( ) ,
replace = module . params [ ' replace ' ]
default = dict ( type = ' bool ' , default = False ) ,
if not module . params [ ' force ' ] :
save = dict ( type = ' bool ' , default = False ) ,
contents = get_config ( module )
)
config = NetworkConfig ( contents = contents , indent = 1 )
candidate = NetworkConfig ( indent = 1 )
mutually_exclusive = [ ( ' lines ' , ' src ' ) ]
candidate . add ( lines , parents = parents )
commands = candidate . difference ( config , path = parents , match = match , replace = replace )
module = NetworkModule ( argument_spec = argument_spec ,
else :
connect_on_load = False ,
commands = parents
mutually_exclusive = mutually_exclusive ,
commands . extend ( lines )
supports_check_mode = True )
result = dict ( changed = False )
warnings = list ( )
check_args ( module , warnings )
if commands :
result = dict ( changed = False , warnings = warnings )
if before :
commands [ : 0 ] = before
if after :
if module . params [ ' backup ' ] :
commands . extend ( after )
result [ ' __backup__ ' ] = module . config . get_config ( )
if not module . check_mode :
try :
commands = [ str ( c ) . strip ( ) for c in commands ]
run ( module , result )
response = module . configure ( commands )
except NetworkError :
result [ ' responses ' ] = response
exc = get_exception ( )
result [ ' changed ' ] = True
module. fail_json ( msg = str ( exc ) )
result [ ' updates ' ] = commands
module . exit_json ( * * result )
module . exit_json ( * * result )
from ansible . module_utils . basic import *
from ansible . module_utils . shell import *
from ansible . module_utils . netcfg import *
from ansible . module_utils . asa import *
if __name__ == ' __main__ ' :
if __name__ == ' __main__ ' :
main ( )
main ( )