---
# (c) 2017, Martin Krizek <mkrizek@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/>.

- name: Install requirements for RHEL 7 and earlier
  ansible.builtin.package:
    name: policycoreutils-python
  when:
    - ansible_distribution == 'RedHat' and ansible_distribution_major_version is version('7', '<=')

- name: Install requirements for RHEL 8 and later
  ansible.builtin.package:
    name: policycoreutils-python-utils
  when:
    - ansible_distribution == 'RedHat' and ansible_distribution_major_version is version('8', '>=')

- name: Get getsebool output preflight
  ansible.builtin.shell:
    cmd: set -o pipefail && semanage boolean -l | grep 'httpd_can_network_connect\W'
    executable: /bin/bash
  changed_when: false
  register: getsebool_output_preflight

- name: Cleanup
  ansible.builtin.shell:
    cmd: set -o pipefail && setsebool -P httpd_can_network_connect 0
    executable: /bin/bash
  changed_when: getsebool_output_preflight.stdout.startswith('httpd_can_network_connect --> on')

- name: Set flag and don't keep it persistent
  ansible.posix.seboolean:
    name: httpd_can_network_connect
    state: true
  register: output

- name: Get getsebool output
  ansible.builtin.shell:
    cmd: set -o pipefail && semanage boolean -l | grep 'httpd_can_network_connect\W'
    executable: /bin/bash
  changed_when: false
  register: getsebool_output

- name: Check output
  ansible.builtin.assert:
    that:
      - output is changed
      - output is not failed
      - output.name == 'httpd_can_network_connect'
      - getsebool_output.stdout.startswith('httpd_can_network_connect      (on   ,  off)')
##########################################################################################
- name: Unset flag
  ansible.posix.seboolean:
    name: httpd_can_network_connect
    state: false

- name: Get getsebool output
  ansible.builtin.shell:
    cmd: set -o pipefail && semanage boolean -l | grep 'httpd_can_network_connect\W'
    executable: /bin/bash
  changed_when: false
  register: getsebool_output

- name: Check output
  ansible.builtin.assert:
    that:
      - output is changed
      - output is not failed
      - output.name == 'httpd_can_network_connect'
      - getsebool_output.stdout.startswith('httpd_can_network_connect      (off  ,  off)')

##########################################################################################
- name: Set flag and keep it persistent
  ansible.posix.seboolean:
    name: httpd_can_network_connect
    state: true
    persistent: true
  register: output

- name: Get getsebool output
  ansible.builtin.shell:
    cmd: set -o pipefail && semanage boolean -l | grep 'httpd_can_network_connect\W'
    executable: /bin/bash
  changed_when: false
  register: getsebool_output

- name: Check output
  ansible.builtin.assert:
    that:
      - output is changed
      - output is not failed
      - output.name == 'httpd_can_network_connect'
      - getsebool_output.stdout.startswith('httpd_can_network_connect      (on   ,   on)')
##########################################################################################
