@ -1,98 +1,94 @@
#!/usr/bin/python
#!/usr/bin/python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
# (c) 2017, Simon Dodsley (simon@purestorage.com)
# (c) 2017, Simon Dodsley (simon@purestorage.com)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
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 : purefa_snap
module : purefa_snap
version_added : 2.4
version_added : ' 2.4 '
short_description : Create or Delet e volume snapshots on Pure Storage FlashArray
short_description : Manag e volume snapshots on Pure Storage FlashArray s
description :
description :
- This module creates or deletes volume snapshots and creates volumes from snapshots on Pure Storage FlashArray .
- Create or delete volumes and volume snapshots on Pure Storage FlashArray .
author : Simon Dodsley ( @sdodsley )
author :
- Simon Dodsley ( @sdodsley )
options :
options :
name :
name :
description :
description :
- Source volume name of snapshot
- The name of the source volume .
required : true
required : true
suffix :
suffix :
description :
description :
- Suffix of snapshot name
- Suffix of snapshot name .
required : false
target :
target :
description :
description :
- Name of target volume if creating from snapshot
- Name of target volume if creating from snapshot .
required : false
overwrite :
overwrite :
description :
description :
- Define whether to overwrite existing volume when creating from snapshot
- Define whether to overwrite existing volume when creating from snapshot .
required : false
default : false
type : bool
type : bool
default : ' no '
state :
state :
description :
description :
- Create or delete volume snapshot
- Define whether the volume snapshot should exist or not .
required: false
choices: [ absent , copy , present ]
default : present
default : present
choices : [ " present " , " absent " , " copy " ]
eradicate :
eradicate :
description :
description :
- Define whether to eradicate the snapshot on delete or leave in trash
- Define whether to eradicate the snapshot on delete or leave in trash .
required : false
default : false
type : bool
type : bool
default : ' no '
extends_documentation_fragment :
extends_documentation_fragment :
- purestorage
- purestorage
'''
'''
EXAMPLES = '''
EXAMPLES = r '''
- name : Create snapshot foo . ansible
- name : Create snapshot foo . ansible
purefa_snap :
purefa_snap :
name : foo
name : foo
suffix : ansible
suffix : ansible
state : present
fa_url : 10.10 .10 .2
fa_url : 10.10 .10 .2
fa_api_token : e31060a7 - 21 fc - e277 - 6240 - 25983 c6c4592
fa_api_token : e31060a7 - 21 fc - e277 - 6240 - 25983 c6c4592
state : present
- name : Create R / W clone foo_clone from snapshot foo . snap
- name : Create R / W clone foo_clone from snapshot foo . snap
purefa_snap :
purefa_snap :
name : foo
name : foo
suffix : snap
suffix : snap
target : foo_clone
target : foo_clone
state : copy
fa_url : 10.10 .10 .2
fa_url : 10.10 .10 .2
fa_api_token : e31060a7 - 21 fc - e277 - 6240 - 25983 c6c4592
fa_api_token : e31060a7 - 21 fc - e277 - 6240 - 25983 c6c4592
state : copy
- name : Overwrite existing volume foo_clone with snapshot foo . snap
- name : Overwrite existing volume foo_clone with snapshot foo . snap
purefa_snap :
purefa_snap :
name : foo
name : foo
suffix : snap
suffix : snap
target : foo_clone
target : foo_clone
state : copy
overwrite : true
overwrite : true
fa_url : 10.10 .10 .2
fa_url : 10.10 .10 .2
fa_api_token : e31060a7 - 21 fc - e277 - 6240 - 25983 c6c4592
fa_api_token : e31060a7 - 21 fc - e277 - 6240 - 25983 c6c4592
state : copy
- name : Delete and eradicate snapshot named foo . snap
- name : Delete and eradicate snapshot named foo . snap
purefa_snap :
purefa_snap :
name : foo
name : foo
suffix : snap
suffix : snap
eradicate : true
eradicate : true
state : absent
fa_url : 10.10 .10 .2
fa_url : 10.10 .10 .2
fa_api_token : e31060a7 - 21 fc - e277 - 6240 - 25983 c6c4592 '''
fa_api_token : e31060a7 - 21 fc - e277 - 6240 - 25983 c6c4592
state : absent
'''
RETURN = '''
RETURN = r '''
'''
'''
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . basic import AnsibleModule
@ -100,9 +96,9 @@ from ansible.module_utils.pure import get_system, purefa_argument_spec
from datetime import datetime
from datetime import datetime
HAS_PURESTORAGE = True
try :
try :
from purestorage import purestorage
from purestorage import purestorage
HAS_PURESTORAGE = True
except ImportError :
except ImportError :
HAS_PURESTORAGE = False
HAS_PURESTORAGE = False
@ -144,15 +140,16 @@ def create_snapshot(module, array):
def create_from_snapshot ( module , array ) :
def create_from_snapshot ( module , array ) :
""" Create Volume from Snapshot """
""" Create Volume from Snapshot """
if not module . check_mode :
source = module . params [ ' name ' ] + " . " + module . params [ ' suffix ' ]
source = module . params [ ' name ' ] + " . " + module . params [ ' suffix ' ]
tgt = get_target ( module , array )
tgt = get_target ( module , array )
if tgt is None :
if tgt is None :
changed = True
changed = True
if not module . check_mode :
array . copy_volume ( source ,
array . copy_volume ( source ,
module . params [ ' target ' ] )
module . params [ ' target ' ] )
elif tgt is not None and module . params [ ' overwrite ' ] :
elif tgt is not None and module . params [ ' overwrite ' ] :
changed = True
changed = True
if not module . check_mode :
array . copy_volume ( source ,
array . copy_volume ( source ,
module . params [ ' target ' ] ,
module . params [ ' target ' ] ,
overwrite = module . params [ ' overwrite ' ] )
overwrite = module . params [ ' overwrite ' ] )
@ -179,16 +176,14 @@ def delete_snapshot(module, array):
def main ( ) :
def main ( ) :
argument_spec = purefa_argument_spec ( )
argument_spec = purefa_argument_spec ( )
argument_spec . update (
argument_spec . update ( dict (
dict (
name = dict ( type = ' str ' , required = True ) ,
name = dict ( required = True ) ,
suffix = dict ( type = ' str ' ) ,
suffix = dict ( ) ,
target = dict ( type = ' str ' ) ,
target = dict ( ) ,
overwrite = dict ( type = ' bool ' , default = False ) ,
overwrite = dict ( default = ' false ' , type = ' bool ' ) ,
eradicate = dict ( type = ' bool ' , default = False ) ,
eradicate = dict ( default = ' false ' , type = ' bool ' ) ,
state = dict ( type = ' str ' , default = ' present ' , choices = [ ' absent ' , ' copy ' , ' present ' ] ) ,
state = dict ( default = ' present ' , choices = [ ' present ' , ' absent ' , ' copy ' ] ) ,
) )
)
)
required_if = [ ( ' state ' , ' copy ' , [ ' target ' , ' suffix ' ] ) ]
required_if = [ ( ' state ' , ' copy ' , [ ' target ' , ' suffix ' ] ) ]
@ -202,6 +197,7 @@ def main():
if module . params [ ' suffix ' ] is None :
if module . params [ ' suffix ' ] is None :
suffix = " snap- " + str ( ( datetime . utcnow ( ) - datetime ( 1970 , 1 , 1 , 0 , 0 , 0 , 0 ) ) . total_seconds ( ) )
suffix = " snap- " + str ( ( datetime . utcnow ( ) - datetime ( 1970 , 1 , 1 , 0 , 0 , 0 , 0 ) ) . total_seconds ( ) )
module . params [ ' suffix ' ] = suffix . replace ( " . " , " " )
module . params [ ' suffix ' ] = suffix . replace ( " . " , " " )
state = module . params [ ' state ' ]
state = module . params [ ' state ' ]
array = get_system ( module )
array = get_system ( module )
volume = get_volume ( module , array )
volume = get_volume ( module , array )