Skip to content

Commit

Permalink
ansible: fix write-ssh-config modules (nodejs#3824)
Browse files Browse the repository at this point in the history
- Use `/usr/bin/python` in the shebang to allow `ansible_python_interpreter` to work.
- Compare the template section before and after the change to avoid unnecessary
  changes and to have a correct report in the playbook output.

Closes: nodejs#3821
  • Loading branch information
targos authored Jul 14, 2024
1 parent d97e82f commit 2bdc315
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ansible/plugins/library/remmina_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright Node.js contributors. All rights reserved.
Expand Down
20 changes: 15 additions & 5 deletions ansible/plugins/library/ssh_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright Node.js contributors. All rights reserved.
Expand Down Expand Up @@ -65,8 +65,12 @@ def multi_replace(content, to_replace):
return content


def is_templatable(path, config):
return os.path.exists(path) and bool(re.search(match, config))
def get_template_section(config):
return match.search(config).group(1)


def is_templatable(config):
return bool(match.search(config))


def render_template(hosts):
Expand Down Expand Up @@ -100,12 +104,18 @@ def main():
module.fail_json(msg='Couldn\'t find a ssh config at %s' %
path)

if not is_templatable(path, contents):
if not is_templatable(contents):
module.fail_json(msg='Your ssh config lacks template stubs. Check README.md for instructions.')

before_value = get_template_section(contents)
after_value = render_template(module.params['hostinfo'])

if before_value == after_value:
module.exit_json(changed=False, meta='ssh config is up-to-date')

rendered = '{}{}{}'.format(
pre_match,
render_template(module.params['hostinfo']),
after_value,
post_match
)

Expand Down

0 comments on commit 2bdc315

Please sign in to comment.