Browse code

Remove the 'host' module for now because the alias handling involves a bit too much automagic. Proposal is to come up with a spec of how it should work and accept to spec, using the existing patch (cherry-picked) as a baseline.

Michael DeHaan authored on 2013/11/20 04:12:45
Showing 2 changed files
... ...
@@ -38,7 +38,6 @@ New modules and plugins.
38 38
 * files: unarchive: pushes and extracts tarballs
39 39
 * files: synchronize: a useful wraper around rsyncing trees of files
40 40
 * system: firewalld -- manage the firewalld configuration
41
-* system: host -- manage `/etc/hosts` file entries
42 41
 * system: modprobe -- manage kernel modules on systems that support modprobe/rmmod
43 42
 * system: open_iscsi -- manage targets on an initiator using open-iscsi
44 43
 * system: blacklist: add or remove modules from the kernel blacklist
45 44
deleted file mode 100644
... ...
@@ -1,194 +0,0 @@
1
-#!/usr/bin/python
2
-# -*- coding: utf-8 -*-
3
-#
4
-# (c) 2013, René Moser <mail@renemoser.net>
5
-#
6
-# This file is part of Ansible
7
-#
8
-# Ansible is free software: you can redistribute it and/or modify
9
-# it under the terms of the GNU General Public License as published by
10
-# the Free Software Foundation, either version 3 of the License, or
11
-# (at your option) any later version.
12
-#
13
-# Ansible is distributed in the hope that it will be useful,
14
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
-# GNU General Public License for more details.
17
-#
18
-# You should have received a copy of the GNU General Public License
19
-# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
20
-
21
-DOCUMENTATION = '''
22
-module: host
23
-author: René Moser
24
-version_added: "1.4"
25
-short_description: Add, update or remove entries in C(/etc/hosts).
26
-requirements:
27
-description:
28
-    - Manage entries in C(/etc/hosts).
29
-options:
30
-    ip:
31
-        required: false
32
-        description:
33
-            - IP address. Required if C(state=present).
34
-    hostname:
35
-        required: false
36
-        description:
37
-            - name of host. Required if C(state=present).
38
-    aliases:
39
-        required: false
40
-        description:
41
-            - list of alias hostnames, comma separated.
42
-    state:
43
-        required: false
44
-        default: "present"
45
-        choices: [ present, absent ]
46
-        description:
47
-            - Whether the entries should be present or not in C(/etc/hosts).
48
-'''
49
-
50
-EXAMPLES = '''
51
-# Example host command from Ansible Playbooks
52
-- host: ip=127.0.0.1 hostname=localhost state=present
53
-- host: ip=127.0.0.1 hostname=localhost aliases=foobar.com,localhost.foobar.com
54
-- host: ip=192.168.1.1 state=absent
55
-- host: hostname=www.example.com state=absent
56
-- host: ip=::1 hostname=localhost6 aliases=ip6-localhost,ip6-loopback
57
-'''
58
-
59
-import os
60
-import tempfile
61
-import fileinput
62
-
63
-class Host(object):
64
-
65
-    HOSTSFILE = '/etc/hosts'
66
-
67
-    def __init__(self, module):
68
-        self.module             = module
69
-        self.state              = module.params['state']
70
-        self.ip                 = module.params['ip']
71
-        self.hostname           = module.params['hostname']
72
-        self.aliases            = module.params['aliases']
73
-
74
-        self._ip_matches        = False
75
-        self._hostname_matches  = False
76
-        self._aliases_matches   = False
77
-        self._found_on_line     = -1
78
-
79
-    def validate_has_hostname_on_present(self):
80
-        err = ''
81
-        if self.state == 'present' and not (self.hostname and self.ip):
82
-            err = "Error: No param 'hostnames' or 'ip' given in state 'present'."
83
-        return err
84
-
85
-    def validate_has_ip_or_hostname_on_absent(self):
86
-        err = ''
87
-        if self.state == 'absent':
88
-            if not (self.hostname or self.ip):
89
-                err = "Error: Either param 'hostnames' or 'ip' must be given in state 'absent'."
90
-            if self.hostname and self.ip:
91
-                err = "Error: Either param 'hostnames' or 'ip' must be given in state 'absent'."
92
-        return err
93
-
94
-    def proceed_hosts_entries(self):
95
-
96
-        f = open(self.HOSTSFILE,'rb')
97
-        self._hostsfile_lines = f.readlines()
98
-        f.close()
99
-
100
-        for lineno, line in enumerate(self._hostsfile_lines):
101
-            if line.startswith("#") or line.startswith("\n"):
102
-                continue
103
-
104
-            ip = line.split()[0:1]
105
-            hostname = line.split()[1:2]
106
-            aliases = ','.join(line.split()[2:])
107
-
108
-            if self.ip and self.ip in ip: 
109
-                self._ip_matches = True
110
-                self._found_on_line = lineno
111
-
112
-            if self.hostname and self.hostname in hostname:
113
-                self._hostname_matches = True
114
-                self._found_on_line = lineno
115
-
116
-            # only look at aliases if we found hostname or ip
117
-            if self._hostname_matches or self._ip_matches:
118
-                if self.aliases == aliases:
119
-                    self._aliases_matches = True
120
-                break
121
-
122
-    def full_entry_exists(self):
123
-        return self._ip_matches and self._hostname_matches and self._aliases_matches
124
-
125
-    def entry_exists(self):
126
-        return self._ip_matches or self._hostname_matches
127
-
128
-    def remove_entry(self):
129
-        self._hostsfile_lines.pop(self._found_on_line)
130
-
131
-    def add_entry(self):
132
-        aliases = ''
133
-        if self.aliases:
134
-            aliases = self.aliases.replace(',',' ')
135
-        host_entry = self.ip + " " + self.hostname + " " + aliases + "\n"
136
-        if self.entry_exists():
137
-            self._hostsfile_lines[self._found_on_line] = host_entry
138
-        else:
139
-            self._hostsfile_lines.extend(host_entry)
140
-
141
-    def write_changes(self):
142
-        tmpfd, tmpfile = tempfile.mkstemp()
143
-        f = os.fdopen(tmpfd,'wb')
144
-        f.writelines(self._hostsfile_lines)
145
-        f.close()
146
-        self.module.atomic_move(tmpfile, self.HOSTSFILE)
147
-
148
-def main():
149
-    module = AnsibleModule(
150
-        argument_spec = dict(
151
-            state=dict(default='present', choices=['present', 'absent'], type='str'),
152
-            ip=dict(default=None, type='str'),
153
-            hostname=dict(default=None, type='str'),
154
-            aliases=dict(default='', type='str'),
155
-        ),
156
-        supports_check_mode=True
157
-    )
158
-
159
-    result = {}
160
-    host = Host(module)
161
-    result['state'] = host.state
162
-    result['changed'] = False
163
-
164
-    result['msg'] = host.validate_has_hostname_on_present()
165
-    if result['msg']:
166
-        module.fail_json(**result)
167
-
168
-    result['msg'] = host.validate_has_ip_or_hostname_on_absent()
169
-    if result['msg']:
170
-        module.fail_json(**result)
171
-
172
-    host.proceed_hosts_entries()
173
-    if host.state == 'present':
174
-        if not host.full_entry_exists():
175
-            if module.check_mode:
176
-                module.exit_json(changed=True)
177
-            host.add_entry()
178
-            host.write_changes()
179
-            result['changed'] = True
180
-
181
-    elif host.state == 'absent':
182
-        if host.entry_exists():
183
-            if module.check_mode:
184
-                module.exit_json(changed=True)
185
-            host.remove_entry()
186
-            host.write_changes()
187
-            result['changed'] = True
188
-
189
-    module.exit_json(**result)
190
-
191
-# include magic from lib/ansible/module_common.py
192
-#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
193
-main()