Debian Codegraph @

lib/ansible/playbook/loop_control.py

raw
1 # (c) 2012-2014, Michael DeHaan <[email protected]>
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 from ansible._internal._templating._engine import TemplateEngine
21 from ansible.errors import AnsibleError
22 from ansible.playbook.attribute import NonInheritableFieldAttribute
23 from ansible.playbook.base import FieldAttributeBase
24 from ansible.utils.vars import validate_variable_name
25
26
27 class LoopControl(FieldAttributeBase):
28
29 loop_var = NonInheritableFieldAttribute(isa='string', default='item', always_post_validate=True)
30 index_var = NonInheritableFieldAttribute(isa='string', always_post_validate=True)
31 label = NonInheritableFieldAttribute(isa='string')
32 pause = NonInheritableFieldAttribute(isa='float', default=0, always_post_validate=True)
33 extended = NonInheritableFieldAttribute(isa='bool', always_post_validate=True)
34 extended_allitems = NonInheritableFieldAttribute(isa='bool', default=True, always_post_validate=True)
35 break_when = NonInheritableFieldAttribute(isa='list', default=list, always_post_validate=True)
36
37 def __init__(self):
38 super(LoopControl, self).__init__()
39
40 @staticmethod
41 def load(data, variable_manager=None, loader=None):
42 t = LoopControl()
43 return t.load_data(data, variable_manager=variable_manager, loader=loader)
44
45 def post_validate(self, templar: TemplateEngine) -> None:
46 super().post_validate(templar)
47
48 try:
49 validate_variable_name(self.loop_var)
50 except AnsibleError as ex:
51 raise AnsibleError("Invalid 'loop_var'.") from ex
52
53 if self.index_var is not None:
54 try:
55 validate_variable_name(self.index_var)
56 except AnsibleError as ex:
57 raise AnsibleError("Invalid 'index_var'.") from ex
58
59 def _post_validate_break_when(self, attr, value, templar):
60 """
61 break_when is evaluated after the execution of the loop is complete,
62 and should not be templated during the regular post_validate step.
63 """
64 return value