2013-09-20 20:50:03 +00:00
|
|
|
#!/usr/bin/python
|
2013-09-06 11:23:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# (c) 2013, René Moser <mail@renemoser.net>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: host
|
|
|
|
author: René Moser
|
2013-09-15 12:55:53 +00:00
|
|
|
version_added: "1.4"
|
2013-09-24 13:46:24 +00:00
|
|
|
short_description: Add, update or remove entries in C(/etc/hosts).
|
2013-09-06 11:23:53 +00:00
|
|
|
requirements:
|
|
|
|
description:
|
2013-09-24 13:46:24 +00:00
|
|
|
- Manage entries in C(/etc/hosts).
|
2013-09-06 11:23:53 +00:00
|
|
|
options:
|
|
|
|
ip:
|
|
|
|
required: false
|
|
|
|
description:
|
2013-09-24 13:46:24 +00:00
|
|
|
- IP address. Required if C(state=present).
|
2013-09-06 11:23:53 +00:00
|
|
|
hostname:
|
|
|
|
required: false
|
|
|
|
description:
|
2013-09-24 13:46:24 +00:00
|
|
|
- name of host. Required if C(state=present).
|
2013-09-06 11:23:53 +00:00
|
|
|
aliases:
|
|
|
|
required: false
|
|
|
|
description:
|
|
|
|
- list of alias hostnames, comma separated.
|
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
default: "present"
|
|
|
|
choices: [ present, absent ]
|
|
|
|
description:
|
2013-09-24 13:46:24 +00:00
|
|
|
- Whether the entries should be present or not in C(/etc/hosts).
|
2013-09-06 11:23:53 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Example host command from Ansible Playbooks
|
|
|
|
- host: ip=127.0.0.1 hostname=localhost state=present
|
2013-09-23 21:05:43 +00:00
|
|
|
- host: ip=127.0.0.1 hostname=localhost aliases=foobar.com,localhost.foobar.com
|
2013-09-06 11:23:53 +00:00
|
|
|
- host: ip=192.168.1.1 state=absent
|
2013-09-24 07:15:16 +00:00
|
|
|
- host: hostname=www.example.com state=absent
|
2013-09-23 21:05:43 +00:00
|
|
|
- host: ip=::1 hostname=localhost6 aliases=ip6-localhost,ip6-loopback
|
2013-09-06 11:23:53 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import fileinput
|
|
|
|
|
|
|
|
class Host(object):
|
|
|
|
|
|
|
|
HOSTSFILE = '/etc/hosts'
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.state = module.params['state']
|
|
|
|
self.ip = module.params['ip']
|
|
|
|
self.hostname = module.params['hostname']
|
|
|
|
self.aliases = module.params['aliases']
|
2013-09-20 20:50:03 +00:00
|
|
|
|
|
|
|
self._ip_matches = False
|
|
|
|
self._hostname_matches = False
|
|
|
|
self._aliases_matches = False
|
|
|
|
self._has_aliases = False
|
|
|
|
self._found_on_line = -1
|
2013-09-06 11:23:53 +00:00
|
|
|
|
|
|
|
def validate_has_hostname_on_present(self):
|
|
|
|
err = ''
|
|
|
|
if self.state == 'present' and not (self.hostname and self.ip):
|
|
|
|
err = "Error: No param 'hostnames' or 'ip' given in state 'present'."
|
|
|
|
return err
|
|
|
|
|
|
|
|
def validate_has_ip_or_hostname_on_absent(self):
|
|
|
|
err = ''
|
|
|
|
if self.state == 'absent':
|
|
|
|
if not (self.hostname or self.ip):
|
|
|
|
err = "Error: Either param 'hostnames' or 'ip' must be given in state 'absent'."
|
|
|
|
if self.hostname and self.ip:
|
|
|
|
err = "Error: Either param 'hostnames' or 'ip' must be given in state 'absent'."
|
|
|
|
return err
|
|
|
|
|
|
|
|
def proceed_hosts_entries(self):
|
|
|
|
|
|
|
|
f = open(self.HOSTSFILE,'rb')
|
2013-09-20 20:50:03 +00:00
|
|
|
self._hostsfile_lines = f.readlines()
|
2013-09-06 11:23:53 +00:00
|
|
|
f.close()
|
|
|
|
|
2013-09-20 20:50:03 +00:00
|
|
|
for lineno, line in enumerate(self._hostsfile_lines):
|
2013-09-06 11:23:53 +00:00
|
|
|
if line.startswith("#"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
ip = line.split()[0:1]
|
|
|
|
hostname = line.split()[1:2]
|
|
|
|
aliases = ','.join(line.split()[2:])
|
|
|
|
|
|
|
|
if self.ip and self.ip in ip:
|
2013-09-20 20:50:03 +00:00
|
|
|
self._ip_matches = True
|
|
|
|
self._found_on_line = lineno
|
2013-09-06 11:23:53 +00:00
|
|
|
|
|
|
|
if self.hostname and self.hostname in hostname:
|
2013-09-20 20:50:03 +00:00
|
|
|
self._hostname_matches = True
|
|
|
|
self._found_on_line = lineno
|
2013-09-06 11:23:53 +00:00
|
|
|
|
|
|
|
# only look at aliases if we found hostname or ip
|
2013-09-20 20:50:03 +00:00
|
|
|
if self._hostname_matches or self._ip_matches:
|
2013-09-06 11:23:53 +00:00
|
|
|
if aliases:
|
2013-09-20 20:50:03 +00:00
|
|
|
self._has_aliases = True
|
2013-09-06 11:23:53 +00:00
|
|
|
if self.aliases and self.aliases == aliases:
|
2013-09-20 20:50:03 +00:00
|
|
|
self._aliases_matches = True
|
2013-09-06 11:23:53 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
def full_entry_exists(self):
|
2013-10-18 23:51:42 +00:00
|
|
|
if self.aliases and not self._aliases_matches:
|
2013-09-06 11:23:53 +00:00
|
|
|
return False
|
2013-09-23 21:05:43 +00:00
|
|
|
return self._ip_matches and self._hostname_matches
|
2013-09-06 11:23:53 +00:00
|
|
|
|
|
|
|
def entry_exists(self):
|
2013-09-23 21:05:43 +00:00
|
|
|
return self._ip_matches or self._hostname_matches
|
2013-09-06 11:23:53 +00:00
|
|
|
|
|
|
|
def remove_entry(self):
|
2013-09-20 20:50:03 +00:00
|
|
|
self._hostsfile_lines.pop(self._found_on_line)
|
2013-09-06 11:23:53 +00:00
|
|
|
|
|
|
|
def add_entry(self):
|
|
|
|
aliases = ''
|
|
|
|
if self.aliases:
|
|
|
|
aliases = self.aliases.replace(',',' ')
|
2013-09-23 21:37:42 +00:00
|
|
|
host_entry = self.ip + " " + self.hostname + " " + aliases + "\n"
|
2013-09-06 11:23:53 +00:00
|
|
|
if self.entry_exists():
|
2013-09-20 20:50:03 +00:00
|
|
|
self._hostsfile_lines[self._found_on_line] = host_entry
|
2013-09-06 11:23:53 +00:00
|
|
|
else:
|
2013-09-20 20:50:03 +00:00
|
|
|
self._hostsfile_lines.extend(host_entry)
|
2013-09-06 11:23:53 +00:00
|
|
|
|
|
|
|
def write_changes(self):
|
|
|
|
tmpfd, tmpfile = tempfile.mkstemp()
|
|
|
|
f = os.fdopen(tmpfd,'wb')
|
2013-09-20 20:50:03 +00:00
|
|
|
f.writelines(self._hostsfile_lines)
|
2013-09-06 11:23:53 +00:00
|
|
|
f.close()
|
|
|
|
self.module.atomic_move(tmpfile, self.HOSTSFILE)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
state=dict(default='present', choices=['present', 'absent'], type='str'),
|
|
|
|
ip=dict(default=None, type='str'),
|
|
|
|
hostname=dict(default=None, type='str'),
|
|
|
|
aliases=dict(default=None, type='str'),
|
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
host = Host(module)
|
|
|
|
result['state'] = host.state
|
2013-09-23 21:05:43 +00:00
|
|
|
result['changed'] = False
|
2013-09-06 11:23:53 +00:00
|
|
|
|
2013-11-15 12:38:32 +00:00
|
|
|
result['msg'] = host.validate_has_hostname_on_present()
|
|
|
|
if result['msg']:
|
|
|
|
module.fail_json(**result)
|
2013-09-06 11:23:53 +00:00
|
|
|
|
2013-11-15 12:38:32 +00:00
|
|
|
result['msg'] = host.validate_has_ip_or_hostname_on_absent()
|
|
|
|
if result['msg']:
|
|
|
|
module.fail_json(**result)
|
2013-09-06 11:23:53 +00:00
|
|
|
|
|
|
|
host.proceed_hosts_entries()
|
|
|
|
if host.state == 'present':
|
|
|
|
if not host.full_entry_exists():
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
host.add_entry()
|
|
|
|
host.write_changes()
|
2013-09-20 20:50:03 +00:00
|
|
|
result['changed'] = True
|
2013-09-06 11:23:53 +00:00
|
|
|
|
|
|
|
elif host.state == 'absent':
|
|
|
|
if host.entry_exists():
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
host.remove_entry()
|
|
|
|
host.write_changes()
|
2013-09-20 20:50:03 +00:00
|
|
|
result['changed'] = True
|
2013-09-06 11:23:53 +00:00
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
# include magic from lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|