mirror of https://github.com/ansible/ansible.git
Correctly write SELinux config file (#31251)
* Add new lines to end of config file lines * Properly write out selinux config file Change module behavior to not always report a change but warn if a reboot is needed and return reboot_required. Improve the output messages. Add strip parameter to get_file_lines utility to help with parsing the selinux config file. * Add return documentation * Add integration tests for selinux module * Use consistent capitalization for SELinux * Use atomic_move in selinux module * Don't copy the config file initially There's no need to make a copy just for reading. * Put message after set_config_policy in case the change fails * Add aliases to selinux testspull/31242/head^2
parent
aa4b3f14c5
commit
00df1fda10
@ -0,0 +1,2 @@
|
|||||||
|
needs/root
|
||||||
|
posix/ci/group2
|
@ -0,0 +1,30 @@
|
|||||||
|
# (c) 2017, Sam Doran <sdoran@redhat.com>
|
||||||
|
|
||||||
|
# This file is part of Ansible
|
||||||
|
#
|
||||||
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
msg: SELinux is disabled
|
||||||
|
when: ansible_selinux is defined and ansible_selinux == False
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
msg: SELinux is {{ ansible_selinux.status }}
|
||||||
|
when: ansible_selinux is defined and ansible_selinux != False
|
||||||
|
|
||||||
|
- include: selinux.yml
|
||||||
|
when:
|
||||||
|
- ansible_selinux is defined
|
||||||
|
- ansible_selinux != False
|
||||||
|
- ansible_selinux.status == 'enabled'
|
@ -0,0 +1,170 @@
|
|||||||
|
# (c) 2017, Sam Doran <sdoran@redhat.com>
|
||||||
|
|
||||||
|
# This file is part of Ansible
|
||||||
|
#
|
||||||
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
# First Test
|
||||||
|
# ##############################################################################
|
||||||
|
# Test changing the state, which requires a reboot
|
||||||
|
|
||||||
|
- name: TEST 1 | Get current SELinux config file contents
|
||||||
|
set_fact:
|
||||||
|
selinux_config_original: "{{ lookup('file', '/etc/sysconfig/selinux').split('\n') }}"
|
||||||
|
before_test_sestatus: "{{ ansible_selinux }}"
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: "{{ item }}"
|
||||||
|
verbosity: 1
|
||||||
|
with_items:
|
||||||
|
- selinux_config_original
|
||||||
|
- before_test_sestatus
|
||||||
|
- ansible_selinux
|
||||||
|
|
||||||
|
- name: TEST 1 | Setup SELinux configuration for tests
|
||||||
|
selinux:
|
||||||
|
state: enforcing
|
||||||
|
policy: targeted
|
||||||
|
|
||||||
|
- name: TEST 1 | Disable SELinux
|
||||||
|
selinux:
|
||||||
|
state: disabled
|
||||||
|
policy: targeted
|
||||||
|
register: _disable_test1
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: _disable_test1
|
||||||
|
verbosity: 1
|
||||||
|
|
||||||
|
- name: TEST 1 | Re-gather facts
|
||||||
|
setup:
|
||||||
|
|
||||||
|
- name: TEST 1 | Assert that status was changed, reboot_required is True, a warning was displayed, and SELinux is configured properly
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- _disable_test1 | changed
|
||||||
|
- _disable_test1.reboot_required
|
||||||
|
- (_disable_test1.warnings | length ) >= 1
|
||||||
|
- ansible_selinux.config_mode == 'disabled'
|
||||||
|
- ansible_selinux.type == 'targeted'
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: ansible_selinux
|
||||||
|
verbosity: 1
|
||||||
|
|
||||||
|
- name: TEST 1 | Disable SELinux again
|
||||||
|
selinux:
|
||||||
|
state: disabled
|
||||||
|
policy: targeted
|
||||||
|
register: _disable_test2
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: _disable_test2
|
||||||
|
verbosity: 1
|
||||||
|
|
||||||
|
- name: TEST 1 | Assert that no change is reported, a warnking was dispalyed, and reboot_required is True
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- not _disable_test2 | changed
|
||||||
|
- (_disable_test1.warnings | length ) >= 1
|
||||||
|
- _disable_test2.reboot_required
|
||||||
|
|
||||||
|
- name: TEST 1 | Get modified config file
|
||||||
|
set_fact:
|
||||||
|
selinux_config_after: "{{ lookup('file', '/etc/sysconfig/selinux').split('\n') }}"
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: selinux_config_after
|
||||||
|
verbosity: 1
|
||||||
|
|
||||||
|
- name: TEST 1 | Ensure SELinux config file is properly formatted
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- selinux_config_original | length == selinux_config_after | length
|
||||||
|
- selinux_config_after[selinux_config_after.index('SELINUX=disabled')] | search("^SELINUX=\w+$")
|
||||||
|
- selinux_config_after[selinux_config_after.index('SELINUXTYPE=targeted')] | search("^SELINUXTYPE=\w+$")
|
||||||
|
|
||||||
|
- name: TEST 1 | Reset SELinux configuration for next test
|
||||||
|
selinux:
|
||||||
|
state: enforcing
|
||||||
|
policy: targeted
|
||||||
|
|
||||||
|
|
||||||
|
# Second Test
|
||||||
|
# ##############################################################################
|
||||||
|
# Test changing only the policy, which does not require a reboot
|
||||||
|
|
||||||
|
- name: TEST 2 | Set SELinux policy
|
||||||
|
selinux:
|
||||||
|
state: enforcing
|
||||||
|
policy: mls
|
||||||
|
register: _state_test1
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: _state_test1
|
||||||
|
verbosity: 1
|
||||||
|
|
||||||
|
- name: TEST 2 | Re-gather facts
|
||||||
|
setup:
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: ansible_selinux
|
||||||
|
tags: debug
|
||||||
|
|
||||||
|
- name: TEST 2 | Assert that status was changed, reboot_required is False, no warnings were displayed, and SELinux is configured properly
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- _state_test1 | changed
|
||||||
|
- not _state_test1.reboot_required
|
||||||
|
- _state_test1.warnings is not defined
|
||||||
|
- ansible_selinux.config_mode == 'enforcing'
|
||||||
|
- ansible_selinux.type == 'mls'
|
||||||
|
|
||||||
|
- name: TEST 2 | Set SELinux policy again
|
||||||
|
selinux:
|
||||||
|
state: enforcing
|
||||||
|
policy: mls
|
||||||
|
register: _state_test2
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: _state_test2
|
||||||
|
verbosity: 1
|
||||||
|
|
||||||
|
- name: TEST 2 | Assert that no change was reported, no warnings were dispalyed, and reboot_required is False
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- not _state_test2 | changed
|
||||||
|
- _state_test2.warnings is not defined
|
||||||
|
- not _state_test2.reboot_required
|
||||||
|
|
||||||
|
- name: TEST 2 | Get modified config file
|
||||||
|
set_fact:
|
||||||
|
selinux_config_after: "{{ lookup('file', '/etc/sysconfig/selinux').split('\n') }}"
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: selinux_config_after
|
||||||
|
verbosity: 1
|
||||||
|
|
||||||
|
- name: TEST 2 | Ensure SELinux config file is properly formatted
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- selinux_config_original | length == selinux_config_after | length
|
||||||
|
- selinux_config_after[selinux_config_after.index('SELINUX=enforcing')] | search("^SELINUX=\w+$")
|
||||||
|
- selinux_config_after[selinux_config_after.index('SELINUXTYPE=mls')] | search("^SELINUXTYPE=\w+$")
|
||||||
|
|
||||||
|
- name: TEST 2 | Reset SELinux configuration for next test
|
||||||
|
selinux:
|
||||||
|
state: enforcing
|
||||||
|
policy: targeted
|
Loading…
Reference in New Issue