| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
__metaclass__ = type |
| 20 |
|
| 21 |
|
| 22 |
DOCUMENTATION = ''' |
| 23 |
--- |
| 24 |
module: snapshot |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Snapshots |
| 27 |
description: |
| 28 |
- "Manage Snapshots for Host Entities" |
| 29 |
- "This module can create, update, revert and delete snapshots" |
| 30 |
- "This module requires the foreman_snapshot_management plugin set up in the server" |
| 31 |
- "See: U(https://github.com/ATIX-AG/foreman_snapshot_management)" |
| 32 |
author: |
| 33 |
- "Manisha Singhal (@Manisha15) ATIX AG" |
| 34 |
options: |
| 35 |
name: |
| 36 |
description: |
| 37 |
- Name of Snapshot |
| 38 |
required: true |
| 39 |
type: str |
| 40 |
description: |
| 41 |
description: |
| 42 |
- Description of Snapshot |
| 43 |
required: false |
| 44 |
type: str |
| 45 |
host: |
| 46 |
description: |
| 47 |
- Name of related Host |
| 48 |
required: true |
| 49 |
type: str |
| 50 |
include_ram: |
| 51 |
description: |
| 52 |
- Option to add RAM (only available for VMWare compute-resource) |
| 53 |
required: false |
| 54 |
type: bool |
| 55 |
quiesce: |
| 56 |
description: |
| 57 |
- Option to create quiesce snapshot (only available for VMWare compute-resource) |
| 58 |
required: false |
| 59 |
type: bool |
| 60 |
state: |
| 61 |
description: |
| 62 |
- State of Snapshot |
| 63 |
default: present |
| 64 |
choices: ["present", "reverted", "absent", "new_snapshot"] |
| 65 |
type: str |
| 66 |
id: |
| 67 |
description: |
| 68 |
- Id of Snapshot |
| 69 |
required: false |
| 70 |
type: str |
| 71 |
extends_documentation_fragment: |
| 72 |
- theforeman.foreman.foreman |
| 73 |
''' |
| 74 |
|
| 75 |
EXAMPLES = ''' |
| 76 |
- name: "Create a Snapshot" |
| 77 |
theforeman.foreman.snapshot: |
| 78 |
username: "admin" |
| 79 |
password: "changeme" |
| 80 |
server_url: "https://foreman.example.com" |
| 81 |
name: "snapshot_before_software_upgrade" |
| 82 |
host: "server.example.com" |
| 83 |
state: present |
| 84 |
|
| 85 |
- name: "Create Snapshots with same name" |
| 86 |
theforeman.foreman.snapshot: |
| 87 |
username: "admin" |
| 88 |
password: "changeme" |
| 89 |
server_url: "https://foreman.example.com" |
| 90 |
name: "snapshot_before_software_upgrade" |
| 91 |
host: "server.example.com" |
| 92 |
state: new_snapshot |
| 93 |
|
| 94 |
- name: "Update a Snapshot" |
| 95 |
theforeman.foreman.snapshot: |
| 96 |
username: "admin" |
| 97 |
password: "changeme" |
| 98 |
server_url: "https://foreman.example.com" |
| 99 |
name: "snapshot_before_software_upgrade" |
| 100 |
host: "server.example.com" |
| 101 |
description: "description of snapshot" |
| 102 |
state: present |
| 103 |
|
| 104 |
- name: "Update a Snapshot with same name" |
| 105 |
theforeman.foreman.snapshot: |
| 106 |
username: "admin" |
| 107 |
password: "changeme" |
| 108 |
server_url: "https://foreman.example.com" |
| 109 |
name: "snapshot_before_software_upgrade" |
| 110 |
host: "server.example.com" |
| 111 |
description: "description of snapshot" |
| 112 |
state: present |
| 113 |
id: "snapshot-id" |
| 114 |
|
| 115 |
- name: "Revert a Snapshot" |
| 116 |
theforeman.foreman.snapshot: |
| 117 |
username: "admin" |
| 118 |
password: "changeme" |
| 119 |
server_url: "https://foreman.example.com" |
| 120 |
name: "snapshot_before_software_upgrade" |
| 121 |
host: "server.example.com" |
| 122 |
state: reverted |
| 123 |
|
| 124 |
- name: "Delete a Snapshot" |
| 125 |
theforeman.foreman.snapshot: |
| 126 |
username: "admin" |
| 127 |
password: "changeme" |
| 128 |
server_url: "https://foreman.example.com" |
| 129 |
name: "snapshot_before_software_upgrade" |
| 130 |
host: "server.example.com" |
| 131 |
state: absent |
| 132 |
''' |
| 133 |
|
| 134 |
RETURN = ''' |
| 135 |
entity: |
| 136 |
description: Final state of the affected entities grouped by their type. |
| 137 |
returned: success |
| 138 |
type: dict |
| 139 |
contains: |
| 140 |
snapshots: |
| 141 |
description: List of snapshots. |
| 142 |
type: list |
| 143 |
elements: dict |
| 144 |
''' |
| 145 |
|
| 146 |
|
| 147 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanEntityAnsibleModule |
| 148 |
|
| 149 |
|
| 150 |
class ForemanSnapshotModule(ForemanEntityAnsibleModule): |
| 151 |
pass |
| 152 |
|
| 153 |
|
| 154 |
def main(): |
| 155 |
module = ForemanSnapshotModule( |
| 156 |
argument_spec=dict( |
| 157 |
state=dict(default='present', choices=['present', 'absent', 'reverted', 'new_snapshot']), |
| 158 |
), |
| 159 |
foreman_spec=dict( |
| 160 |
host=dict(type='entity', required=True, ensure=False), |
| 161 |
name=dict(required=True), |
| 162 |
description=dict(), |
| 163 |
include_ram=dict(type='bool'), |
| 164 |
id=dict(), |
| 165 |
quiesce=dict(type='bool'), |
| 166 |
), |
| 167 |
required_plugins=[('snapshot_management', ['*'])], |
| 168 |
entity_opts={'scope': ['host']}, |
| 169 |
) |
| 170 |
|
| 171 |
with module.api_connection(): |
| 172 |
host_val = module.lookup_entity('host') |
| 173 |
params = {'host_id': host_val['id']} |
| 174 |
if module.state == 'new_snapshot': |
| 175 |
module.ensure_entity('snapshots', module.foreman_params, None, params=params) |
| 176 |
elif module.state != 'new_snapshot' and module.foreman_params.get('id'): |
| 177 |
snapshot = module.resource_action('snapshots', 'show', params={'id': module.params['id'], 'host_id': host_val['id']}) |
| 178 |
module.ensure_entity('snapshots', module.foreman_params, snapshot, params=params, state=module.state) |
| 179 |
else: |
| 180 |
module.run() |
| 181 |
|
| 182 |
|
| 183 |
if __name__ == '__main__': |
| 184 |
main() |