| 1 |
|
| 2 |
from __future__ import absolute_import, division, print_function |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
ANSIBLE_METADATA = {'status': ['preview'], |
| 9 |
'supported_by': 'community', |
| 10 |
'metadata_version': '1.1'} |
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: fmgr_dvmdb_script |
| 15 |
short_description: Script table. |
| 16 |
version_added: "1.0.0" |
| 17 |
extends_documentation_fragment: |
| 18 |
- fortinet.fortimanager.general |
| 19 |
- fortinet.fortimanager.general.full_crud |
| 20 |
options: |
| 21 |
adom: |
| 22 |
description: The parameter (adom) in requested url. |
| 23 |
type: str |
| 24 |
required: true |
| 25 |
dvmdb_script: |
| 26 |
description: The top level parameters set. |
| 27 |
required: false |
| 28 |
type: dict |
| 29 |
suboptions: |
| 30 |
content: |
| 31 |
type: str |
| 32 |
description: The full content of the script result log. |
| 33 |
desc: |
| 34 |
type: str |
| 35 |
description: Desc. |
| 36 |
filter_build: |
| 37 |
type: int |
| 38 |
description: The value will be ignored in add/set/update requests if filter_ostype is not set. |
| 39 |
filter_device: |
| 40 |
type: int |
| 41 |
description: Name or id of an existing device in the database. |
| 42 |
filter_hostname: |
| 43 |
type: str |
| 44 |
description: The value has no effect if target is adom_database. |
| 45 |
filter_ostype: |
| 46 |
type: str |
| 47 |
description: The value has no effect if target is adom_database. |
| 48 |
choices: ['unknown', 'fos'] |
| 49 |
filter_osver: |
| 50 |
type: str |
| 51 |
description: The value will be ignored in add/set/update requests if filter_ostype is not set. |
| 52 |
choices: ['unknown', '4.00', '5.00', '6.00'] |
| 53 |
filter_platform: |
| 54 |
type: str |
| 55 |
description: The value will be ignored in add/set/update requests if filter_ostype is not set. |
| 56 |
filter_serial: |
| 57 |
type: str |
| 58 |
description: The value has no effect if target is adom_database. |
| 59 |
modification_time: |
| 60 |
type: str |
| 61 |
description: It is a read-only attribute indicating the time when the script was created or modified. |
| 62 |
name: |
| 63 |
type: str |
| 64 |
description: Name. |
| 65 |
required: true |
| 66 |
script_schedule: |
| 67 |
type: list |
| 68 |
elements: dict |
| 69 |
description: Script schedule. |
| 70 |
suboptions: |
| 71 |
datetime: |
| 72 |
type: str |
| 73 |
description: |
| 74 |
- Indicates the date and time of the schedule. |
| 75 |
- onetime |
| 76 |
- daily |
| 77 |
- weekly |
| 78 |
- monthly |
| 79 |
day_of_week: |
| 80 |
type: str |
| 81 |
description: Day of week. |
| 82 |
choices: ['unknown', 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'] |
| 83 |
device: |
| 84 |
type: int |
| 85 |
description: Name or id of an existing device in the database. |
| 86 |
name: |
| 87 |
type: str |
| 88 |
description: Name. |
| 89 |
run_on_db: |
| 90 |
type: str |
| 91 |
description: Indicates if the scheduled script should be executed on device database. |
| 92 |
choices: ['disable', 'enable'] |
| 93 |
type: |
| 94 |
type: str |
| 95 |
description: Type. |
| 96 |
choices: ['auto', 'onetime', 'daily', 'weekly', 'monthly'] |
| 97 |
target: |
| 98 |
type: str |
| 99 |
description: Target. |
| 100 |
choices: ['device_database', 'remote_device', 'adom_database'] |
| 101 |
type: |
| 102 |
type: str |
| 103 |
description: Type. |
| 104 |
choices: ['cli', 'tcl', 'cligrp', 'tclgrp', 'jinja'] |
| 105 |
''' |
| 106 |
|
| 107 |
EXAMPLES = ''' |
| 108 |
- name: Apply a script to device (For FMG <= 7.6.4) |
| 109 |
hosts: fortimanagers |
| 110 |
gather_facts: false |
| 111 |
connection: httpapi |
| 112 |
vars: |
| 113 |
ansible_httpapi_use_ssl: true |
| 114 |
ansible_httpapi_validate_certs: false |
| 115 |
ansible_httpapi_port: 443 |
| 116 |
device_adom: "root" |
| 117 |
script_name: "FooScript" |
| 118 |
device_name: "CustomHostName" |
| 119 |
device_vdom: "root" |
| 120 |
tasks: |
| 121 |
# For FMG 7.6.4 and earlier, use fmgr_dvmdb_script. |
| 122 |
# For FMG 7.6.5 and later, use fmgr_fmg_script. |
| 123 |
- name: Create a Script to later execute |
| 124 |
fortinet.fortimanager.fmgr_dvmdb_script: |
| 125 |
adom: "{{ device_adom }}" |
| 126 |
state: "present" |
| 127 |
dvmdb_script: |
| 128 |
name: "{{ script_name }}" |
| 129 |
desc: "A script created via Ansible" |
| 130 |
content: | |
| 131 |
config system global |
| 132 |
set remoteauthtimeout 80 |
| 133 |
end |
| 134 |
type: "cli" |
| 135 |
- name: Run the Script |
| 136 |
fortinet.fortimanager.fmgr_dvmdb_script_execute: |
| 137 |
adom: "{{ device_adom }}" |
| 138 |
dvmdb_script_execute: |
| 139 |
adom: "{{ device_adom }}" |
| 140 |
script: "{{ script_name }}" |
| 141 |
scope: |
| 142 |
- name: "{{ device_name }}" |
| 143 |
vdom: "{{ device_vdom }}" |
| 144 |
register: running_task |
| 145 |
- name: Inspect the Task Status |
| 146 |
fortinet.fortimanager.fmgr_fact: |
| 147 |
facts: |
| 148 |
selector: "task_task" |
| 149 |
params: |
| 150 |
task: "{{ running_task.meta.response_data.task }}" |
| 151 |
register: taskinfo |
| 152 |
until: taskinfo.meta.response_data.percent == 100 |
| 153 |
retries: 30 |
| 154 |
delay: 3 |
| 155 |
failed_when: taskinfo.meta.response_data.state == 'error' |
| 156 |
|
| 157 |
- name: Example playbook |
| 158 |
hosts: fortimanagers |
| 159 |
gather_facts: false |
| 160 |
connection: httpapi |
| 161 |
vars: |
| 162 |
ansible_httpapi_use_ssl: true |
| 163 |
ansible_httpapi_validate_certs: false |
| 164 |
ansible_httpapi_port: 443 |
| 165 |
tasks: |
| 166 |
- name: Script table. |
| 167 |
fortinet.fortimanager.fmgr_dvmdb_script: |
| 168 |
bypass_validation: false |
| 169 |
adom: ansible |
| 170 |
state: present |
| 171 |
dvmdb_script: |
| 172 |
content: "ansible-test" |
| 173 |
name: "ansible-test" |
| 174 |
target: device_database |
| 175 |
type: cli |
| 176 |
|
| 177 |
- name: Gathering fortimanager facts |
| 178 |
hosts: fortimanagers |
| 179 |
gather_facts: false |
| 180 |
connection: httpapi |
| 181 |
vars: |
| 182 |
ansible_httpapi_use_ssl: true |
| 183 |
ansible_httpapi_validate_certs: false |
| 184 |
ansible_httpapi_port: 443 |
| 185 |
tasks: |
| 186 |
- name: Retrieve all the scripts in the device |
| 187 |
fortinet.fortimanager.fmgr_fact: |
| 188 |
facts: |
| 189 |
selector: "dvmdb_script" |
| 190 |
params: |
| 191 |
adom: "ansible" |
| 192 |
script: "your_value" |
| 193 |
|
| 194 |
- name: Example playbook |
| 195 |
hosts: fortimanagers |
| 196 |
gather_facts: false |
| 197 |
connection: httpapi |
| 198 |
vars: |
| 199 |
ansible_httpapi_use_ssl: true |
| 200 |
ansible_httpapi_validate_certs: false |
| 201 |
ansible_httpapi_port: 443 |
| 202 |
tasks: |
| 203 |
- name: Enable workspace mode |
| 204 |
fortinet.fortimanager.fmgr_system_global: |
| 205 |
system_global: |
| 206 |
adom_status: enable |
| 207 |
workspace_mode: normal |
| 208 |
|
| 209 |
- name: Script table. |
| 210 |
fortinet.fortimanager.fmgr_dvmdb_script: |
| 211 |
bypass_validation: false |
| 212 |
adom: root |
| 213 |
state: present |
| 214 |
workspace_locking_adom: "root" |
| 215 |
dvmdb_script: |
| 216 |
content: "ansible-test" |
| 217 |
name: "fooscript000" |
| 218 |
target: device_database |
| 219 |
type: cli |
| 220 |
|
| 221 |
- name: Verify script table |
| 222 |
fortinet.fortimanager.fmgr_fact: |
| 223 |
facts: |
| 224 |
selector: "dvmdb_script" |
| 225 |
params: |
| 226 |
adom: "root" |
| 227 |
script: "fooscript000" |
| 228 |
register: info |
| 229 |
failed_when: info.meta.response_code != 0 |
| 230 |
|
| 231 |
- name: Restore workspace mode |
| 232 |
fortinet.fortimanager.fmgr_system_global: |
| 233 |
system_global: |
| 234 |
adom_status: enable |
| 235 |
workspace_mode: disabled |
| 236 |
''' |
| 237 |
|
| 238 |
RETURN = ''' |
| 239 |
meta: |
| 240 |
description: The result of the request. |
| 241 |
type: dict |
| 242 |
returned: always |
| 243 |
contains: |
| 244 |
request_url: |
| 245 |
description: The full url requested. |
| 246 |
returned: always |
| 247 |
type: str |
| 248 |
sample: /sys/login/user |
| 249 |
response_code: |
| 250 |
description: The status of api request. |
| 251 |
returned: always |
| 252 |
type: int |
| 253 |
sample: 0 |
| 254 |
response_data: |
| 255 |
description: The api response. |
| 256 |
type: list |
| 257 |
returned: always |
| 258 |
response_message: |
| 259 |
description: The descriptive message of the api response. |
| 260 |
type: str |
| 261 |
returned: always |
| 262 |
sample: OK. |
| 263 |
system_information: |
| 264 |
description: The information of the target system. |
| 265 |
type: dict |
| 266 |
returned: always |
| 267 |
rc: |
| 268 |
description: The status the request. |
| 269 |
type: int |
| 270 |
returned: always |
| 271 |
sample: 0 |
| 272 |
version_check_warning: |
| 273 |
description: Warning if the parameters used in the playbook are not supported by the current FortiManager version. |
| 274 |
type: list |
| 275 |
returned: complex |
| 276 |
''' |
| 277 |
from ansible.module_utils.basic import AnsibleModule |
| 278 |
from ansible.module_utils.connection import Connection |
| 279 |
from ansible_collections.fortinet.fortimanager.plugins.module_utils.napi import NAPIManager, check_galaxy_version, check_parameter_bypass |
| 280 |
from ansible_collections.fortinet.fortimanager.plugins.module_utils.common import get_module_arg_spec |
| 281 |
|
| 282 |
|
| 283 |
def main(): |
| 284 |
urls_list = [ |
| 285 |
'/dvmdb/adom/{adom}/script', |
| 286 |
'/dvmdb/global/script', |
| 287 |
'/dvmdb/script' |
| 288 |
] |
| 289 |
module_arg_spec = { |
| 290 |
'adom': {'required': True, 'type': 'str'}, |
| 291 |
'dvmdb_script': { |
| 292 |
'type': 'dict', 'v_range': [['6.0.0', '']], |
| 293 |
'options': { |
| 294 |
'content': {'type': 'str'}, |
| 295 |
'desc': {'type': 'str'}, |
| 296 |
'filter_build': {'type': 'int'}, |
| 297 |
'filter_device': {'type': 'int'}, |
| 298 |
'filter_hostname': {'type': 'str'}, |
| 299 |
'filter_ostype': {'choices': ['unknown', 'fos'], 'type': 'str'}, |
| 300 |
'filter_osver': {'choices': ['unknown', '4.00', '5.00', '6.00'], 'type': 'str'}, |
| 301 |
'filter_platform': {'type': 'str'}, |
| 302 |
'filter_serial': {'type': 'str'}, |
| 303 |
'modification_time': {'type': 'str'}, |
| 304 |
'name': {'required': True, 'type': 'str'}, |
| 305 |
'script_schedule': { |
| 306 |
'type': 'list', |
| 307 |
'options': { |
| 308 |
'datetime': {'type': 'str'}, |
| 309 |
'day_of_week': {'choices': ['unknown', 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'], 'type': 'str'}, |
| 310 |
'device': {'type': 'int'}, |
| 311 |
'name': {'type': 'str'}, |
| 312 |
'run_on_db': {'choices': ['disable', 'enable'], 'type': 'str'}, |
| 313 |
'type': {'choices': ['auto', 'onetime', 'daily', 'weekly', 'monthly'], 'type': 'str'} |
| 314 |
}, |
| 315 |
'elements': 'dict' |
| 316 |
}, |
| 317 |
'target': {'choices': ['device_database', 'remote_device', 'adom_database'], 'type': 'str'}, |
| 318 |
'type': {'choices': ['cli', 'tcl', 'cligrp', 'tclgrp', 'jinja'], 'type': 'str'} |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
module_option_spec = get_module_arg_spec('full crud') |
| 324 |
module_arg_spec.update(module_option_spec) |
| 325 |
check_galaxy_version(module_arg_spec) |
| 326 |
module = AnsibleModule(argument_spec=check_parameter_bypass(module_arg_spec, 'dvmdb_script'), |
| 327 |
supports_check_mode=True) |
| 328 |
if not module._socket_path: |
| 329 |
module.fail_json(msg='MUST RUN IN HTTPAPI MODE') |
| 330 |
connection = Connection(module._socket_path) |
| 331 |
fmgr = NAPIManager('full crud', module_arg_spec, urls_list, |
| 332 |
'name', 'data', module, connection) |
| 333 |
fmgr.process_crud() |
| 334 |
module.exit_json(meta=module.params) |
| 335 |
|
| 336 |
|
| 337 |
if __name__ == '__main__': |
| 338 |
main() |