Debian Codegraph @

ansible_collections/community/ciscosmb/plugins/modules/command.py

raw
1 #!/usr/bin/python
2
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import (absolute_import, division, print_function)
6 __metaclass__ = type
7
8 DOCUMENTATION = '''
9 ---
10 module: command
11 author: "Petr Klima (@qaxi)"
12 short_description: Run commands on remote Cisco SMB devices
13 description:
14 - Sends arbitrary commands to an Cisco SMB node and returns the results
15 read from the device. This module includes an
16 argument that will cause the module to wait for a specific condition
17 before returning or timing out if the condition is not met.
18 options:
19 commands:
20 description:
21 - List of commands to send to the remote Cisco SMB device over the
22 configured provider. The resulting output from the command
23 is returned. If the I(wait_for) argument is provided, the
24 module is not returned until the condition is satisfied or
25 the number of retries has expired.
26 required: true
27 type: list
28 elements: str
29 wait_for:
30 description:
31 - List of conditions to evaluate against the output of the
32 command. The task will wait for each condition to be true
33 before moving forward. If the conditional is not true
34 within the configured number of retries, the task fails.
35 See examples.
36 type: list
37 elements: str
38 match:
39 description:
40 - The I(match) argument is used in conjunction with the
41 I(wait_for) argument to specify the match policy. Valid
42 values are C(all) or C(any). If the value is set to C(all)
43 then all conditionals in the wait_for must be satisfied. If
44 the value is set to C(any) then only one of the values must be
45 satisfied.
46 default: all
47 type: str
48 choices: ['any', 'all']
49 retries:
50 description:
51 - Specifies the number of retries a command should by tried
52 before it is considered failed. The command is run on the
53 target device every retry and evaluated against the
54 I(wait_for) conditions.
55 default: 10
56 type: int
57 interval:
58 description:
59 - Configures the interval in seconds to wait between retries
60 of the command. If the command does not pass the specified
61 conditions, the interval indicates how long to wait before
62 trying the command again.
63 default: 1
64 type: int
65 '''
66
67 EXAMPLES = """
68 - name: Run command on remote devices
69 community.ciscosmb.command:
70 commands: show version
71
72 - name: Run command and check to see if output contains PID
73 community.ciscosmb.command:
74 commands: show inventory
75 wait_for: result[0] contains PID
76
77 - name: Run multiple commands on remote nodes
78 community.ciscosmb.command:
79 commands:
80 - show version
81 - show system
82
83 - name: Run multiple commands and evaluate the output
84 community.ciscosmb.command:
85 commands:
86 - show version
87 - show system
88 wait_for:
89 - result[0] contains Active-image
90 - result[1] contains "System Up Time"
91 """
92
93 RETURN = """
94 stdout:
95 description: The set of responses from the commands.
96 returned: always apart from low level errors (such as action plugin)
97 type: list
98 sample: ['...', '...']
99 stdout_lines:
100 description: The value of stdout split into a list.
101 returned: always apart from low level errors (such as action plugin)
102 type: list
103 sample: [['...', '...'], ['...'], ['...']]
104 failed_conditions:
105 description: The list of conditionals that have failed.
106 returned: failed
107 type: list
108 sample: ['...', '...']
109 """
110
111 import time
112
113 from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.parsing import Conditional
114 from ansible_collections.community.ciscosmb.plugins.module_utils.ciscosmb import run_commands
115 from ansible_collections.community.ciscosmb.plugins.module_utils.ciscosmb import ciscosmb_argument_spec
116 from ansible.module_utils.basic import AnsibleModule
117 from ansible.module_utils.six import string_types
118
119
120 def to_lines(stdout):
121 for item in stdout:
122 if isinstance(item, string_types):
123 item = str(item).split('\n')
124 yield item
125
126
127 def main():
128 """main entry point for module execution
129 """
130 argument_spec = dict(
131 commands=dict(type='list', elements='str', required=True),
132
133 wait_for=dict(type='list', elements='str'),
134 match=dict(default='all', choices=['all', 'any']),
135
136 retries=dict(default=10, type='int'),
137 interval=dict(default=1, type='int')
138 )
139
140 argument_spec.update(ciscosmb_argument_spec)
141
142 module = AnsibleModule(argument_spec=argument_spec,
143 supports_check_mode=False)
144
145 result = {'changed': False}
146
147 wait_for = module.params['wait_for'] or list()
148 conditionals = [Conditional(c) for c in wait_for]
149
150 retries = module.params['retries']
151 interval = module.params['interval']
152 match = module.params['match']
153
154 while retries > 0:
155 responses = run_commands(module, module.params['commands'])
156
157 for item in list(conditionals):
158 if item(responses):
159 if match == 'any':
160 conditionals = list()
161 break
162 conditionals.remove(item)
163
164 if not conditionals:
165 break
166
167 time.sleep(interval)
168 retries -= 1
169
170 if conditionals:
171 failed_conditions = [item.raw for item in conditionals]
172 msg = 'One or more conditional statements have not been satisfied'
173 module.fail_json(msg=msg, failed_conditions=failed_conditions)
174
175 result.update({
176 'changed': False,
177 'stdout': responses,
178 'stdout_lines': list(to_lines(responses))
179 })
180
181 module.exit_json(**result)
182
183
184 if __name__ == '__main__':
185 main()