| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: lightsail_snapshot |
| 10 |
version_added: "6.0.0" |
| 11 |
short_description: Creates snapshots of AWS Lightsail instances |
| 12 |
description: |
| 13 |
- Creates snapshots of AWS Lightsail instances. |
| 14 |
author: |
| 15 |
- "Nuno Saavedra (@Nfsaavedra)" |
| 16 |
options: |
| 17 |
state: |
| 18 |
description: |
| 19 |
- Indicate desired state of the target. |
| 20 |
default: present |
| 21 |
choices: ['present', 'absent'] |
| 22 |
type: str |
| 23 |
snapshot_name: |
| 24 |
description: Name of the new instance snapshot. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
instance_name: |
| 28 |
description: |
| 29 |
- Name of the instance to create the snapshot. |
| 30 |
- Required when I(state=present). |
| 31 |
type: str |
| 32 |
wait: |
| 33 |
description: |
| 34 |
- Wait for the instance snapshot to be created before returning. |
| 35 |
type: bool |
| 36 |
default: true |
| 37 |
wait_timeout: |
| 38 |
description: |
| 39 |
- How long before I(wait) gives up, in seconds. |
| 40 |
default: 300 |
| 41 |
type: int |
| 42 |
|
| 43 |
extends_documentation_fragment: |
| 44 |
- amazon.aws.common.modules |
| 45 |
- amazon.aws.region.modules |
| 46 |
- amazon.aws.boto3 |
| 47 |
""" |
| 48 |
|
| 49 |
EXAMPLES = r""" |
| 50 |
- name: Create AWS Lightsail snapshot |
| 51 |
lightsail_snapshot: |
| 52 |
region: us-east-1 |
| 53 |
snapshot_name: "my_instance_snapshot" |
| 54 |
instance_name: "my_instance" |
| 55 |
|
| 56 |
- name: Delete AWS Lightsail snapshot |
| 57 |
lightsail_snapshot: |
| 58 |
region: us-east-1 |
| 59 |
snapshot_name: "my_instance_snapshot" |
| 60 |
state: absent |
| 61 |
""" |
| 62 |
|
| 63 |
RETURN = r""" |
| 64 |
changed: |
| 65 |
description: if a snapshot has been modified/created |
| 66 |
returned: always |
| 67 |
type: bool |
| 68 |
sample: |
| 69 |
changed: true |
| 70 |
snapshot: |
| 71 |
description: instance snapshot data |
| 72 |
type: dict |
| 73 |
returned: always |
| 74 |
sample: |
| 75 |
arn: "arn:aws:lightsail:us-east-1:070807442430:InstanceSnapshot/54b0f785-7132-443d-9e32-95a6825636a4" |
| 76 |
created_at: "2023-02-23T18:46:11.183000+00:00" |
| 77 |
from_attached_disks: [] |
| 78 |
from_blueprint_id: "amazon_linux_2" |
| 79 |
from_bundle_id: "nano_2_0" |
| 80 |
from_instance_arn: "arn:aws:lightsail:us-east-1:070807442430:Instance/5ca1e7ca-a994-4e19-bb82-deb9d79e9ca3" |
| 81 |
from_instance_name: "my_instance" |
| 82 |
is_from_auto_snapshot: false |
| 83 |
location: |
| 84 |
availability_zone: "all" |
| 85 |
region_name: "us-east-1" |
| 86 |
name: "my_instance_snapshot" |
| 87 |
resource_type: "InstanceSnapshot" |
| 88 |
size_in_gb: 20 |
| 89 |
state: "available" |
| 90 |
support_code: "351201681302/ami-06b48e5589f1e248b" |
| 91 |
tags: [] |
| 92 |
""" |
| 93 |
|
| 94 |
import time |
| 95 |
|
| 96 |
try: |
| 97 |
import botocore |
| 98 |
except ImportError: |
| 99 |
# will be caught by AnsibleAWSModule |
| 100 |
pass |
| 101 |
|
| 102 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 103 |
|
| 104 |
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code |
| 105 |
|
| 106 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 107 |
|
| 108 |
|
| 109 |
def find_instance_snapshot_info(module, client, instance_snapshot_name, fail_if_not_found=False): |
| 110 |
try: |
| 111 |
res = client.get_instance_snapshot(instanceSnapshotName=instance_snapshot_name) |
| 112 |
except is_boto3_error_code("NotFoundException") as e: |
| 113 |
if fail_if_not_found: |
| 114 |
module.fail_json_aws(e) |
| 115 |
return None |
| 116 |
except botocore.exceptions.ClientError as e: # pylint: disable=duplicate-except |
| 117 |
module.fail_json_aws(e) |
| 118 |
return res["instanceSnapshot"] |
| 119 |
|
| 120 |
|
| 121 |
def wait_for_instance_snapshot(module, client, instance_snapshot_name): |
| 122 |
wait_timeout = module.params.get("wait_timeout") |
| 123 |
wait_max = time.time() + wait_timeout |
| 124 |
snapshot = find_instance_snapshot_info(module, client, instance_snapshot_name) |
| 125 |
|
| 126 |
while wait_max > time.time(): |
| 127 |
snapshot = find_instance_snapshot_info(module, client, instance_snapshot_name) |
| 128 |
current_state = snapshot["state"] |
| 129 |
if current_state != "pending": |
| 130 |
break |
| 131 |
time.sleep(5) |
| 132 |
else: |
| 133 |
module.fail_json(msg=f'Timed out waiting for instance snapshot "{instance_snapshot_name}" to be created.') |
| 134 |
|
| 135 |
return snapshot |
| 136 |
|
| 137 |
|
| 138 |
def create_snapshot(module, client): |
| 139 |
snapshot = find_instance_snapshot_info(module, client, module.params.get("snapshot_name")) |
| 140 |
new_instance = snapshot is None |
| 141 |
|
| 142 |
if module.check_mode or not new_instance: |
| 143 |
snapshot = snapshot if snapshot is not None else {} |
| 144 |
module.exit_json( |
| 145 |
changed=new_instance, |
| 146 |
instance_snapshot=camel_dict_to_snake_dict(snapshot), |
| 147 |
) |
| 148 |
|
| 149 |
try: |
| 150 |
snapshot = client.create_instance_snapshot( |
| 151 |
instanceSnapshotName=module.params.get("snapshot_name"), |
| 152 |
instanceName=module.params.get("instance_name"), |
| 153 |
) |
| 154 |
except botocore.exceptions.ClientError as e: |
| 155 |
module.fail_json_aws(e) |
| 156 |
|
| 157 |
if module.params.get("wait"): |
| 158 |
snapshot = wait_for_instance_snapshot(module, client, module.params.get("snapshot_name")) |
| 159 |
|
| 160 |
module.exit_json( |
| 161 |
changed=new_instance, |
| 162 |
instance_snapshot=camel_dict_to_snake_dict(snapshot), |
| 163 |
) |
| 164 |
|
| 165 |
|
| 166 |
def delete_snapshot(module, client): |
| 167 |
snapshot = find_instance_snapshot_info(module, client, module.params.get("snapshot_name")) |
| 168 |
if module.check_mode or snapshot is None: |
| 169 |
changed = snapshot is not None |
| 170 |
instance = snapshot if changed else {} |
| 171 |
module.exit_json(changed=changed, instance=instance) |
| 172 |
|
| 173 |
try: |
| 174 |
client.delete_instance_snapshot(instanceSnapshotName=module.params.get("snapshot_name")) |
| 175 |
except botocore.exceptions.ClientError as e: |
| 176 |
module.fail_json_aws(e) |
| 177 |
|
| 178 |
module.exit_json(changed=True, instance=camel_dict_to_snake_dict(snapshot)) |
| 179 |
|
| 180 |
|
| 181 |
def main(): |
| 182 |
argument_spec = dict( |
| 183 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 184 |
snapshot_name=dict(type="str", required=True), |
| 185 |
instance_name=dict(type="str"), |
| 186 |
wait=dict(type="bool", default=True), |
| 187 |
wait_timeout=dict(default=300, type="int"), |
| 188 |
) |
| 189 |
required_if = [ |
| 190 |
["state", "present", ("instance_name",)], |
| 191 |
] |
| 192 |
|
| 193 |
module = AnsibleAWSModule(argument_spec=argument_spec, required_if=required_if, supports_check_mode=True) |
| 194 |
client = module.client("lightsail") |
| 195 |
|
| 196 |
state = module.params.get("state") |
| 197 |
|
| 198 |
if state == "present": |
| 199 |
create_snapshot(module, client) |
| 200 |
elif state == "absent": |
| 201 |
delete_snapshot(module, client) |
| 202 |
|
| 203 |
|
| 204 |
if __name__ == "__main__": |
| 205 |
main() |