Debian Codegraph @

ansible_collections/fortinet/fortimanager/plugins/modules/fmgr_fmg_script.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_fmg_script
15 short_description: Fmg script
16 version_added: "2.13.0"
17 extends_documentation_fragment:
18 - fortinet.fortimanager.general
19 - fortinet.fortimanager.general.full_crud
20 options:
21 revision_note:
22 description: The change note that can be specified when an object is created or updated.
23 type: str
24 adom:
25 description: The parameter (adom) in requested url.
26 type: str
27 required: true
28 fmg_script:
29 description: The top level parameters set.
30 required: false
31 type: dict
32 suboptions:
33 content:
34 type: str
35 description: Content.
36 desc:
37 type: str
38 description: Desc.
39 filter_build:
40 type: int
41 description: Filter build.
42 filter_device:
43 type: int
44 description: Filter device.
45 filter_hostname:
46 type: str
47 description: Filter hostname.
48 filter_ostype:
49 type: int
50 description: Filter ostype.
51 filter_osver:
52 type: int
53 description: Filter osver.
54 filter_platform:
55 type: str
56 description: Filter platform.
57 filter_serial:
58 type: str
59 description: Filter serial.
60 member:
61 type: list
62 elements: str
63 description: Member.
64 name:
65 type: str
66 description: Name.
67 required: true
68 schedule:
69 type: list
70 elements: dict
71 description: Schedule.
72 suboptions:
73 datetime:
74 type: str
75 description: Datetime.
76 day_of_week:
77 aliases: ['day-of-week']
78 type: int
79 description: Day of week.
80 device:
81 type: int
82 description: Device.
83 run_on_db:
84 aliases: ['run-on-db']
85 type: int
86 description: Run on db.
87 timestamp:
88 type: int
89 description: Timestamp.
90 type:
91 type: str
92 description: Type.
93 choices: ['auto', 'onetime', 'daily', 'weekly', 'monthly']
94 user:
95 type: str
96 description: User.
97 target:
98 type: str
99 description: Target.
100 choices: ['devdb', 'remote', 'adomdb']
101 type:
102 type: str
103 description: Type.
104 choices: ['cli', 'tcl', 'cligrp', 'tclgrp', 'jinja']
105 '''
106
107 EXAMPLES = '''
108 - name: Create and run script (For FMG 7.6.5+)
109 hosts: fortimanagers
110 gather_facts: false
111 connection: httpapi
112 vars:
113 fmg_adom: "root"
114 script_name: "your_script_name"
115 device_name: "your_device_name"
116 device_vdom: "root"
117 state: "present"
118 tasks:
119 # For FMG 7.6.4 and earlier, use fmgr_dvmdb_script.
120 # For FMG 7.6.5 and later, use fmgr_fmg_script.
121 - name: Create a script (For FMG 7.6.5+)
122 fortinet.fortimanager.fmgr_fmg_script:
123 state: "{{ state }}"
124 adom: "{{ fmg_adom }}"
125 fmg_script:
126 name: "{{ script_name }}"
127 content: |
128 config system global
129 set remoteauthtimeout 80
130 end
131 type: cli
132 desc: A script created via Ansible
133 target: devdb
134 - name: Run the Script
135 fortinet.fortimanager.fmgr_dvmdb_script_execute:
136 adom: "{{ fmg_adom }}"
137 dvmdb_script_execute:
138 adom: "{{ fmg_adom }}"
139 script: "{{ script_name }}"
140 scope:
141 - name: "{{ device_name }}"
142 vdom: "{{ device_vdom }}"
143 register: running_task
144 - name: Inspect the Task Status
145 fortinet.fortimanager.fmgr_fact:
146 facts:
147 selector: "task_task"
148 params:
149 task: "{{ running_task.meta.response_data.task }}"
150 register: taskinfo
151 until: taskinfo.meta.response_data.percent == 100
152 retries: 30
153 delay: 3
154 failed_when: taskinfo.meta.response_data.state == 'error'
155 '''
156
157 RETURN = '''
158 meta:
159 description: The result of the request.
160 type: dict
161 returned: always
162 contains:
163 request_url:
164 description: The full url requested.
165 returned: always
166 type: str
167 sample: /sys/login/user
168 response_code:
169 description: The status of api request.
170 returned: always
171 type: int
172 sample: 0
173 response_data:
174 description: The api response.
175 type: list
176 returned: always
177 response_message:
178 description: The descriptive message of the api response.
179 type: str
180 returned: always
181 sample: OK.
182 system_information:
183 description: The information of the target system.
184 type: dict
185 returned: always
186 rc:
187 description: The status the request.
188 type: int
189 returned: always
190 sample: 0
191 version_check_warning:
192 description: Warning if the parameters used in the playbook are not supported by the current FortiManager version.
193 type: list
194 returned: complex
195 '''
196 from ansible.module_utils.basic import AnsibleModule
197 from ansible.module_utils.connection import Connection
198 from ansible_collections.fortinet.fortimanager.plugins.module_utils.napi import NAPIManager, check_galaxy_version, check_parameter_bypass
199 from ansible_collections.fortinet.fortimanager.plugins.module_utils.common import get_module_arg_spec
200
201
202 def main():
203 urls_list = [
204 '/pm/config/adom/{adom}/obj/fmg/script',
205 '/pm/config/global/obj/fmg/script'
206 ]
207 module_arg_spec = {
208 'adom': {'required': True, 'type': 'str'},
209 'revision_note': {'type': 'str'},
210 'fmg_script': {
211 'type': 'dict', 'v_range': [['7.6.5', '']],
212 'options': {
213 'content': {'v_range': [['7.6.5', '']], 'type': 'str'},
214 'desc': {'v_range': [['7.6.5', '']], 'type': 'str'},
215 'filter_build': {'v_range': [['7.6.5', '']], 'type': 'int'},
216 'filter_device': {'v_range': [['7.6.5', '']], 'type': 'int'},
217 'filter_hostname': {'v_range': [['7.6.5', '']], 'type': 'str'},
218 'filter_ostype': {'v_range': [['7.6.5', '']], 'type': 'int'},
219 'filter_osver': {'v_range': [['7.6.5', '']], 'type': 'int'},
220 'filter_platform': {'v_range': [['7.6.5', '']], 'type': 'str'},
221 'filter_serial': {'v_range': [['7.6.5', '']], 'type': 'str'},
222 'member': {'v_range': [['7.6.5', '']], 'type': 'list', 'elements': 'str'},
223 'name': {'v_range': [['7.6.5', '']], 'required': True, 'type': 'str'},
224 'schedule': {
225 'v_range': [['7.6.5', '']],
226 'type': 'list',
227 'options': {
228 'datetime': {'v_range': [['7.6.5', '']], 'type': 'str'},
229 'day-of-week': {'v_range': [['7.6.5', '']], 'type': 'int'},
230 'device': {'v_range': [['7.6.5', '']], 'type': 'int'},
231 'run-on-db': {'v_range': [['7.6.5', '']], 'type': 'int'},
232 'timestamp': {'v_range': [['7.6.5', '']], 'type': 'int'},
233 'type': {'v_range': [['7.6.5', '']], 'choices': ['auto', 'onetime', 'daily', 'weekly', 'monthly'], 'type': 'str'},
234 'user': {'v_range': [['7.6.5', '']], 'type': 'str'}
235 },
236 'elements': 'dict'
237 },
238 'target': {'v_range': [['7.6.5', '']], 'choices': ['devdb', 'remote', 'adomdb'], 'type': 'str'},
239 'type': {'v_range': [['7.6.5', '']], 'choices': ['cli', 'tcl', 'cligrp', 'tclgrp', 'jinja'], 'type': 'str'}
240 }
241 }
242 }
243
244 module_option_spec = get_module_arg_spec('full crud')
245 module_arg_spec.update(module_option_spec)
246 check_galaxy_version(module_arg_spec)
247 module = AnsibleModule(argument_spec=check_parameter_bypass(module_arg_spec, 'fmg_script'),
248 supports_check_mode=True)
249 if not module._socket_path:
250 module.fail_json(msg='MUST RUN IN HTTPAPI MODE')
251 connection = Connection(module._socket_path)
252 fmgr = NAPIManager('full crud', module_arg_spec, urls_list,
253 'name', 'data', module, connection)
254 fmgr.process_crud()
255 module.exit_json(meta=module.params)
256
257
258 if __name__ == '__main__':
259 main()