mirror of
https://github.com/ansible-collections/ansible.posix.git
synced 2026-01-11 15:15:26 +01:00
Compare commits
11 commits
cc36f525d0
...
4474b90eca
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4474b90eca | ||
|
|
c273ac2a01 | ||
|
|
f978998521 | ||
|
|
c319c856ed | ||
|
|
6280bb8ec8 | ||
|
|
3b79155e68 | ||
|
|
05724a097b | ||
|
|
7e1b76c46e | ||
|
|
505a4aaa09 | ||
|
|
d70d2aaaa7 | ||
|
|
806ff5c1a3 |
5 changed files with 97 additions and 16 deletions
2
changelogs/fragments/650-profile_tasks_roles.yml
Normal file
2
changelogs/fragments/650-profile_tasks_roles.yml
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- "profile_tasks and profile_roles callback plugins - avoid deleted/deprecated callback functions, instead use modern interface that was introduced a longer time ago (https://github.com/ansible-collections/ansible.posix/issues/650)."
|
||||||
|
|
@ -124,10 +124,7 @@ class CallbackModule(CallbackBase):
|
||||||
def v2_playbook_on_handler_task_start(self, task):
|
def v2_playbook_on_handler_task_start(self, task):
|
||||||
self._record_task(task)
|
self._record_task(task)
|
||||||
|
|
||||||
def playbook_on_setup(self):
|
def v2_playbook_on_stats(self, stats):
|
||||||
self._display_tasktime()
|
|
||||||
|
|
||||||
def playbook_on_stats(self, stats):
|
|
||||||
# Align summary report header with other callback plugin summary
|
# Align summary report header with other callback plugin summary
|
||||||
self._display.banner("ROLES RECAP")
|
self._display.banner("ROLES RECAP")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -209,10 +209,7 @@ class CallbackModule(CallbackBase):
|
||||||
def v2_playbook_on_handler_task_start(self, task):
|
def v2_playbook_on_handler_task_start(self, task):
|
||||||
self._record_task(task)
|
self._record_task(task)
|
||||||
|
|
||||||
def playbook_on_setup(self):
|
def v2_playbook_on_stats(self, stats):
|
||||||
self._display_tasktime()
|
|
||||||
|
|
||||||
def playbook_on_stats(self, stats):
|
|
||||||
# Align summary report header with other callback plugin summary
|
# Align summary report header with other callback plugin summary
|
||||||
self._display.banner("TASKS RECAP")
|
self._display.banner("TASKS RECAP")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,7 @@ import os
|
||||||
import platform
|
import platform
|
||||||
import re
|
import re
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import glob
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.six import string_types
|
from ansible.module_utils.six import string_types
|
||||||
|
|
@ -121,12 +122,24 @@ class SysctlModule(object):
|
||||||
# success or failure.
|
# success or failure.
|
||||||
LANG_ENV = {'LANG': 'C', 'LC_ALL': 'C', 'LC_MESSAGES': 'C'}
|
LANG_ENV = {'LANG': 'C', 'LC_ALL': 'C', 'LC_MESSAGES': 'C'}
|
||||||
|
|
||||||
|
# We define a variable to keep all the directories to be read, equivalent to
|
||||||
|
# (/sbin/sysctl --system) option
|
||||||
|
SYSCTL_DIRS = [
|
||||||
|
'/etc/sysctl.d/*.conf',
|
||||||
|
'/run/sysctl.d/*.conf',
|
||||||
|
'/usr/local/lib/sysctl.d/*.conf',
|
||||||
|
'/usr/lib/sysctl.d/*.conf',
|
||||||
|
'/lib/sysctl.d/*.conf',
|
||||||
|
'/etc/sysctl.conf'
|
||||||
|
]
|
||||||
|
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
self.module = module
|
self.module = module
|
||||||
self.args = self.module.params
|
self.args = self.module.params
|
||||||
|
|
||||||
self.sysctl_cmd = self.module.get_bin_path('sysctl', required=True)
|
self.sysctl_cmd = self.module.get_bin_path('sysctl', required=True)
|
||||||
self.sysctl_file = self.args['sysctl_file']
|
self.sysctl_file = self.args['sysctl_file']
|
||||||
|
self.system_Wide = self.args['system_Wide']
|
||||||
|
|
||||||
self.proc_value = None # current token value in proc fs
|
self.proc_value = None # current token value in proc fs
|
||||||
self.file_value = None # current token value in file
|
self.file_value = None # current token value in file
|
||||||
|
|
@ -306,15 +319,22 @@ class SysctlModule(object):
|
||||||
# https://github.com/ansible/ansible/issues/58158
|
# https://github.com/ansible/ansible/issues/58158
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
# system supports reloading via the -p flag to sysctl, so we'll use that
|
if self.system_Wide:
|
||||||
sysctl_args = [self.sysctl_cmd, '-p', self.sysctl_file]
|
for sysctl_file in self.SYSCTL_DIRS:
|
||||||
if self.args['ignoreerrors']:
|
for conf_file in glob.glob(sysctl_file):
|
||||||
sysctl_args.insert(1, '-e')
|
rc, out, err = self.module.run_command([self.sysctl_cmd, '-p', conf_file], environ_update=self.LANG_ENV)
|
||||||
|
if rc != 0 or self._stderr_failed(err):
|
||||||
|
self.module.fail_json(msg="Failed to reload sysctl: %s" % to_native(out) + to_native(err))
|
||||||
|
else:
|
||||||
|
# system supports reloading via the -p flag to sysctl, so we'll use that
|
||||||
|
sysctl_args = [self.sysctl_cmd, '-p', self.sysctl_file]
|
||||||
|
if self.args['ignoreerrors']:
|
||||||
|
sysctl_args.insert(1, '-e')
|
||||||
|
|
||||||
rc, out, err = self.module.run_command(sysctl_args, environ_update=self.LANG_ENV)
|
rc, out, err = self.module.run_command(sysctl_args, environ_update=self.LANG_ENV)
|
||||||
|
|
||||||
if rc != 0 or self._stderr_failed(err):
|
if rc != 0 or self._stderr_failed(err):
|
||||||
self.module.fail_json(msg="Failed to reload sysctl: %s" % to_native(out) + to_native(err))
|
self.module.fail_json(msg="Failed to reload sysctl: %s" % to_native(out) + to_native(err))
|
||||||
|
|
||||||
# ==============================================================
|
# ==============================================================
|
||||||
# SYSCTL FILE MANAGEMENT
|
# SYSCTL FILE MANAGEMENT
|
||||||
|
|
@ -401,7 +421,8 @@ def main():
|
||||||
reload=dict(default=True, type='bool'),
|
reload=dict(default=True, type='bool'),
|
||||||
sysctl_set=dict(default=False, type='bool'),
|
sysctl_set=dict(default=False, type='bool'),
|
||||||
ignoreerrors=dict(default=False, type='bool'),
|
ignoreerrors=dict(default=False, type='bool'),
|
||||||
sysctl_file=dict(default='/etc/sysctl.conf', type='path')
|
sysctl_file=dict(default='/etc/sysctl.conf', type='path'),
|
||||||
|
system_wide=dict(default=False, type='bool'), # system_wide parameter
|
||||||
),
|
),
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
required_if=[('state', 'present', ['value'])],
|
required_if=[('state', 'present', ['value'])],
|
||||||
|
|
|
||||||
|
|
@ -229,6 +229,40 @@
|
||||||
ansible.builtin.assert:
|
ansible.builtin.assert:
|
||||||
that:
|
that:
|
||||||
- sysctl_test4 is failed
|
- sysctl_test4 is failed
|
||||||
|
|
||||||
|
##
|
||||||
|
## sysctl --system
|
||||||
|
##
|
||||||
|
|
||||||
|
- name: Set vm.swappiness to 10 with --system option
|
||||||
|
ansible.posix.sysctl:
|
||||||
|
name: vm.swappiness
|
||||||
|
value: 10
|
||||||
|
state: present
|
||||||
|
reload: false
|
||||||
|
sysctl_set: true
|
||||||
|
system: true
|
||||||
|
register: sysctl_system_test1
|
||||||
|
|
||||||
|
- name: Check with sysctl command
|
||||||
|
ansible.builtin.command: sysctl vm.swappiness
|
||||||
|
changed_when: false
|
||||||
|
register: sysctl_check_system1
|
||||||
|
|
||||||
|
- name: Debug sysctl_system_test1 sysctl_check_system1
|
||||||
|
ansible.builtin.debug:
|
||||||
|
var: item
|
||||||
|
verbosity: 1
|
||||||
|
with_items:
|
||||||
|
- "{{ sysctl_system_test1 }}"
|
||||||
|
- "{{ sysctl_check_system1 }}"
|
||||||
|
|
||||||
|
- name: Validate results for --system option
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- sysctl_system_test1 is changed
|
||||||
|
- sysctl_check_system1.stdout_lines == ["vm.swappiness = 10"]
|
||||||
|
|
||||||
|
|
||||||
- name: Test on RHEL VMs
|
- name: Test on RHEL VMs
|
||||||
when:
|
when:
|
||||||
|
|
@ -366,3 +400,33 @@
|
||||||
that:
|
that:
|
||||||
- stat_result.stat.islnk is defined and stat_result.stat.islnk
|
- stat_result.stat.islnk is defined and stat_result.stat.islnk
|
||||||
- stat_result.stat.lnk_source == '/tmp/ansible_sysctl_test.conf'
|
- stat_result.stat.lnk_source == '/tmp/ansible_sysctl_test.conf'
|
||||||
|
|
||||||
|
# Test sysctl: --system
|
||||||
|
- name: Set vm.swappiness to 10 with --system option
|
||||||
|
ansible.posix.sysctl:
|
||||||
|
name: vm.swappiness
|
||||||
|
value: 10
|
||||||
|
state: present
|
||||||
|
reload: false
|
||||||
|
sysctl_set: true
|
||||||
|
system: true
|
||||||
|
register: sysctl_system_test1
|
||||||
|
|
||||||
|
- name: Check with sysctl command
|
||||||
|
ansible.builtin.command: sysctl vm.swappiness
|
||||||
|
changed_when: false
|
||||||
|
register: sysctl_check_system1
|
||||||
|
|
||||||
|
- name: Debug sysctl_system_test1 sysctl_check_system1
|
||||||
|
ansible.builtin.debug:
|
||||||
|
var: item
|
||||||
|
verbosity: 1
|
||||||
|
with_items:
|
||||||
|
- "{{ sysctl_system_test1 }}"
|
||||||
|
- "{{ sysctl_check_system1 }}"
|
||||||
|
|
||||||
|
- name: Validate results for --system option
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- sysctl_system_test1 is changed
|
||||||
|
- sysctl_check_system1.stdout_lines == ["vm.swappiness = 10"]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue