0706bb
diff --git a/lib/ansible/module_utils/facts/utils.py b/lib/ansible/module_utils/facts/utils.py
0706bb
index 2446ae6..728934c 100644
0706bb
--- a/lib/ansible/module_utils/facts/utils.py
0706bb
+++ b/lib/ansible/module_utils/facts/utils.py
0706bb
@@ -36,9 +36,9 @@ def get_file_content(path, default=None, strip=True):
0706bb
     return data
0706bb
 
0706bb
 
0706bb
-def get_file_lines(path):
0706bb
+def get_file_lines(path, strip=True):
0706bb
     '''get list of lines from file'''
0706bb
-    data = get_file_content(path)
0706bb
+    data = get_file_content(path, strip=strip)
0706bb
     if data:
0706bb
         ret = data.splitlines()
0706bb
     else:
0706bb
diff --git a/lib/ansible/modules/system/selinux.py b/lib/ansible/modules/system/selinux.py
0706bb
index 7cd4011..7d1cb8d 100644
0706bb
--- a/lib/ansible/modules/system/selinux.py
0706bb
+++ b/lib/ansible/modules/system/selinux.py
0706bb
@@ -8,9 +8,11 @@ from __future__ import absolute_import, division, print_function
0706bb
 __metaclass__ = type
0706bb
 
0706bb
 
0706bb
-ANSIBLE_METADATA = {'metadata_version': '1.1',
0706bb
-                    'status': ['stableinterface'],
0706bb
-                    'supported_by': 'core'}
0706bb
+ANSIBLE_METADATA = {
0706bb
+    'metadata_version': '1.1',
0706bb
+    'status': ['stableinterface'],
0706bb
+    'supported_by': 'core'
0706bb
+}
0706bb
 
0706bb
 
0706bb
 DOCUMENTATION = '''
0706bb
@@ -59,21 +61,51 @@ EXAMPLES = '''
0706bb
     state: disabled
