Add option create= to lineinfile module

We use the lineinfile module to modify configuration files of a proprietary application. This application reads configuration options from files, but does not require those files to exist (if the default options are fine). However this application may modify the configuration file at will, so we cannot copy or template those files. And after a silent install the configuration may not exist (depending on the response file).

Whatever the case, during deployment we need to make sure some configuration options are set after the installation.

So the cleanest way to handle this situation is to allow the lineinfile module to create the file if it is missing (and this is the expected behavior). When I proposed this behavior, @sergevanginderachter needed the same functionality and was now working around it as well.
This commit is contained in:
Dag Wieers 2012-10-30 16:57:45 +01:00
parent 9a12873166
commit 1e3dcfce4b

View file

@ -65,6 +65,14 @@ options:
file, and C(EOF) for inserting the line at the end of the file. file, and C(EOF) for inserting the line at the end of the file.
choices: [ BOF, EOF ] choices: [ BOF, EOF ]
default: EOF default: EOF
create:
required: false
choices: [ yes, no ]
default: no
description:
- Used with C(state=present). If specified, the file will be created
if it does not already exist. By default it will fail if the file
is missing.
backup: backup:
required: false required: false
default: no default: no
@ -76,7 +84,18 @@ examples:
- code: 'lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel"' - code: 'lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel"'
''' '''
def present(module, dest, regexp, line, insertafter, backup): def present(module, dest, regexp, line, insertafter, create, backup):
if os.path.isdir(dest):
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
elif not os.path.exists(dest):
if not create:
module.fail_json(rc=257, msg='Destination %s does not exist !' % dest)
destpath = os.path.dirname(dest)
if not os.path.exists(destpath):
os.makedirs(destpath)
lines = []
else:
f = open(dest, 'rb') f = open(dest, 'rb')
lines = f.readlines() lines = f.readlines()
f.close() f.close()
@ -162,18 +181,20 @@ def main():
regexp=dict(required=True), regexp=dict(required=True),
line=dict(aliases=['value']), line=dict(aliases=['value']),
insertafter=dict(default='EOF'), insertafter=dict(default='EOF'),
create=dict(default=false, choices=BOOLEANS),
backup=dict(default=False, choices=BOOLEANS), backup=dict(default=False, choices=BOOLEANS),
), ),
) )
params = module.params params = module.params
create = module.boolean(module.params.get('create', False))
backup = module.boolean(module.params.get('backup', False)) backup = module.boolean(module.params.get('backup', False))
if params['state'] == 'present': if params['state'] == 'present':
if 'line' not in params: if 'line' not in params:
module.fail_json(msg='line= is required with state=present') module.fail_json(msg='line= is required with state=present')
present(module, params['dest'], params['regexp'], params['line'], present(module, params['dest'], params['regexp'], params['line'],
params['insertafter'], backup) params['insertafter'], create, backup)
else: else:
absent(module, params['dest'], params['regexp'], backup) absent(module, params['dest'], params['regexp'], backup)