forked from gc3-uzh-ch/ansible-playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7553317
commit 24c9549
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
- hosts: all | ||
vars: | ||
nfslients: | ||
- 10.1.1.1 | ||
- 10.1.1.2 | ||
- 10.1.1.3 | ||
tasks: | ||
- action: nfsexport path=/home | ||
- action: nfsexport path=/srv clients=$nfsclients options=rw,no_root_squash | ||
|
||
# Of course, if you have defined a group you can use the hostnames on | ||
# that group as clients. | ||
|
||
- hosts: all | ||
tasks: | ||
- action: nfsexport path=/home clients=${groups.myclients} | ||
|
||
# Unfortunately, however, there is not (yet) an easy way to convert | ||
# the hostnames into ip addresses. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*-# | ||
# @(#)nfsexport | ||
# | ||
# | ||
# Copyright (C) 2013, GC3, University of Zurich. All rights reserved. | ||
# | ||
# | ||
# This program 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 2 of the License, or (at your | ||
# option) any later version. | ||
# | ||
# This program 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 this program; if not, write to the Free Software Foundation, Inc., | ||
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
|
||
__docformat__ = 'reStructuredText' | ||
|
||
|
||
DOCUMENTATION = """ | ||
--- | ||
module: nfsexport | ||
short_description: This module will configure a nfs exports | ||
description: | ||
- This module will configure an export by writing an entry in the ``/etc/exports`` file. | ||
author: Antonio Messina <[email protected]> | ||
options: | ||
path: | ||
description: | ||
- Directory to export | ||
required: true | ||
dest: | ||
description: | ||
- export file to write. | ||
required: false | ||
default: /etc/exports | ||
clients: | ||
description: | ||
- Address o list of addresses of the nfs clients | ||
required: false | ||
default: '*', which means `all` | ||
options: | ||
description: | ||
- nfs export options. | ||
required: false | ||
default: ro,root_squash | ||
""" | ||
EXAMPLES = """ | ||
- code: nfsexport path=/home clients=10.0.0.1 options=rw | ||
""" | ||
import os | ||
|
||
def main(): | ||
module = AnsibleModule( | ||
argument_spec = dict( | ||
path = dict(required=True), | ||
dest = dict(required=False, default='/etc/exports'), | ||
clients = dict(required=False, default='*'), | ||
options = dict(required=False, default='ro,root_squash'), | ||
) | ||
) | ||
|
||
params = module.params | ||
params['path'] = params['path'].strip() | ||
|
||
client_list = params['clients'].split(',') | ||
exports = [] | ||
for client in client_list: | ||
exports.append("%s(%s)" % (client, params['options'])) | ||
clients = str.join(',', exports) | ||
exportline = "%s %s\n" % (params['path'], clients ) | ||
|
||
# Check if destination file exists already | ||
if os.path.exists(params['dest']): | ||
current_exports = open(params['dest'], 'r').readlines() | ||
# Check if the parameters match an export which is already | ||
# defined | ||
for i, line in enumerate(current_exports): | ||
path = line.split()[0].strip() | ||
if path == params['path']: | ||
if line.split(' ', 1)[1].strip() == clients: | ||
# It matches, but it's not changed. | ||
module.exit_json(changed=False) | ||
else: | ||
# It matches, and it's changed. Update the file! | ||
current_exports[i] = exportline | ||
fd = open(params['dest'], 'w') | ||
fd.writelines(current_exports) | ||
fd.close() | ||
module.exit_json(changed=True) | ||
|
||
# Export path not present, proceed and add one. | ||
fd = open(params['dest'], 'a') | ||
fd.write(exportline) | ||
fd.close() | ||
module.exit_json(changed=True) | ||
|
||
# fd = open(params['dest'], 'w') | ||
# fd.write(TEMPLATE) | ||
# fd.close() | ||
# module.exit_json(changed=True) | ||
|
||
# include magic from lib/ansible/module_common.py | ||
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> | ||
main() |