Debian Codegraph @

ansible_collections/community/hrobot/plugins/modules/failover_ip.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2019 Felix Fontein <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = r"""
13 module: failover_ip
14 short_description: Manage Hetzner's failover IPs
15 author:
16 - Felix Fontein (@felixfontein)
17 description:
18 - Manage Hetzner's failover IPs.
19 seealso:
20 - name: Failover IP documentation
21 description: Hetzner's documentation on failover IPs.
22 link: https://docs.hetzner.com/robot/dedicated-server/ip/failover/
23 - module: community.hrobot.failover_ip_info
24 description: Retrieve information on failover IPs.
25 extends_documentation_fragment:
26 - community.hrobot.robot
27 - community.hrobot.attributes
28 - community.hrobot.attributes.actiongroup_robot
29
30 attributes:
31 action_group:
32 version_added: 1.6.0
33 check_mode:
34 support: full
35 diff_mode:
36 support: full
37 idempotent:
38 support: full
39
40 options:
41 failover_ip:
42 description: The failover IP address.
43 type: str
44 required: true
45 state:
46 description:
47 - Defines whether the IP will be routed or not.
48 - If set to V(routed), O(value) must be specified.
49 type: str
50 choices:
51 - routed
52 - unrouted
53 default: routed
54 value:
55 description:
56 - The new value for the failover IP address.
57 - Required when setting O(state) to V(routed).
58 type: str
59 timeout:
60 description:
61 - Timeout to use when routing or unrouting the failover IP.
62 - Note that the API call returns when the failover IP has been successfully routed to the new address, respectively
63 successfully unrouted.
64 type: int
65 default: 180
66 """
67
68 EXAMPLES = r"""
69 ---
70 - name: Set value of failover IP 1.2.3.4 to 5.6.7.8
71 community.hrobot.failover_ip:
72 hetzner_user: foo
73 hetzner_password: bar
74 failover_ip: 1.2.3.4
75 value: 5.6.7.8
76
77 - name: Set value of failover IP 1.2.3.4 to unrouted
78 community.hrobot.failover_ip:
79 hetzner_user: foo
80 hetzner_password: bar
81 failover_ip: 1.2.3.4
82 state: unrouted
83 """
84
85 RETURN = r"""
86 value:
87 description:
88 - The value of the failover IP.
89 - Will be V(none) if the IP is unrouted.
90 returned: success
91 type: str
92 state:
93 description:
94 - Will be V(routed) or V(unrouted).
95 returned: success
96 type: str
97 """
98
99 from ansible.module_utils.basic import AnsibleModule
100 from ansible_collections.community.hrobot.plugins.module_utils.robot import (
101 ROBOT_DEFAULT_ARGUMENT_SPEC,
102 )
103 from ansible_collections.community.hrobot.plugins.module_utils.failover import (
104 get_failover,
105 set_failover,
106 get_failover_state,
107 )
108
109
110 def main():
111 argument_spec = dict(
112 failover_ip=dict(type='str', required=True),
113 state=dict(type='str', default='routed', choices=['routed', 'unrouted']),
114 value=dict(type='str'),
115 timeout=dict(type='int', default=180),
116 )
117 argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC)
118 module = AnsibleModule(
119 argument_spec=argument_spec,
120 supports_check_mode=True,
121 required_if=(
122 ('state', 'routed', ['value']),
123 ),
124 )
125
126 failover_ip = module.params['failover_ip']
127 value = get_failover(module, failover_ip)
128 changed = False
129 before = get_failover_state(value)
130
131 if module.params['state'] == 'routed':
132 new_value = module.params['value']
133 else:
134 new_value = None
135
136 if value != new_value:
137 if module.check_mode:
138 value = new_value
139 changed = True
140 else:
141 value, changed = set_failover(module, failover_ip, new_value, timeout=module.params['timeout'])
142
143 after = get_failover_state(value)
144 module.exit_json(
145 changed=changed,
146 diff=dict(
147 before=before,
148 after=after,
149 ),
150 **after
151 )
152
153
154 if __name__ == '__main__': # pragma: no cover
155 main() # pragma: no cover