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

ini_file: Don't creates new file instead of following symlink #6546

Merged
merged 3 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/ini_file-preserve-symlink.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- "ini_file - Follow the symlinks instead of replacing them (https://github.com/ansible-collections/community.general/issues/6470)."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- "ini_file - Follow the symlinks instead of replacing them (https://github.com/ansible-collections/community.general/issues/6470)."
- "ini_file - follow the symlinks instead of replacing them (https://github.com/ansible-collections/community.general/issues/6470, https://github.com/ansible-collections/community.general/pull/6546)."

19 changes: 12 additions & 7 deletions plugins/modules/ini_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,20 @@ def do_ini(module, filename, section=None, option=None, values=None,
after_header='%s (content)' % filename,
)

if not os.path.exists(filename):
if os.path.islink(filename):
target_filename = os.path.realpath(filename)
else:
target_filename = filename

if not os.path.exists(target_filename):
if not create:
module.fail_json(rc=257, msg='Destination %s does not exist!' % filename)
destpath = os.path.dirname(filename)
module.fail_json(rc=257, msg='Destination %s does not exist!' % target_filename)
destpath = os.path.dirname(target_filename)
if not os.path.exists(destpath) and not module.check_mode:
os.makedirs(destpath)
ini_lines = []
else:
with io.open(filename, 'r', encoding="utf-8-sig") as ini_file:
with io.open(target_filename, 'r', encoding="utf-8-sig") as ini_file:
ini_lines = [to_text(line) for line in ini_file.readlines()]

if module._diff:
Expand Down Expand Up @@ -404,7 +409,7 @@ def do_ini(module, filename, section=None, option=None, values=None,
backup_file = None
if changed and not module.check_mode:
if backup:
backup_file = module.backup_local(filename)
backup_file = module.backup_local(target_filename)

encoded_ini_lines = [to_bytes(line) for line in ini_lines]
try:
Expand All @@ -416,10 +421,10 @@ def do_ini(module, filename, section=None, option=None, values=None,
module.fail_json(msg="Unable to create temporary file %s", traceback=traceback.format_exc())

try:
module.atomic_move(tmpfile, filename)
module.atomic_move(tmpfile, target_filename)
except IOError:
module.ansible.fail_json(msg='Unable to move temporary \
file %s to %s, IOError' % (tmpfile, filename), traceback=traceback.format_exc())
file %s to %s, IOError' % (tmpfile, target_filename), traceback=traceback.format_exc())

return (changed, backup_file, diff, msg)

Expand Down
46 changes: 46 additions & 0 deletions tests/integration/targets/ini_file/tasks/tests/04-symlink.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- name: Create a file
ansible.builtin.copy:
content: |
[main]
foo=BAR
dest: "{{ remote_tmp_dir }}/my_original_file.ini"
- name: Create a symbolic link
ansible.builtin.file:
src: "{{ remote_tmp_dir }}/my_original_file.ini"
dest: "{{ remote_tmp_dir }}/symlink.ini"
state: link

- name: Set a value
community.general.ini_file:
path: "{{ remote_tmp_dir }}/symlink.ini"
section: main
option: proxy
value: 'http://proxy.myorg.org:3128'
register: result
- ansible.builtin.assert:
that:
- result is changed
- name: Set a value (idempotency)
community.general.ini_file:
path: "{{ remote_tmp_dir }}/symlink.ini"
section: main
option: proxy
value: 'http://proxy.myorg.org:3128'
register: result
- ansible.builtin.assert:
that:
- "not (result is changed)"
- name: Set a value (on the final file)
community.general.ini_file:
path: "{{ remote_tmp_dir }}/my_original_file.ini"
section: main
option: proxy
value: 'http://proxy.myorg.org:3128'
register: result
- ansible.builtin.assert:
that:
- "not (result is changed)"