Debian Codegraph @

ansible_collections/fortinet/fortimanager/plugins/modules/fmgr_dvmdb_script_execute.py

raw symbols SCIP
1 #!/usr/bin/python
2 from __future__ import absolute_import, division, print_function
3 # Copyright 2019-2024 Fortinet, Inc.
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
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_execute
15 short_description: Run script.
16 version_added: "1.0.0"
17 extends_documentation_fragment:
18 - fortinet.fortimanager.general
19 options:
20 adom:
21 description: The parameter (adom) in requested url.
22 type: str
23 required: true
24 dvmdb_script_execute:
25 description: The top level parameters set.
26 required: false
27 type: dict
28 suboptions:
29 adom:
30 type: str
31 description: Adom.
32 package:
33 type: str
34 description: Package.
35 scope:
36 type: list
37 elements: dict
38 description: Scope.
39 suboptions:
40 name:
41 type: str
42 description: Name.
43 vdom:
44 type: str
45 description: Vdom.
46 script:
47 type: str
48 description: Script name.
49 pblock:
50 type: str
51 description: Pblock.
52 '''
53
54 EXAMPLES = '''
55 - name: Apply a script to device (For FMG <= 7.6.4)
56 hosts: fortimanagers
57 gather_facts: false
58 connection: httpapi
59 vars:
60 ansible_httpapi_use_ssl: true
61 ansible_httpapi_validate_certs: false
62 ansible_httpapi_port: 443
63 device_adom: "root"
64 script_name: "FooScript"
65 device_name: "CustomHostName"
66 device_vdom: "root"
67 tasks:
68 # For FMG 7.6.4 and earlier, use fmgr_dvmdb_script.
69 # For FMG 7.6.5 and later, use fmgr_fmg_script.
70 - name: Create a Script to later execute
71 fortinet.fortimanager.fmgr_dvmdb_script:
72 adom: "{{ device_adom }}"
73 state: "present"
74 dvmdb_script:
75 name: "{{ script_name }}"
76 desc: "A script created via Ansible"
77 content: |
78 config system global
79 set remoteauthtimeout 80
80 end
81 type: "cli"
82 - name: Run the Script
83 fortinet.fortimanager.fmgr_dvmdb_script_execute:
84 adom: "{{ device_adom }}"
85 dvmdb_script_execute:
86 adom: "{{ device_adom }}"
87 script: "{{ script_name }}"
88 scope:
89 - name: "{{ device_name }}"
90 vdom: "{{ device_vdom }}"
91 register: running_task
92 - name: Inspect the Task Status
93 fortinet.fortimanager.fmgr_fact:
94 facts:
95 selector: "task_task"
96 params:
97 task: "{{ running_task.meta.response_data.task }}"
98 register: taskinfo
99 until: taskinfo.meta.response_data.percent == 100
100 retries: 30
101 delay: 3
102 failed_when: taskinfo.meta.response_data.state == 'error'
103
104 - name: Example playbook
105 hosts: fortimanagers
106 gather_facts: false
107 connection: httpapi
108 vars:
109 ansible_httpapi_use_ssl: true
110 ansible_httpapi_validate_certs: false
111 ansible_httpapi_port: 443
112 tasks:
113 - name: Run script.
114 fortinet.fortimanager.fmgr_dvmdb_script_execute:
115 bypass_validation: false
116 adom: ansible
117 dvmdb_script_execute:
118 adom: ansible
119 package: "your_value"
120 scope:
121 - name: Ansible-test
122 vdom: root
123 script: ansible-test
124
125 - name: Create and run script (For FMG 7.6.5+)
126 hosts: fortimanagers
127 gather_facts: false
128 connection: httpapi
129 vars:
130 fmg_adom: "root"
131 script_name: "your_script_name"
132 device_name: "your_device_name"
133 device_vdom: "root"
134 state: "present"
135 tasks:
136 # For FMG 7.6.4 and earlier, use fmgr_dvmdb_script.
137 # For FMG 7.6.5 and later, use fmgr_fmg_script.
138 - name: Create a script (For FMG 7.6.5+)
139 fortinet.fortimanager.fmgr_fmg_script:
140 state: "{{ state }}"
141 adom: "{{ fmg_adom }}"
142 fmg_script:
143 name: "{{ script_name }}"
144 content: |
145 config system global
146 set remoteauthtimeout 80
147 end
148 type: cli
149 desc: A script created via Ansible
150 target: devdb
151 - name: Run the Script
152 fortinet.fortimanager.fmgr_dvmdb_script_execute:
153 adom: "{{ fmg_adom }}"
154 dvmdb_script_execute:
155 adom: "{{ fmg_adom }}"
156 script: "{{ script_name }}"
157 scope:
158 - name: "{{ device_name }}"
159 vdom: "{{ device_vdom }}"
160 register: running_task
161 - name: Inspect the Task Status
162 fortinet.fortimanager.fmgr_fact:
163 facts:
164 selector: "task_task"
165 params:
166 task: "{{ running_task.meta.response_data.task }}"
167 register: taskinfo
168 until: taskinfo.meta.response_data.percent == 100
169 retries: 30
170 delay: 3
171 failed_when: taskinfo.meta.response_data.state == 'error'
172 '''
173
174 RETURN = '''
175 meta:
176 description: The result of the request.
177 type: dict
178 returned: always
179 contains:
180 request_url:
181 description: The full url requested.
182 returned: always
183 type: str
184 sample: /sys/login/user
185 response_code:
186 description: The status of api request.
187 returned: always
188 type: int
189 sample: 0
190 response_data:
191 description: The api response.
192 type: list
193 returned: always
194 response_message:
195 description: The descriptive message of the api response.
196 type: str
197 returned: always
198 sample: OK.
199 system_information:
200 description: The information of the target system.
201 type: dict
202 returned: always
203 rc:
204 description: The status the request.
205 type: int
206 returned: always
207 sample: 0
208 version_check_warning:
209 description: Warning if the parameters used in the playbook are not supported by the current FortiManager version.
210 type: list
211 returned: complex
212 '''
213 from ansible.module_utils.basic import AnsibleModule
214 from ansible.module_utils.connection import Connection
215 from ansible_collections.fortinet.fortimanager.plugins.module_utils.napi import NAPIManager, check_galaxy_version, check_parameter_bypass
216 from ansible_collections.fortinet.fortimanager.plugins.module_utils.common import get_module_arg_spec
217
218
219 def main():
220 urls_list = [
221 '/dvmdb/adom/{adom}/script/execute',
222 '/dvmdb/global/script/execute',
223 '/dvmdb/script/execute'
224 ]
225 module_arg_spec = {
226 'adom': {'required': True, 'type': 'str'},
227 'dvmdb_script_execute': {
228 'type': 'dict', 'v_range': [['6.0.0', '']],
229 'options': {
230 'adom': {'v_range': [['6.4.7', '6.4.15'], ['7.0.1', '']], 'type': 'str'},
231 'package': {'v_range': [['6.4.7', '6.4.15'], ['7.0.1', '']], 'type': 'str'},
232 'scope': {
233 'v_range': [['6.4.7', '6.4.15'], ['7.0.1', '']],
234 'type': 'list',
235 'options': {
236 'name': {'v_range': [['6.4.7', '6.4.15'], ['7.0.1', '']], 'type': 'str'},
237 'vdom': {'v_range': [['6.4.7', '6.4.15'], ['7.0.1', '']], 'type': 'str'}
238 },
239 'elements': 'dict'
240 },
241 'script': {'v_range': [['6.4.7', '6.4.15'], ['7.0.1', '']], 'type': 'str'},
242 'pblock': {'v_range': [['7.0.10', '7.0.16'], ['7.2.5', '7.2.12'], ['7.4.2', '']], 'type': 'str'}
243 }
244 }
245 }
246
247 module_option_spec = get_module_arg_spec('exec')
248 module_arg_spec.update(module_option_spec)
249 check_galaxy_version(module_arg_spec)
250 module = AnsibleModule(argument_spec=check_parameter_bypass(module_arg_spec, 'dvmdb_script_execute'),
251 supports_check_mode=True)
252 if not module._socket_path:
253 module.fail_json(msg='MUST RUN IN HTTPAPI MODE')
254 connection = Connection(module._socket_path)
255 fmgr = NAPIManager('exec', module_arg_spec, urls_list,
256 None, 'data', module, connection)
257 fmgr.process_exec()
258 module.exit_json(meta=module.params)
259
260
261 if __name__ == '__main__':
262 main()