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

make tuned.conf have correct ansible_managed comment #72

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 32 additions & 3 deletions library/kernel_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@
the given settings to be the current and only settings
type: bool
default: false
ansible_managed_new:
description:
- Ansible ansible_managed string to put in header of file
- should be in the format of {{ ansible_managed | comment }}
- as rendered by the template module
type: str
required: true
ansible_managed_current:
description:
- the current config file header to be compared and replaced
type: str
required: true

author:
- Rich Megginson (@richm)
Expand Down Expand Up @@ -486,13 +498,17 @@ def apply_params_to_profile(params, current_profile, purge):
return changestatus, reboot_required


def write_profile(current_profile):
def write_profile(current_profile, ansible_managed_new):
"""write the profile to the profile file"""
# convert profile to configobj to write ini-style file
# profile.options go into [main] section
# profile.units go into [unitname] section
cfg = configobj.ConfigObj(indent_type="")
cfg.initial_comment = ["File managed by Ansible - DO NOT EDIT"]
# ansible_managed_new should be in the format of
# ansible_managed_new passed through the comment filter
# as rendered by the ansible "template" module - strip the
# trailing newline, if any
cfg.initial_comment = ansible_managed_new.strip().split("\n")
cfg["main"] = current_profile.options
for unitname, unit in current_profile.units.items():
cfg[unitname] = unit.options
Expand Down Expand Up @@ -794,6 +810,14 @@ def run_module():
# use raw here - type can be dict or list - perform validation
# below
module_args[plugin_name] = dict(type="raw", required=False)
module_args["ansible_managed_new"] = dict(
type="str",
required=True,
)
module_args["ansible_managed_current"] = dict(
type="str",
required=True,
)

result = dict(changed=False, message="")

Expand All @@ -808,6 +832,8 @@ def run_module():
# remove any non-tuned fields from params and save them locally
# state = params.pop("state")
purge = params.pop("purge", False)
ansible_managed_new = params.pop("ansible_managed_new")
ansible_managed_current = params.pop("ansible_managed_current")
# also remove any empty or None
# pylint: disable=blacklisted-name
_ = remove_if_empty(params)
Expand Down Expand Up @@ -851,9 +877,12 @@ def run_module():
if changestatus == NOCHANGES:
changestatus = CHANGES
result["msg"] = "Updated active profile and/or mode."
if changestatus == NOCHANGES and ansible_managed_new != ansible_managed_current:
changestatus = CHANGES
result["msg"] = "Updated ansible_managed."
if changestatus > NOCHANGES:
try:
write_profile(current_profile)
write_profile(current_profile, ansible_managed_new)
# notify tuned to reload/reapply profile
except TunedException as tex:
module.debug("caught TunedException [{0}]".format(tex))
Expand Down
15 changes: 15 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
force: no
mode: 0644

- name: Get current config
slurp:
src: "{{ __kernel_settings_profile_filename }}"
register: __kernel_settings_profile_contents_b64
changed_when: false

- name: Apply kernel settings
kernel_settings:
sysctl: "{{ kernel_settings_sysctl if kernel_settings_sysctl else omit }}"
Expand Down Expand Up @@ -76,6 +82,8 @@
value: "{{ kernel_settings_bootloader_cmdline |
d([]) }}"
purge: "{{ kernel_settings_purge }}"
ansible_managed_new: "{{ __kernel_settings_new_header }}"
ansible_managed_current: "{{ __kernel_settings_cur_header }}"
notify: __kernel_settings_handler_modified
register: __kernel_settings_register_module
when:
Expand All @@ -85,6 +93,13 @@
or kernel_settings_transparent_hugepages_defrag
or kernel_settings_bootloader_cmdline | d({})
or kernel_settings_purge
or __kernel_settings_cur_header != __kernel_settings_new_header
vars:
__kernel_settings_cur_header: "{{
__kernel_settings_profile_contents_b64.content | b64decode |
regex_replace('(?sm)^\\[.*$', '') }}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are there two backslashes in '(?sm)^\\[.*$'?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's apparently how you have to escape the [ so it won't be interpreted as a character set in this context

Copy link
Collaborator

Choose a reason for hiding this comment

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

must be to escape the second backslash, Ansible won't read it otherwise

__kernel_settings_new_header: "{{
lookup('template', 'get_ansible_managed.j2') }}"

- name: tuned apply settings
command: >
Expand Down
1 change: 1 addition & 0 deletions templates/get_ansible_managed.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ ansible_managed | comment }}
4 changes: 4 additions & 0 deletions tests/tests_simple_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@
when:
- kernel_settings_reboot_required | d(false)
- not kernel_settings_test_reboot_ok | d(false)

- name: show contents of tuned.conf
command: cat /etc/tuned/kernel_settings/tuned.conf
changed_when: false
9 changes: 7 additions & 2 deletions tests/unit/modules/test_kernel_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,17 @@ def test_write_profile(self):
)
self.assertEqual(kernel_settings.CHANGES, changestatus)
self.assertTrue(reboot_required)
kernel_settings.write_profile(current_profile)
kernel_settings.write_profile(
current_profile,
"# one\n# two\n# three\n",
)
fname = os.path.join(
tuned.consts.LOAD_DIRECTORIES[-1], "kernel_settings", "tuned.conf"
)
expected_lines = [
"# File managed by Ansible - DO NOT EDIT",
"# one",
"# two",
"# three",
"[main]",
"summary = kernel settings",
"[sysctl]",
Expand Down