| 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: image |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Images |
| 27 |
description: |
| 28 |
- Create, update, and delete Images |
| 29 |
author: |
| 30 |
- "Mark Hlawatschek (@hlawatschek) ATIX AG" |
| 31 |
options: |
| 32 |
name: |
| 33 |
description: Image name |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
compute_resource: |
| 37 |
description: Compute resource the image is assigned to |
| 38 |
required: true |
| 39 |
type: str |
| 40 |
uuid: |
| 41 |
aliases: |
| 42 |
- image_uuid |
| 43 |
description: UUID or Marketplace URN of the operatingsystem image |
| 44 |
required: true |
| 45 |
type: str |
| 46 |
image_username: |
| 47 |
description: Username that is used to login into the operating system |
| 48 |
required: true |
| 49 |
type: str |
| 50 |
image_password: |
| 51 |
description: Password that is used to login into the operating system |
| 52 |
required: false |
| 53 |
type: str |
| 54 |
operatingsystem: |
| 55 |
required: true |
| 56 |
architecture: |
| 57 |
description: architecture of the image |
| 58 |
required: true |
| 59 |
type: str |
| 60 |
user_data: |
| 61 |
description: Image supports user_data |
| 62 |
required: false |
| 63 |
type: bool |
| 64 |
extends_documentation_fragment: |
| 65 |
- theforeman.foreman.foreman |
| 66 |
- theforeman.foreman.foreman.entity_state |
| 67 |
- theforeman.foreman.foreman.operatingsystem |
| 68 |
''' |
| 69 |
|
| 70 |
EXAMPLES = ''' |
| 71 |
- name: create Image for EC2 |
| 72 |
theforeman.foreman.image: |
| 73 |
name: CentOS |
| 74 |
image_uuid: "ami-0ff760d16d9497662" |
| 75 |
image_username: "centos" |
| 76 |
operatingsystem: "CentOS 7" |
| 77 |
compute_resource: "AWS" |
| 78 |
architecture: "x86_64" |
| 79 |
''' |
| 80 |
|
| 81 |
RETURN = ''' |
| 82 |
entity: |
| 83 |
description: Final state of the affected entities grouped by their type. |
| 84 |
returned: success |
| 85 |
type: dict |
| 86 |
contains: |
| 87 |
images: |
| 88 |
description: List of images. |
| 89 |
type: list |
| 90 |
elements: dict |
| 91 |
''' |
| 92 |
|
| 93 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanEntityAnsibleModule |
| 94 |
|
| 95 |
|
| 96 |
class ForemanImageModule(ForemanEntityAnsibleModule): |
| 97 |
pass |
| 98 |
|
| 99 |
|
| 100 |
def main(): |
| 101 |
module = ForemanImageModule( |
| 102 |
argument_spec=dict( |
| 103 |
image_username=dict(required=True), |
| 104 |
image_password=dict(no_log=True), |
| 105 |
), |
| 106 |
foreman_spec=dict( |
| 107 |
name=dict(required=True), |
| 108 |
username=dict(invisible=True), |
| 109 |
uuid=dict(required=True, aliases=['image_uuid']), |
| 110 |
password=dict(invisible=True, no_log=True), |
| 111 |
compute_resource=dict(type='entity', required=True), |
| 112 |
architecture=dict(type='entity', required=True), |
| 113 |
operatingsystem=dict(type='entity', required=True), |
| 114 |
user_data=dict(type='bool') |
| 115 |
), |
| 116 |
entity_opts={'scope': ['compute_resource']}, |
| 117 |
) |
| 118 |
|
| 119 |
module.foreman_params['username'] = module.foreman_params.pop('image_username') |
| 120 |
if 'image_password' in module.foreman_params: |
| 121 |
module.foreman_params['password'] = module.foreman_params.pop('image_password') |
| 122 |
with module.api_connection(): |
| 123 |
scope = module.scope_for('compute_resource') |
| 124 |
operatingsystem_id = module.lookup_entity('operatingsystem')['id'] |
| 125 |
module.set_entity('entity', module.find_resource( |
| 126 |
'images', |
| 127 |
search='name="{0}",operatingsystem="{1}"'.format(module.foreman_params['name'], operatingsystem_id), |
| 128 |
params=scope, |
| 129 |
failsafe=True, |
| 130 |
)) |
| 131 |
module.run() |
| 132 |
|
| 133 |
|
| 134 |
if __name__ == '__main__': |
| 135 |
main() |