| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import absolute_import, division, print_function |
| 20 |
__metaclass__ = type |
| 21 |
|
| 22 |
|
| 23 |
DOCUMENTATION = ''' |
| 24 |
--- |
| 25 |
module: sync_plan |
| 26 |
version_added: 1.0.0 |
| 27 |
short_description: Manage Sync Plans |
| 28 |
description: |
| 29 |
- Manage sync plans |
| 30 |
author: |
| 31 |
- "Andrew Kofink (@akofink)" |
| 32 |
- "Matthis Dellweg (@mdellweg) ATIX-AG" |
| 33 |
options: |
| 34 |
name: |
| 35 |
description: |
| 36 |
- Name of the sync plan |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
description: |
| 40 |
description: |
| 41 |
- Description of the sync plan |
| 42 |
type: str |
| 43 |
interval: |
| 44 |
description: |
| 45 |
- How often synchronization should run |
| 46 |
choices: |
| 47 |
- hourly |
| 48 |
- daily |
| 49 |
- weekly |
| 50 |
- custom cron |
| 51 |
required: true |
| 52 |
type: str |
| 53 |
enabled: |
| 54 |
description: |
| 55 |
- Whether the sync plan is active |
| 56 |
required: true |
| 57 |
type: bool |
| 58 |
sync_date: |
| 59 |
description: |
| 60 |
- Start date and time of the first synchronization. |
| 61 |
- Multiple formats are accepted, but only C(YYYY-mm-dd HH:MM:SS +z) (e.g. C(2024-08-01 00:00:00 +0000)) will be idempotent. |
| 62 |
required: true |
| 63 |
type: str |
| 64 |
cron_expression: |
| 65 |
description: |
| 66 |
- A cron expression as found in crontab files |
| 67 |
- This must be provided together with I(interval='custom cron'). |
| 68 |
type: str |
| 69 |
products: |
| 70 |
description: |
| 71 |
- List of products to include in the sync plan |
| 72 |
required: false |
| 73 |
type: list |
| 74 |
elements: str |
| 75 |
extends_documentation_fragment: |
| 76 |
- theforeman.foreman.foreman |
| 77 |
- theforeman.foreman.foreman.entity_state_with_defaults |
| 78 |
- theforeman.foreman.foreman.organization |
| 79 |
''' |
| 80 |
|
| 81 |
EXAMPLES = ''' |
| 82 |
- name: "Create or update weekly RHEL sync plan" |
| 83 |
theforeman.foreman.sync_plan: |
| 84 |
username: "admin" |
| 85 |
password: "changeme" |
| 86 |
server_url: "https://foreman.example.com" |
| 87 |
name: "Weekly RHEL Sync" |
| 88 |
organization: "Default Organization" |
| 89 |
interval: "weekly" |
| 90 |
enabled: false |
| 91 |
sync_date: "2017-01-01 00:00:00 UTC" |
| 92 |
products: |
| 93 |
- 'Red Hat Enterprise Linux Server' |
| 94 |
state: present |
| 95 |
''' |
| 96 |
|
| 97 |
RETURN = ''' |
| 98 |
entity: |
| 99 |
description: Final state of the affected entities grouped by their type. |
| 100 |
returned: success |
| 101 |
type: dict |
| 102 |
contains: |
| 103 |
sync_plans: |
| 104 |
description: List of sync plans. |
| 105 |
type: list |
| 106 |
elements: dict |
| 107 |
''' |
| 108 |
|
| 109 |
|
| 110 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import KatelloEntityAnsibleModule |
| 111 |
|
| 112 |
|
| 113 |
class KatelloSyncPlanModule(KatelloEntityAnsibleModule): |
| 114 |
pass |
| 115 |
|
| 116 |
|
| 117 |
def main(): |
| 118 |
module = KatelloSyncPlanModule( |
| 119 |
foreman_spec=dict( |
| 120 |
name=dict(required=True), |
| 121 |
description=dict(), |
| 122 |
interval=dict(choices=['hourly', 'daily', 'weekly', 'custom cron'], required=True), |
| 123 |
enabled=dict(type='bool', required=True), |
| 124 |
sync_date=dict(required=True), |
| 125 |
cron_expression=dict(), |
| 126 |
products=dict(type='entity_list', scope=['organization'], resolve=False), |
| 127 |
), |
| 128 |
argument_spec=dict( |
| 129 |
state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), |
| 130 |
), |
| 131 |
required_if=[ |
| 132 |
['interval', 'custom cron', ['cron_expression']], |
| 133 |
], |
| 134 |
) |
| 135 |
|
| 136 |
if (module.foreman_params['interval'] != 'custom cron') and ('cron_expression' in module.foreman_params): |
| 137 |
module.fail_json(msg='"cron_expression" cannot be combined with "interval"!="custom cron".') |
| 138 |
|
| 139 |
with module.api_connection(): |
| 140 |
entity = module.lookup_entity('entity') |
| 141 |
scope = module.scope_for('organization') |
| 142 |
|
| 143 |
handle_products = not (module.desired_absent or module.state == 'present_with_defaults') and 'products' in module.foreman_params |
| 144 |
if handle_products: |
| 145 |
module.lookup_entity('products') |
| 146 |
|
| 147 |
products = module.foreman_params.pop('products', None) |
| 148 |
sync_plan = module.run() |
| 149 |
|
| 150 |
if handle_products: |
| 151 |
desired_product_ids = set(product['id'] for product in products) |
| 152 |
current_product_ids = set(product['id'] for product in entity['products']) if entity else set() |
| 153 |
|
| 154 |
module.record_before('sync_plans/products', {'id': sync_plan['id'], 'product_ids': current_product_ids}) |
| 155 |
module.record_after('sync_plans/products', {'id': sync_plan['id'], 'product_ids': desired_product_ids}) |
| 156 |
module.record_after_full('sync_plans/products', {'id': sync_plan['id'], 'product_ids': desired_product_ids}) |
| 157 |
|
| 158 |
if desired_product_ids != current_product_ids: |
| 159 |
if not module.check_mode: |
| 160 |
product_ids_to_add = desired_product_ids - current_product_ids |
| 161 |
if product_ids_to_add: |
| 162 |
payload = { |
| 163 |
'id': sync_plan['id'], |
| 164 |
'product_ids': list(product_ids_to_add), |
| 165 |
} |
| 166 |
payload.update(scope) |
| 167 |
module.resource_action('sync_plans', 'add_products', payload) |
| 168 |
product_ids_to_remove = current_product_ids - desired_product_ids |
| 169 |
if product_ids_to_remove: |
| 170 |
payload = { |
| 171 |
'id': sync_plan['id'], |
| 172 |
'product_ids': list(product_ids_to_remove), |
| 173 |
} |
| 174 |
payload.update(scope) |
| 175 |
module.resource_action('sync_plans', 'remove_products', payload) |
| 176 |
else: |
| 177 |
module.set_changed() |
| 178 |
|
| 179 |
|
| 180 |
if __name__ == '__main__': |
| 181 |
main() |