@ -21,6 +21,7 @@ from __future__ import absolute_import, division, print_function
from ansible . module_utils . six import string_types
from ansible . module_utils . six import string_types
from ansible . module_utils . k8s . common import KubernetesAnsibleModule
from ansible . module_utils . k8s . common import KubernetesAnsibleModule
from ansible . module_utils . common . dict_transformations import dict_merge
try :
try :
@ -143,7 +144,9 @@ class KubernetesRawModule(KubernetesAnsibleModule):
return result
return result
else :
else :
if not existing :
if not existing :
if not self . check_mode :
if self . check_mode :
k8s_obj = definition
else :
try :
try :
k8s_obj = resource . create ( definition , namespace = namespace )
k8s_obj = resource . create ( definition , namespace = namespace )
except ConflictError :
except ConflictError :
@ -153,7 +156,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
self . warn ( " {0} was not found, but creating it returned a 409 Conflict error. This can happen \
self . warn ( " {0} was not found, but creating it returned a 409 Conflict error. This can happen \
if the resource you are creating does not directly create a resource of the same kind . " .format(name))
if the resource you are creating does not directly create a resource of the same kind . " .format(name))
return result
return result
result [ ' result ' ] = k8s_obj . to_dict ( )
result [ ' result ' ] = k8s_obj . to_dict ( )
result [ ' changed ' ] = True
result [ ' changed ' ] = True
result [ ' method ' ] = ' create '
result [ ' method ' ] = ' create '
return result
return result
@ -162,28 +165,32 @@ class KubernetesRawModule(KubernetesAnsibleModule):
diffs = [ ]
diffs = [ ]
if existing and force :
if existing and force :
if not self . check_mode :
if self . check_mode :
k8s_obj = definition
else :
try :
try :
k8s_obj = resource . replace ( definition , name = name , namespace = namespace ) . to_dict ( )
k8s_obj = resource . replace ( definition , name = name , namespace = namespace ) . to_dict ( )
match , diffs = self . diff_objects ( existing . to_dict ( ) , k8s_obj )
result [ ' result ' ] = k8s_obj
except DynamicApiError as exc :
except DynamicApiError as exc :
self . fail_json ( msg = " Failed to replace object: {0} " . format ( exc . body ) ,
self . fail_json ( msg = " Failed to replace object: {0} " . format ( exc . body ) ,
error = exc . status , status = exc . status , reason = exc . reason )
error = exc . status , status = exc . status , reason = exc . reason )
match , diffs = self . diff_objects ( existing . to_dict ( ) , k8s_obj )
result [ ' result ' ] = k8s_obj
result [ ' changed ' ] = not match
result [ ' changed ' ] = not match
result [ ' method ' ] = ' replace '
result [ ' method ' ] = ' replace '
result [ ' diff ' ] = diffs
result [ ' diff ' ] = diffs
return result
return result
# Differences exist between the existing obj and requested params
# Differences exist between the existing obj and requested params
if not self . check_mode :
if self . check_mode :
k8s_obj = dict_merge ( existing . to_dict ( ) , definition )
else :
try :
try :
k8s_obj = resource . patch ( definition , name = name , namespace = namespace ) . to_dict ( )
k8s_obj = resource . patch ( definition , name = name , namespace = namespace ) . to_dict ( )
match , diffs = self . diff_objects ( existing . to_dict ( ) , k8s_obj )
result [ ' result ' ] = k8s_obj
except DynamicApiError as exc :
except DynamicApiError as exc :
self . fail_json ( msg = " Failed to patch object: {0} " . format ( exc . body ) ,
self . fail_json ( msg = " Failed to patch object: {0} " . format ( exc . body ) ,
error = exc . status , status = exc . status , reason = exc . reason )
error = exc . status , status = exc . status , reason = exc . reason )
match , diffs = self . diff_objects ( existing . to_dict ( ) , k8s_obj )
result [ ' result ' ] = k8s_obj
result [ ' changed ' ] = not match
result [ ' changed ' ] = not match
result [ ' method ' ] = ' patch '
result [ ' method ' ] = ' patch '
result [ ' diff ' ] = diffs
result [ ' diff ' ] = diffs