Ansible 検証「ansible.posix.selinux」モジュールで RHEL 9 の SELinux を無効化してはいけない ※2025/03 現在

こんにちは。サイオステクノロジーの橋本です。
今回、調べてもイマイチわからなかったので実際に動作検証してみました。

今回のテーマは
Ansible のモジュール「ansible.posix.selinux」を用いて
RHEL 9 の SELinux を変更すると正しく変更してくれるか
です。

結論は…タイトルにある通りです。

前提として RHEL 9 から SELinux の無効化方法が変更となっています。

【参考】RHEL9 での SELinux の無効化方法について

今回 SELinux を無効化する PlayBook は以下の通りかなりシンプルになります。

- name: Disable SELinux
  ansible.posix.selinux:
    state: disabled

RHEL 9 サーバは現状 SELinux 有効化 (Enforcing) されています。

RHEL 9 サーバの現状設定 : getenforce

# getenforce
Enforcing
#

RHEL 9 で SELinux を無効化するには
grubby --update-kernel ALL --args selinux=0
コマンドを実行する必要があります。

上記コマンドを実行した場合、「/etc/default/grub」ファイルあるいは
「grubby –info=ALL」の実行結果に「selinux=0」という記述が追記されます。
現状では SELinux に関わる記述は特にありません

RHEL 9 サーバの現状設定 : grub

# cat /etc/default/grub
GRUB_CMDLINE_LINUX="console=ttyS0,115200n8 console=tty0 net.ifnames=0 rd.blacklist=nouveau nvme_core.io_timeout=4294967295"
GRUB_TIMEOUT=0
GRUB_ENABLE_BLSCFG=true
GRUB_DEFAULT=saved
#
# grubby --info=ALL
index=0
kernel="/boot/vmlinuz-5.14.0-362.13.1.el9_3.x86_64"
args="console=ttyS0,115200n8 console=tty0 net.ifnames=0 rd.blacklist=nouveau nvme_core.io_timeout=4294967295 crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M $tuned_params"
root="UUID=8b4a4ce6-cc11-42e3-afc4-bbb13f950d40"
initrd="/boot/initramfs-5.14.0-362.13.1.el9_3.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (5.14.0-362.13.1.el9_3.x86_64) 9.3 (Plow)"
id="ffffffffffffffffffffffffffffffff-5.14.0-362.13.1.el9_3.x86_64"
#

PlayBook を実行する前に旧来のSELinux 設定ファイル (/etc/selinux/config) を見てみましょう。

RHEL 9 サーバの現状設定 : /etc/selinux/config

# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
# See also:
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/using_selinux/changing-selinux-states-and-modes_using-selinux#changing-selinux-modes-at-boot-time_changing-selinux-states-and-modes
#
# NOTE: Up to RHEL 8 release included, SELINUX=disabled would also
# fully disable SELinux during boot. If you need a system with SELinux
# fully disabled instead of SELinux running with no policy loaded, you
# need to pass selinux=0 to the kernel command line. You can use grubby
# to persistently set the bootloader to boot with selinux=0:
#
# grubby --update-kernel ALL --args selinux=0
#
# To revert back to SELinux enabled:
#
# grubby --update-kernel ALL --remove-args selinux
#
SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

#

さて、上記 Playbook の実行です!!

$ ansible-playbook -i hosts main.yml

PLAY [all] **************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************
金曜日 07 3月 2025 13:13:38 +0900 (0:00:00.035) 0:00:00.035 ****************
ok: [172.31.15.211]

TASK [oss_install : Disable SELinux] ************************************************************************************************************
金曜日 07 3月 2025 13:13:41 +0900 (0:00:00.025) 0:00:02.937 ****************
[WARNING]: SELinux state temporarily changed from 'enforcing' to 'permissive'. State change will take effect next reboot.
changed: [172.31.15.211]

PLAY RECAP **************************************************************************************************************************************
172.31.15.211 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

金曜日 07 3月 2025 13:13:43 +0900 (0:00:01.561) 0:00:04.499 ****************
===============================================================================
Gathering Facts -------------------------------------------------------------------------------------------------------------------------- 2.88s
oss_install : Disable SELinux ------------------------------------------------------------------------------------------------------------ 1.56s
$

結論は…残念 /etc/selinux/config に設定が記述されてしまいました。
この状況でサーバ再起動をするとサーバが起動しない可能性があるので、
「ansible.posix.selinux」モジュールを利用して RHEL 9 の SELinux を無効化しない方がよさそうです。

# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
# See also:
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/using_selinux/changing-selinux-states-and-modes_using-selinux#changing-selinux-modes-at-boot-time_changing-selinux-states-and-modes
#
# NOTE: Up to RHEL 8 release included, SELINUX=disabled would also
# fully disable SELinux during boot. If you need a system with SELinux
# fully disabled instead of SELinux running with no policy loaded, you
# need to pass selinux=0 to the kernel command line. You can use grubby
# to persistently set the bootloader to boot with selinux=0:
#
# grubby --update-kernel ALL --args selinux=0
#
# To revert back to SELinux enabled:
#
# grubby --update-kernel ALL --remove-args selinux
#
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

#

補足 ちなみに以下のように記述してもうまく行きませんでした。

- name: Disable SELinux
  ansible.posix.selinux:
    configfile: /etc/default/grub
    state: disabled

実行結果

# cat /etc/default/grub
GRUB_CMDLINE_LINUX="console=ttyS0,115200n8 console=tty0 net.ifnames=0 rd.blacklist=nouveau nvme_core.io_timeout=4294967295"
GRUB_TIMEOUT=0
GRUB_ENABLE_BLSCFG=true
GRUB_DEFAULT=saved
SELINUX=disabled
#
ご覧いただきありがとうございます! この投稿はお役に立ちましたか?

役に立った 役に立たなかった

2人がこの投稿は役に立ったと言っています。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です