0706bb
 '''
0706bb
 
0706bb
+RETURN = '''
0706bb
+msg:
0706bb
+    description: Messages that describe changes that were made
0706bb
+    returned: always
0706bb
+    type: string
0706bb
+    sample: Config SELinux state changed from 'disabled' to 'permissive'
0706bb
+configfile:
0706bb
+    description: Path to SELinux configuration file
0706bb
+    returned: always
0706bb
+    type: string
0706bb
+    sample: /etc/selinux/config
0706bb
+policy:
0706bb
+    description: Name of the SELinux policy
0706bb
+    returned: always
0706bb
+    type: string
0706bb
+    sample: targeted
0706bb
+state:
0706bb
+    description: SELinux mode
0706bb
+    returned: always
0706bb
+    type: string
0706bb
+    sample: enforcing
0706bb
+reboot_required:
0706bb
+    description: Whether or not an reboot is required for the changes to take effect
0706bb
+    returned: always
0706bb
+    type: bool
0706bb
+    sample: true
0706bb
+'''
0706bb
+
0706bb
 import os
0706bb
 import re
0706bb
+import tempfile
0706bb
 
0706bb
 try:
0706bb
     import selinux
0706bb
     HAS_SELINUX = True
0706bb
 except ImportError:
0706bb
     HAS_SELINUX = False
0706bb
+
0706bb
 from ansible.module_utils.basic import AnsibleModule
0706bb
 from ansible.module_utils.facts.utils import get_file_lines
0706bb
 
0706bb
 
0706bb
 # getter subroutines
0706bb
 def get_config_state(configfile):
0706bb
-    lines = get_file_lines(configfile)
0706bb
+    lines = get_file_lines(configfile, strip=False)
0706bb
 
0706bb
     for line in lines:
0706bb
         stateline = re.match(r'^SELINUX=.*$', line)
0706bb
@@ -82,7 +114,7 @@ def get_config_state(configfile):
0706bb
 
0706bb
 
0706bb
 def get_config_policy(configfile):
0706bb
-    lines = get_file_lines(configfile)
0706bb
+    lines = get_file_lines(configfile, strip=False)
0706bb
 
0706bb
     for line in lines:
0706bb
         stateline = re.match(r'^SELINUXTYPE=.*$', line)
0706bb
@@ -91,16 +123,19 @@ def get_config_policy(configfile):
0706bb
 
0706bb
 
0706bb
 # setter subroutines
0706bb
-def set_config_state(state, configfile):
0706bb
+def set_config_state(module, state, configfile):
0706bb
     # SELINUX=permissive
0706bb
     # edit config file with state value
0706bb
     stateline = 'SELINUX=%s' % state
0706bb
+    lines = get_file_lines(configfile, strip=False)
0706bb
 
0706bb
-    lines = get_file_lines(configfile)
0706bb
+    tmpfd, tmpfile = tempfile.mkstemp()
0706bb
 
0706bb
-    with open(configfile, "w") as write_file:
0706bb
+    with open(tmpfile, "w") as write_file:
0706bb
         for line in lines:
0706bb
-            write_file.write(re.sub(r'^SELINUX=.*', stateline, line))
0706bb
+            write_file.write(re.sub(r'^SELINUX=.*', stateline, line) + '\n')
0706bb
+
0706bb
+    module.atomic_move(tmpfile, configfile)
0706bb
 
0706bb
 
0706bb
 def set_state(module, state):
0706bb
@@ -115,15 +150,19 @@ def set_state(module, state):
0706bb
         module.fail_json(msg=msg)
0706bb
 
0706bb
 
0706bb
-def set_config_policy(policy, configfile):
0706bb
+def set_config_policy(module, policy, configfile):
0706bb
     # edit config file with state value
0706bb
     # SELINUXTYPE=targeted
0706bb
     policyline = 'SELINUXTYPE=%s' % policy
0706bb
-    lines = get_file_lines(configfile)
0706bb
+    lines = get_file_lines(configfile, strip=False)
0706bb
 
0706bb
-    with open(configfile, "w") as write_file:
0706bb
+    tmpfd, tmpfile = tempfile.mkstemp()
0706bb
+
0706bb
+    with open(tmpfile, "w") as write_file:
0706bb
         for line in lines:
0706bb
-            write_file.write(re.sub(r'^SELINUXTYPE=.*', policyline, line))
0706bb
+            write_file.write(re.sub(r'^SELINUXTYPE=.*', policyline, line) + '\n')
0706bb
+
0706bb
+    module.atomic_move(tmpfile, configfile)
0706bb
 
0706bb
 
0706bb
 def main():
0706bb
@@ -148,6 +187,7 @@ def main():
0706bb
     runtime_enabled = selinux.is_selinux_enabled()
0706bb
     runtime_policy = selinux.selinux_getpolicytype()[1]
0706bb
     runtime_state = 'disabled'
0706bb
+    reboot_required = False
0706bb
 
0706bb
     if runtime_enabled:
0706bb
         # enabled means 'enforcing' or 'permissive'
0706bb
@@ -167,7 +207,7 @@ def main():
0706bb
     # check to see if policy is set if state is not 'disabled'
0706bb
     if state != 'disabled':
0706bb
         if not policy:
0706bb
-            module.fail_json(msg='policy is required if state is not \'disabled\'')
0706bb
+            module.fail_json(msg='Policy is required if state is not \'disabled\'')
0706bb
     else:
0706bb
         if not policy:
0706bb
             policy = config_policy
0706bb
@@ -177,14 +217,14 @@ def main():
0706bb
         if module.check_mode:
0706bb
             module.exit_json(changed=True)
0706bb
         # cannot change runtime policy
0706bb
-        msgs.append('reboot to change the loaded policy')
0706bb
+        msgs.append('Running SELinux policy changed from \'%s\' to \'%s\'' % (runtime_policy, policy))
0706bb
         changed = True
0706bb
 
0706bb
     if policy != config_policy:
0706bb
         if module.check_mode:
0706bb
             module.exit_json(changed=True)
0706bb
-        msgs.append('config policy changed from \'%s\' to \'%s\'' % (config_policy, policy))
0706bb
-        set_config_policy(policy, configfile)
0706bb
+        set_config_policy(module, policy, configfile)
0706bb
+        msgs.append('SELinux policy configuration in \'%s\' changed from \'%s\' to \'%s\'' % (configfile, config_policy, policy))
0706bb
         changed = True
0706bb
 
0706bb
     if state != runtime_state:
0706bb
@@ -195,26 +235,30 @@ def main():
0706bb
                 if runtime_state != 'permissive':
0706bb
                     # Temporarily set state to permissive
0706bb
                     set_state(module, 'permissive')
0706bb
-                    msgs.append('runtime state temporarily changed from \'%s\' to \'permissive\', state change will take effect next reboot' % (runtime_state))
0706bb
+                    module.warn('SELinux state temporarily changed from \'%s\' to \'permissive\'. State change will take effect next reboot.' % (runtime_state))
0706bb
                 else:
0706bb
-                    msgs.append('state change will take effect next reboot')
0706bb
+                    module.warn('SELinux state change will take effect next reboot')
0706bb
+                reboot_required = True
0706bb
             else:
0706bb
                 set_state(module, state)
0706bb
-                msgs.append('runtime state changed from \'%s\' to \'%s\'' % (runtime_state, state))
0706bb
+                msgs.append('SELinux state changed from \'%s\' to \'%s\'' % (runtime_state, state))
0706bb
+
0706bb
+                # Only report changes if the file is changed.
0706bb
+                # This prevents the task from reporting changes every time the task is run.
0706bb
+                changed = True
0706bb
         else:
0706bb
-            msgs.append('state change will take effect next reboot')
0706bb
-        changed = True
0706bb
+            module.warn("Reboot is required to set SELinux state to %s" % state)
0706bb
+            reboot_required = True
0706bb
 
0706bb
     if state != config_state:
0706bb
         if module.check_mode:
0706bb
             module.exit_json(changed=True)
0706bb
-        msgs.append('config state changed from \'%s\' to \'%s\'' % (config_state, state))
0706bb
-        set_config_state(state, configfile)
0706bb
+        msgs.append('Config SELinux state changed from \'%s\' to \'%s\'' % (config_state, state))
0706bb
+        set_config_state(module, state, configfile)
0706bb
         changed = True
0706bb
 
0706bb
-    module.exit_json(changed=changed, msg=', '.join(msgs), configfile=configfile, policy=policy, state=state)
0706bb
+    module.exit_json(changed=changed, msg=', '.join(msgs), configfile=configfile, policy=policy, state=state, reboot_required=reboot_required)
0706bb
 
0706bb
-#################################################
0706bb
 
0706bb
 if __name__ == '__main__':
0706bb
     main()
0706bb
diff --git a/test/integration/targets/selinux/aliases b/test/integration/targets/selinux/aliases
0706bb
new file mode 100644
0706bb
index 0000000..53b3251
0706bb
--- /dev/null
0706bb
+++ b/test/integration/targets/selinux/aliases
0706bb
@@ -0,0 +1,2 @@
0706bb
+needs/root
0706bb
+posix/ci/group2
0706bb
diff --git a/test/integration/targets/selinux/tasks/main.yml b/test/integration/targets/selinux/tasks/main.yml
0706bb
new file mode 100644
0706bb
index 0000000..dc3e678
0706bb
--- /dev/null
0706bb
+++ b/test/integration/targets/selinux/tasks/main.yml
0706bb
@@ -0,0 +1,30 @@
0706bb
+# (c) 2017, Sam Doran <sdoran@redhat.com>
0706bb
+
0706bb
+# This file is part of Ansible
0706bb
+#
0706bb
+# Ansible is free software: you can redistribute it and/or modify
0706bb
+# it under the terms of the GNU General Public License as published by
0706bb
+# the Free Software Foundation, either version 3 of the License, or
0706bb
+# (at your option) any later version.
0706bb
+#
0706bb
+# Ansible is distributed in the hope that it will be useful,
0706bb
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
0706bb
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0706bb
+# GNU General Public License for more details.
0706bb
+#
0706bb
+# You should have received a copy of the GNU General Public License
0706bb
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
0706bb
+
0706bb
+- debug:
0706bb
+    msg: SELinux is disabled
0706bb
+  when: ansible_selinux is defined and ansible_selinux == False
0706bb
+
0706bb
+- debug:
0706bb
+    msg: SELinux is {{ ansible_selinux.status }}
0706bb
+  when: ansible_selinux is defined and ansible_selinux != False
0706bb
+
0706bb
+- include: selinux.yml
0706bb
+  when:
0706bb
+    - ansible_selinux is defined
0706bb
+    - ansible_selinux != False
0706bb
+    - ansible_selinux.status == 'enabled'
0706bb
diff --git a/test/integration/targets/selinux/tasks/selinux.yml b/test/integration/targets/selinux/tasks/selinux.yml
0706bb
new file mode 100644
0706bb
index 0000000..ff8b2fa
0706bb
--- /dev/null
0706bb
+++ b/test/integration/targets/selinux/tasks/selinux.yml
0706bb
@@ -0,0 +1,170 @@
0706bb
+# (c) 2017, Sam Doran <sdoran@redhat.com>
0706bb
+
0706bb
+# This file is part of Ansible
0706bb
+#
0706bb
+# Ansible is free software: you can redistribute it and/or modify
0706bb
+# it under the terms of the GNU General Public License as published by
0706bb
+# the Free Software Foundation, either version 3 of the License, or
0706bb
+# (at your option) any later version.
0706bb
+#
0706bb
+# Ansible is distributed in the hope that it will be useful,
0706bb
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
0706bb
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0706bb
+# GNU General Public License for more details.
0706bb
+#
0706bb
+# You should have received a copy of the GNU General Public License
0706bb
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
0706bb
+
0706bb
+
0706bb
+# First Test
0706bb
+# ##############################################################################
0706bb
+# Test changing the state, which requires a reboot
0706bb
+
0706bb
+- name: TEST 1 | Get current SELinux config file contents
0706bb
+  set_fact:
0706bb
+    selinux_config_original: "{{ lookup('file', '/etc/sysconfig/selinux').split('\n') }}"
0706bb
+    before_test_sestatus: "{{ ansible_selinux }}"
0706bb
+
0706bb
+- debug:
0706bb
+    var: "{{ item }}"
0706bb
+    verbosity: 1
0706bb
+  with_items:
0706bb
+    - selinux_config_original
0706bb
+    - before_test_sestatus
0706bb
+    - ansible_selinux
0706bb
+
0706bb
+- name: TEST 1 | Setup SELinux configuration for tests
0706bb
+  selinux:
0706bb
+    state: enforcing
0706bb
+    policy: targeted
0706bb
+
0706bb
+- name: TEST 1 | Disable SELinux
0706bb
+  selinux:
0706bb
+    state: disabled
0706bb
+    policy: targeted
0706bb
+  register: _disable_test1
0706bb
+
0706bb
+- debug:
0706bb
+    var: _disable_test1
0706bb
+    verbosity: 1
0706bb
+
0706bb
+- name: TEST 1 | Re-gather facts
0706bb
+  setup:
0706bb
+
0706bb
+- name: TEST 1 | Assert that status was changed, reboot_required is True, a warning was displayed, and SELinux is configured properly
0706bb
+  assert:
0706bb
+    that:
0706bb
+      - _disable_test1 | changed
0706bb
+      - _disable_test1.reboot_required
0706bb
+      - (_disable_test1.warnings | length ) >= 1
0706bb
+      - ansible_selinux.config_mode == 'disabled'
0706bb
+      - ansible_selinux.type == 'targeted'
0706bb
+
0706bb
+- debug:
0706bb
+    var: ansible_selinux
0706bb
+    verbosity: 1
0706bb
+
0706bb
+- name: TEST 1 | Disable SELinux again
0706bb
+  selinux:
0706bb
+    state: disabled
0706bb
+    policy: targeted
0706bb
+  register: _disable_test2
0706bb
+
0706bb
+- debug:
0706bb
+    var: _disable_test2
0706bb
+    verbosity: 1
0706bb
+
0706bb
+- name: TEST 1 | Assert that no change is reported, a warnking was dispalyed, and reboot_required is True
0706bb
+  assert:
0706bb
+    that:
0706bb
+      - not _disable_test2 | changed
0706bb
+      - (_disable_test1.warnings | length ) >= 1
0706bb
+      - _disable_test2.reboot_required
0706bb
+
0706bb
+- name: TEST 1 | Get modified config file
0706bb
+  set_fact:
0706bb
+    selinux_config_after: "{{ lookup('file', '/etc/sysconfig/selinux').split('\n') }}"
0706bb
+
0706bb
+- debug:
0706bb
+    var: selinux_config_after
0706bb
+    verbosity: 1
0706bb
+
0706bb
+- name: TEST 1 | Ensure SELinux config file is properly formatted
0706bb
+  assert:
0706bb
+    that:
0706bb
+      - selinux_config_original | length == selinux_config_after | length
0706bb
+      - selinux_config_after[selinux_config_after.index('SELINUX=disabled')] | search("^SELINUX=\w+$")
0706bb
+      - selinux_config_after[selinux_config_after.index('SELINUXTYPE=targeted')] | search("^SELINUXTYPE=\w+$")
0706bb
+
0706bb
+- name: TEST 1 | Reset SELinux configuration for next test
0706bb
+  selinux:
0706bb
+    state: enforcing
0706bb
+    policy: targeted
0706bb
+
0706bb
+
0706bb
+# Second Test
0706bb
+# ##############################################################################
0706bb
+# Test changing only the policy, which does not require a reboot
0706bb
+
0706bb
+- name: TEST 2 | Set SELinux policy
0706bb
+  selinux:
0706bb
+    state: enforcing
0706bb
+    policy: mls
0706bb
+  register: _state_test1
0706bb
+
0706bb
+- debug:
0706bb
+    var: _state_test1
0706bb
+    verbosity: 1
0706bb
+
0706bb
+- name: TEST 2 | Re-gather facts
0706bb
+  setup:
0706bb
+
0706bb
+- debug:
0706bb
+    var: ansible_selinux
0706bb
+  tags: debug
0706bb
+
0706bb
+- name: TEST 2 | Assert that status was changed, reboot_required is False, no warnings were displayed, and SELinux is configured properly
0706bb
+  assert:
0706bb
+    that:
0706bb
+      - _state_test1 | changed
0706bb
+      - not _state_test1.reboot_required
0706bb
+      - _state_test1.warnings is not defined
0706bb
+      - ansible_selinux.config_mode == 'enforcing'
0706bb
+      - ansible_selinux.type == 'mls'
0706bb
+
0706bb
+- name: TEST 2 | Set SELinux policy again
0706bb
+  selinux:
0706bb
+    state: enforcing
0706bb
+    policy: mls
0706bb
+  register: _state_test2
0706bb
+
0706bb
+- debug:
0706bb
+    var: _state_test2
0706bb
+    verbosity: 1
0706bb
+
0706bb
+- name: TEST 2 | Assert that no change was reported, no warnings were dispalyed, and reboot_required is False
0706bb
+  assert:
0706bb
+    that:
0706bb
+      - not _state_test2 | changed
0706bb
+      - _state_test2.warnings is not defined
0706bb
+      - not _state_test2.reboot_required
0706bb
+
0706bb
+- name: TEST 2 | Get modified config file
0706bb
+  set_fact:
0706bb
+    selinux_config_after: "{{ lookup('file', '/etc/sysconfig/selinux').split('\n') }}"
0706bb
+
0706bb
+- debug:
0706bb
+    var: selinux_config_after
0706bb
+    verbosity: 1
0706bb
+
0706bb
+- name: TEST 2 | Ensure SELinux config file is properly formatted
0706bb
+  assert:
0706bb
+    that:
0706bb
+      - selinux_config_original | length == selinux_config_after | length
0706bb
+      - selinux_config_after[selinux_config_after.index('SELINUX=enforcing')] | search("^SELINUX=\w+$")
0706bb
+      - selinux_config_after[selinux_config_after.index('SELINUXTYPE=mls')] | search("^SELINUXTYPE=\w+$")
0706bb
+
0706bb
+- name: TEST 2 | Reset SELinux configuration for next test
0706bb
+  selinux:
0706bb
+    state: enforcing
0706bb
+    policy: targeted