Ansible Role selinux
This role
sets the state of SELinux using
setenforcetoggles SELinux booleans using
setseboolsets SELinux file contexts using
semanage fcontext. It does NOT automatically apply them usingrestorecon- have a look atselinux__restorecons__*_varmanages SELinux ports using
semanage portapplies SELinux contexts to files using
restoreconcompiles and installs custom SELinux policy modules from source (.te, .fc, .if files). Note: Module installation is not idempotent - modules with
state: presentwill always be compiled and installed on each run
Mandatory Requirements
Install the SELinux python bindings. This can be done using the linuxfabrik.lfops.policycoreutils role.
Optional Role Variables
Variable |
Description |
Default Value |
|---|---|---|
|
A list of dictionaries containing SELinux booleans to set persistently. Subkeys: |
|
|
A list of dictionaries containing SELinux file contexts. Subkeys: |
|
|
A list of dictionaries containing custom SELinux policy modules to compile and install. Subkeys:
host_vars / group_vars (can only be used in one group at a time). Note: Modules with state: present will always be compiled and installed on each run to ensure they stay up-to-date with source changes. |
|
|
The name of the SELinux policy to use. |
|
|
A list of dictionaries containing SELinux ports. Subkeys:
|
|
|
A list of dictionaries containing paths to run
host_vars / group_vars (can only be used in one group at a time). |
|
|
The SELinux state. Possible options: |
|
Example:
# optional
selinux__booleans__host_var:
- key: 'httpd_can_network_connect_db'
value: 'on'
- key: 'httpd_can_sendmail'
value: 'on'
- key: 'httpd_execmem'
value: 'on'
- key: 'httpd_use_nfs'
value: 'on'
selinux__fcontexts__host_var:
- setype: 'httpd_sys_rw_content_t'
target: '/data(/.*)?'
state: 'present'
- setype: 'httpd_sys_rw_content_t'
target: '/var/www/html/nextcloud/.htaccess'
state: 'present'
selinux__modules__host_var:
- name: 'myapp_policy'
src: '{{ inventory_dir }}/host_files/selinux/myapp_policy' # directory containing myapp_policy.te, myapp_policy.fc, myapp_policy.if
state: 'present'
- name: 'custom_httpd'
src: '{{ inventory_dir }}/host_files/selinux/custom_httpd'
- name: 'old_module'
state: 'absent'
selinux__policy: 'default'
selinux__ports__host_var:
- setype: 'http_port_t'
port: '8070-8080'
- setype: 'ssh_port_t'
port: 22
selinux__restorecons__host_var:
- path: '/data'
- path: '/var/www/html/nextcloud'
- path: '/opt/app/file.txt'
recursive: false # only restore this specific file, not recursively
- path: '/tmp/test'
force: false # only update the type portion of the context
- path: '/old/legacy/path'
state: 'absent' # skip this path
selinux__state: 'enforcing'