| 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 |
DOCUMENTATION = ''' |
| 22 |
--- |
| 23 |
module: job_invocation |
| 24 |
short_description: Invoke Remote Execution Jobs |
| 25 |
version_added: 1.4.0 |
| 26 |
description: |
| 27 |
- "Invoke and schedule Remote Execution Jobs" |
| 28 |
author: |
| 29 |
- "Peter Ondrejka (@pondrejk)" |
| 30 |
options: |
| 31 |
search_query: |
| 32 |
description: |
| 33 |
- Search query to identify hosts |
| 34 |
type: str |
| 35 |
bookmark: |
| 36 |
description: |
| 37 |
- Bookmark to infer the search query from |
| 38 |
type: str |
| 39 |
job_template: |
| 40 |
description: |
| 41 |
- Job template to execute |
| 42 |
type: str |
| 43 |
targeting_type: |
| 44 |
description: |
| 45 |
- Dynamic query updates the search results before execution (useful for scheduled jobs) |
| 46 |
choices: |
| 47 |
- static_query |
| 48 |
- dynamic_query |
| 49 |
default: static_query |
| 50 |
type: str |
| 51 |
randomized_ordering: |
| 52 |
description: |
| 53 |
- Whether to order the selected hosts randomly |
| 54 |
type: bool |
| 55 |
execution_timeout_interval: |
| 56 |
description: |
| 57 |
- Override the timeout interval from the template for this invocation only |
| 58 |
type: int |
| 59 |
ssh: |
| 60 |
description: |
| 61 |
- ssh related options |
| 62 |
type: dict |
| 63 |
suboptions: |
| 64 |
effective_user: |
| 65 |
description: |
| 66 |
- What user should be used to run the script (using sudo-like mechanisms) |
| 67 |
- Defaults to a template parameter or global setting |
| 68 |
type: str |
| 69 |
command: |
| 70 |
description: |
| 71 |
- Command to be executed on host. Required for command templates |
| 72 |
type: str |
| 73 |
inputs: |
| 74 |
description: |
| 75 |
- Inputs to use |
| 76 |
type: dict |
| 77 |
recurrence: |
| 78 |
description: |
| 79 |
- Schedule a recurring job |
| 80 |
type: dict |
| 81 |
suboptions: |
| 82 |
cron_line: |
| 83 |
description: |
| 84 |
- How often the job should occur, in the cron format |
| 85 |
type: str |
| 86 |
max_iteration: |
| 87 |
description: |
| 88 |
- Repeat a maximum of N times |
| 89 |
type: int |
| 90 |
end_time: |
| 91 |
description: |
| 92 |
- Perform no more executions after this time |
| 93 |
type: str |
| 94 |
purpose: |
| 95 |
description: |
| 96 |
- Designation of a special purpose |
| 97 |
type: str |
| 98 |
scheduling: |
| 99 |
description: |
| 100 |
- Schedule the job to start at a later time |
| 101 |
type: dict |
| 102 |
suboptions: |
| 103 |
start_at: |
| 104 |
description: |
| 105 |
- Schedule the job for a future time |
| 106 |
type: str |
| 107 |
start_before: |
| 108 |
description: |
| 109 |
- Indicates that the action should be cancelled if it cannot be started before this time. |
| 110 |
type: str |
| 111 |
concurrency_control: |
| 112 |
description: |
| 113 |
- Control concurrency level and distribution over time |
| 114 |
type: dict |
| 115 |
suboptions: |
| 116 |
time_span: |
| 117 |
description: |
| 118 |
- Distribute tasks over given number of seconds |
| 119 |
- This is removed since foreman_remote_execution-11.0.0 |
| 120 |
type: int |
| 121 |
concurrency_level: |
| 122 |
description: |
| 123 |
- Maximum jobs to be executed at once |
| 124 |
type: int |
| 125 |
description_format: |
| 126 |
description: |
| 127 |
- Override the description format from the template for this invocation only |
| 128 |
type: str |
| 129 |
feature: |
| 130 |
description: |
| 131 |
- Feature label that should be triggered |
| 132 |
- A job template assigned to this feature will be used |
| 133 |
type: str |
| 134 |
version_added: 5.8.0 |
| 135 |
extends_documentation_fragment: |
| 136 |
- theforeman.foreman.foreman |
| 137 |
''' |
| 138 |
|
| 139 |
EXAMPLES = ''' |
| 140 |
|
| 141 |
- name: "Run remote command on a single host once" |
| 142 |
theforeman.foreman.job_invocation: |
| 143 |
search_query: "name ^ (foreman.example.com)" |
| 144 |
command: 'ls' |
| 145 |
job_template: "Run Command - SSH Default" |
| 146 |
ssh: |
| 147 |
effective_user: "tester" |
| 148 |
|
| 149 |
- name: "Run ansible command on active hosts once a day" |
| 150 |
theforeman.foreman.job_invocation: |
| 151 |
bookmark: 'active' |
| 152 |
command: 'pwd' |
| 153 |
job_template: "Run Command - Ansible Default" |
| 154 |
recurrence: |
| 155 |
cron_line: "30 2 * * *" |
| 156 |
concurrency_control: |
| 157 |
concurrency_level: 2 |
| 158 |
|
| 159 |
- name: "Run the katello_package_install feature to install a package on a single host" |
| 160 |
theforeman.foreman.job_invocation: |
| 161 |
search_query: "name ^ (foreman.example.com)" |
| 162 |
feature: 'katello_package_install' |
| 163 |
inputs: 'package=podman' |
| 164 |
''' |
| 165 |
|
| 166 |
RETURN = ''' |
| 167 |
entity: |
| 168 |
description: Final state of the affected entities grouped by their type. |
| 169 |
returned: success |
| 170 |
type: dict |
| 171 |
contains: |
| 172 |
job_invocations: |
| 173 |
description: List of job invocations |
| 174 |
type: list |
| 175 |
elements: dict |
| 176 |
''' |
| 177 |
|
| 178 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ( |
| 179 |
ForemanAnsibleModule, |
| 180 |
) |
| 181 |
|
| 182 |
ssh_foreman_spec = { |
| 183 |
'effective_user': dict(), |
| 184 |
} |
| 185 |
|
| 186 |
recurrence_foreman_spec = { |
| 187 |
'cron_line': dict(), |
| 188 |
'max_iteration': dict(type='int'), |
| 189 |
'end_time': dict(), |
| 190 |
'purpose': dict(), |
| 191 |
} |
| 192 |
|
| 193 |
scheduling_foreman_spec = { |
| 194 |
'start_at': dict(), |
| 195 |
'start_before': dict(), |
| 196 |
} |
| 197 |
|
| 198 |
concurrency_control_foreman_spec = { |
| 199 |
'time_span': dict(type='int'), |
| 200 |
'concurrency_level': dict(type='int'), |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
class ForemanJobInvocationModule(ForemanAnsibleModule): |
| 205 |
pass |
| 206 |
|
| 207 |
|
| 208 |
def main(): |
| 209 |
module = ForemanJobInvocationModule( |
| 210 |
foreman_spec=dict( |
| 211 |
search_query=dict(), |
| 212 |
bookmark=dict(type='entity'), |
| 213 |
job_template=dict(type='entity'), |
| 214 |
targeting_type=dict(default='static_query', choices=['static_query', 'dynamic_query']), |
| 215 |
randomized_ordering=dict(type='bool'), |
| 216 |
command=dict(), |
| 217 |
inputs=dict(type='dict'), |
| 218 |
execution_timeout_interval=dict(type='int'), |
| 219 |
ssh=dict(type='dict', options=ssh_foreman_spec), |
| 220 |
recurrence=dict(type='dict', options=recurrence_foreman_spec), |
| 221 |
scheduling=dict(type='dict', options=scheduling_foreman_spec), |
| 222 |
concurrency_control=dict(type='dict', options=concurrency_control_foreman_spec), |
| 223 |
description_format=dict(), |
| 224 |
feature=dict(), |
| 225 |
), |
| 226 |
required_one_of=[['search_query', 'bookmark'], ['job_template', 'feature']], |
| 227 |
required_if=[ |
| 228 |
['job_template', 'Run Command - SSH Default', ['command']], |
| 229 |
['job_template', 'Run Command - Ansible Default', ['command']], |
| 230 |
], |
| 231 |
) |
| 232 |
|
| 233 |
|
| 234 |
if 'command' in module.foreman_params: |
| 235 |
module.foreman_params['inputs'] = {"command": module.foreman_params.pop('command')} |
| 236 |
|
| 237 |
with module.api_connection(): |
| 238 |
if 'bookmark' in module.foreman_params: |
| 239 |
module.set_entity('bookmark', module.find_resource('bookmarks', search='name="{0}",controller="hosts"'.format( |
| 240 |
module.foreman_params['bookmark']), |
| 241 |
failsafe=False, |
| 242 |
)) |
| 243 |
module.auto_lookup_entities() |
| 244 |
module.ensure_entity('job_invocations', module.foreman_params, None, state='present') |
| 245 |
|
| 246 |
|
| 247 |
if __name__ == '__main__': |
| 248 |
main() |