Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lineinfile atomic #2845

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ansible/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ def on_task_start(self, name, is_conditional):
else:
self.skip_task = True
else:
self.skip_task = False
display(banner(msg))

call_callback_module('playbook_on_task_start', name, is_conditional)
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def _executor_internal_inner(self, host, module_name, module_args, inject, port,
actual_port = delegate_info.get('ansible_ssh_port', port)
actual_user = delegate_info.get('ansible_ssh_user', actual_user)
actual_pass = delegate_info.get('ansible_ssh_pass', actual_pass)
actual_private_key_file = delegate_info.get('private_key_file', self.private_key_file)
actual_private_key_file = delegate_info.get('ansible_ssh_private_key_file', self.private_key_file)
actual_transport = delegate_info.get('ansible_connection', self.transport)
for i in delegate_info:
if i.startswith("ansible_") and i.endswith("_interpreter"):
Expand Down
29 changes: 17 additions & 12 deletions library/files/lineinfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import re
import os
import tempfile

DOCUMENTATION = """
---
Expand Down Expand Up @@ -128,6 +129,14 @@ EXAMPLES = r"""
lineinfile: dest=/opt/jboss-as/bin/standalone.conf regexp='^(.*)Xms(\d+)m(.*)$' line='\\1Xms${xms}m\\3' backrefs=yes
"""

def write_changes(module,lines,dest):

tmpfd, tmpfile = tempfile.mkstemp()
f = os.fdopen(tmpfd,'wb')
f.writelines(lines)
f.close()

module.atomic_move(tmpfile, dest)

def check_file_attrs(module, changed, message):

Expand All @@ -145,9 +154,7 @@ def check_file_attrs(module, changed, message):
def present(module, dest, regexp, line, insertafter, insertbefore, create,
backup, backrefs):

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 os.path.exists(dest):
if not create:
module.fail_json(rc=257, msg='Destination %s does not exist !' % dest)
destpath = os.path.dirname(dest)
Expand Down Expand Up @@ -229,19 +236,15 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
if changed and not module.check_mode:
if backup and os.path.exists(dest):
module.backup_local(dest)
f = open(dest, 'wb')
f.writelines(lines)
f.close()
write_changes(module, lines, dest)

msg, changed = check_file_attrs(module, changed, msg)
module.exit_json(changed=changed, msg=msg)


def absent(module, dest, regexp, 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 os.path.exists(dest):
module.exit_json(changed=False, msg="file not present")

msg = ""
Expand All @@ -264,9 +267,7 @@ def absent(module, dest, regexp, backup):
if changed and not module.check_mode:
if backup:
module.backup_local(dest)
f = open(dest, 'wb')
f.writelines(lines)
f.close()
write_changes(module, lines, dest)

if changed:
msg = "%s line(s) removed" % len(found)
Expand Down Expand Up @@ -299,6 +300,10 @@ def main():
backrefs = module.params['backrefs']
dest = os.path.expanduser(params['dest'])


if os.path.isdir(dest):
module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)

if params['state'] == 'present':
if 'line' not in params:
module.fail_json(msg='line= is required with state=present')
Expand Down