0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ansible : ansible.posix.sysctl モジュールで RHEL kernel 設定を実行

Last updated at Posted at 2025-07-20

はじめに

Ansibleの ansible.posix.sysctl モジュールを使用してRHEL のカーネルパラメータを設定する Ansible playbookを検証を含めて作成した際のログです。

・ansible.posix.sysctl module – Manage entries in sysctl.conf.
https://docs.ansible.com/ansible/latest/collections/ansible/posix/sysctl_module.html


環境

  • Ansible サーバー
    RHEL 9.4 (ppc64le)

  • Ansible 実行対象サーバー
    RHEL 9.4 (ppc64le)

IBM Power 上の RHEL で確認しています。

  • HW: S1014

Ansible 基本設定

インベントリー

inventory/test.yml
[rhel1]
ansible94u ansible_user=ansible-test ansible_ssh_private_key_file=/home/ansible-test/.ssh/ansible-test-id_rsa

ホスト名 ansible94u は/etc/hostsで名前解決しています。
ansible94u はAnsible実行サーバーと /root/.ssh/id_rsa_ansible で鍵交換。


オフラインで ansible.posix モジュールの導入

  1. Ansible Galaxyのサイトから ansible.posix コレクションのtarballをダウンロードします。

  1. ダウンロードしたファイル(例: ansible-posix-2.0.0.tar.gz)をAnsibleサーバーに配置します。

  2. ansible-galaxyコマンドでコレクションをインストールします。

#  ansible-galaxy collection install ansible-posix-2.0.0.tar.gz
Using /work/ansible-test/ansible.cfg as config file
Process install dependency map
Starting collection install process
Installing 'ansible.posix:2.0.0' to '/root/.ansible/collections/ansible_collections/ansible/posix'

その他、ssh 鍵での疎通設定を実施しています。


RHEL kernel 設定の Playbook

Roleを使用し、変数ファイルをループ処理することで、複数のカーネル設定を一度に実行できる構造です。

ディレクトリ構造

<ansible home>
│
├── rhel_sysctl_set.yml
├── ansible.cfg
├── inventory
│   └── test.yml
│
├── group_vars
│   └── rhel_sysctl_set_vars.yml  # ← kernel 設定変数ファイル
│
└── roles
    └── rhel_sysctl_set
         └── tasks
            └── main.yml

Playbook定義

  • <ansible home>/rhel_sysctl_set.yml
rhel_sysctl_set.yml
---
- name: Set RHEL kernel parameters using a role
  hosts: "{{ target_hosts | default(dummy) }}"
  gather_facts: yes

  roles:
    - rhel_sysctl_set
  • <ansible home>/roles/rhel_sysctl_set/tasks/main.yml
main.yml
---
- name: Set sysctl parameters
  ansible.posix.sysctl:
    name: "{{ item.name }}"
    value: "{{ item.value }}"
    sysctl_file: "{{ sysctl_file }}"
    state: present
  loop: "{{ rhel_sysctl_settings }}"
  • <ansible home>/group_vars/rhel_sysctl_set_vars.yml
rhel_sysctl_set_vars.yml
---
# RHELサーバー用のカーネルパラメータ設定
# この変数は roles/rhel_sysctl_set で使用されます。
sysctl_file: '/etc/sysctl.d/99-ansible-custom.conf'
rhel_sysctl_settings:
- name: net.ipv4.ip_forward
  value: '0'
- name: net.ipv4.conf.all.accept_redirects
  value: '0'
- name: net.ipv4.conf.all.send_redirects
  value: '0'
- name: net.ipv4.conf.default.accept_redirects
  value: '0'
- name: net.ipv4.conf.default.send_redirects
  value: '0'
- name: net.ipv6.conf.all.accept_redirects
  value: '0'
- name: net.ipv6.conf.default.accept_redirects
  value: '0'

RHEL kernel 設定実行


# ansible-playbook rhel_sysctl_set.yml -e "@./group_vars/rhel_sysctl_set_vars.yml" -e "target_hosts=rhel1"

Using /work/ansible-test/ansible.cfg as config file

PLAY [Set RHEL kernel parameters using a role] *********************************
Friday 18 July 2025  02:40:56 -0400 (0:00:00.044)       0:00:00.044 ***********
Friday 18 July 2025  02:40:57 -0400 (0:00:01.285)       0:00:01.330 ***********

TASK [rhel_sysctl_set : Set sysctl parameters] *********************************
changed: [ansible94u] => (item={'name': 'net.ipv4.conf.all.accept_redirects', 'value': '0'}) => {"ansible_loop_var": "item", "changed": true, "item": {"name": "net.ipv4.conf.all.accept_redirects", "value": "0"}}
changed: [ansible94u] => (item={'name': 'net.ipv4.conf.all.send_redirects', 'value': '0'}) => {"ansible_loop_var": "item", "changed": true, "item": {"name": "net.ipv4.conf.all.send_redirects", "value": "0"}}
changed: [ansible94u] => (item={'name': 'net.ipv4.conf.default.accept_redirects', 'value': '0'}) => {"ansible_loop_var": "item", "changed": true, "item": {"name": "net.ipv4.conf.default.accept_redirects", "value": "0"}}
changed: [ansible94u] => (item={'name': 'net.ipv4.conf.default.send_redirects', 'value': '0'}) => {"ansible_loop_var": "item", "changed": true, "item": {"name": "net.ipv4.conf.default.send_redirects", "value": "0"}}
changed: [ansible94u] => (item={'name': 'net.ipv6.conf.all.accept_redirects', 'value': '0'}) => {"ansible_loop_var": "item", "changed": true, "item": {"name": "net.ipv6.conf.all.accept_redirects", "value": "0"}}
changed: [ansible94u] => (item={'name': 'net.ipv6.conf.default.accept_redirects', 'value': '0'}) => {"ansible_loop_var": "item", "changed": true, "item": {"name": "net.ipv6.conf.default.accept_redirects", "value": "0"}}

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

Friday 18 July 2025  02:41:00 -0400 (0:00:02.202)       0:00:03.533 ***********
===============================================================================
rhel_sysctl_set : Set sysctl parameters --------------------------------- 2.20s
Gathering Facts --------------------------------------------------------- 1.29s

確認

etc/sysctl.d/99-ansible-custom.conf の内容を確認

# ansible rhel1 -m shell -a "cat /etc/sysctl.d/99-ansible-custom.conf"

Using /work/ansible-test/ansible.cfg as config file
ansible94u | CHANGED | rc=0 >>
net.ipv4.ip_forward=0
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.conf.default.send_redirects=0
net.ipv6.conf.all.accept_redirects=0
net.ipv6.conf.default.accept_redirects=0

コマンドで kernel パラメータ設定値の確認

# ansible rhel1 -m shell -a "sysctl net.ipv4.ip_forward"
Using /work/ansible-test/ansible.cfg as config file
ansible94u | CHANGED | rc=0 >>
net.ipv4.ip_forward = 0


# ansible rhel1 -m shell -a "sysctl net.ipv4.conf.all.accept_redirects"
Using /work/ansible-test/ansible.cfg as config file
ansible94u | CHANGED | rc=0 >>
net.ipv4.conf.all.accept_redirects = 0


# ansible rhel1 -m shell -a "sysctl net.ipv4.conf.all.send_redirects"
Using /work/ansible-test/ansible.cfg as config file
ansible94u | CHANGED | rc=0 >>
net.ipv4.conf.all.send_redirects = 0


# ansible rhel1 -m shell -a "sysctl net.ipv4.conf.default.accept_redirects"
Using /work/ansible-test/ansible.cfg as config file
ansible94u | CHANGED | rc=0 >>
net.ipv4.conf.default.accept_redirects = 0


# ansible rhel1 -m shell -a "sysctl net.ipv4.conf.default.send_redirects"
Using /work/ansible-test/ansible.cfg as config file
ansible94u | CHANGED | rc=0 >>
net.ipv4.conf.default.send_redirects = 0


# ansible rhel1 -m shell -a "sysctl net.ipv6.conf.all.accept_redirects"
Using /work/ansible-test/ansible.cfg as config file
ansible94u | CHANGED | rc=0 >>
net.ipv6.conf.all.accept_redirects = 0

# ansible rhel1 -m shell -a "sysctl net.ipv6.conf.default.accept_redirects"
Using /work/ansible-test/ansible.cfg as config file
ansible94u | CHANGED | rc=0 >>
net.ipv6.conf.default.accept_redirects = 0

ーーー

sysctl の詳細説明

sysctl -a コマンドで出力される内容についてまとめた内容を折りたたみ表記しています。
RHEL 9.4 で確認した内容です。

RHEL `sysctl -a` 出力項目の解説

sysctl -a コマンドは、Linuxカーネルのパラメータを一覧表示します。これらのパラメータは、システムの動作(メモリ管理、ネットワーク、ファイルシステムなど)を動的に変更するために使用されます。

【重要】
これらのパラメータの変更は、システムのパフォーマンスや安定性、セキュリティに大きな影響を与える可能性があります。変更する際は、そのパラメータの意味を十分に理解し、テスト環境で検証した上で実施が必要です。


1. 暗号化関連 (crypto)

カーネルの暗号化APIやFIPSモードに関する設定です。

パラメータ名 意味・概要
crypto.fips_enabled FIPS (連邦情報処理標準) モードが有効かどうかを示します。(0: 無効, 1: 有効)
crypto.fips_name FIPSモジュール名を示します。
crypto.fips_version FIPSモジュールのバージョンを示します。

2. デバッグ関連 (debug)

カーネルのデバッグ機能に関する設定です。

パラメータ名 意味・概要
debug.exception-trace カーネルの例外発生時にスタックトレースを出力するかどうか。(1: 有効)
debug.kprobes-optimization kprobes (カーネルの動的トレーシング) の最適化を有効にするかどうか。(1: 有効)

3. デバイス関連 (dev)

CD-ROM、RAID、SCSI、TTYなどのデバイスに関する設定です。

パラメータ名 意味・概要
dev.cdrom.* CD-ROMドライブに関する設定。
dev.raid.speed_limit_max RAID再同期(resync)速度の上限 (KB/s)。
dev.raid.speed_limit_min RAID再同期速度の下限 (KB/s)。
dev.scsi.logging_level SCSIサブシステムのログレベル。
dev.tty.ldisc_autoload (RHEL 9で追加) TTYラインディシプリンモジュールの自動ロードを許可するかどうか。セキュリティ強化のため。

4. ファイルシステム関連 (fs)

ファイルシステム、VFS、各種リソース制限に関する設定です。

パラメータ名 意味・概要
fs.aio-max-nr システム全体での非同期I/Oリクエストの最大数。
fs.binfmt_misc.status binfmt_misc モジュール(様々なバイナリ形式を扱う)が有効かどうか。
fs.dentry-state dentryキャッシュの状態。
fs.dir-notify-enable dnotify(ディレクトリ変更通知)を有効にするか。
fs.epoll.max_user_watches epollで1ユーザーが監視できるファイルディスクリプタの最大数。
fs.fanotify.* (RHEL 9で追加/拡充) fanotify(ファイルアクセス監視機構)のキューやマーク数の上限。
fs.file-max システム全体で同時にオープンできるファイルディスクリプタの最大数。
fs.inode-state inodeキャッシュの状態。
fs.inotify.* inotify(ファイル/ディレクトリ変更監視機構)のインスタンス数やウォッチ数などの上限。
fs.lease-break-time ファイルリースが破棄されるまでの猶予時間(秒)。
fs.mount-max システム全体でマウントできるファイルシステムの最大数。
fs.mqueue.* POSIXメッセージキューの最大数、最大メッセージサイズなどの設定。
fs.nr_open 1プロセスがオープンできるファイルディスクリプタの最大数(ulimit -n に対応)。
fs.pipe-max-size パイプの最大サイズ (バイト)。
fs.protected_fifos (デフォルト値変更) スティッキービット付きの誰でも書き込めるディレクトリ内で、FIFOファイルが所有者以外に上書きされるのを防ぐか。(RHEL 9ではデフォルトで有効)
fs.protected_hardlinks スティッキービット付きの誰でも書き込めるディレクトリ内で、所有者以外がファイルへのハードリンクを作成するのを防ぐか。
fs.protected_regular (デフォルト値変更) スティッキービット付きの誰でも書き込めるディレクトリ内で、通常ファイルが所有者以外に上書きされるのを防ぐか。(RHEL 9ではデフォルトで有効)
fs.protected_symlinks スティッキービット付きの誰でも書き込めるディレクトリ内で、所有者以外がシンボリックリンクをたどることを防ぐか。
fs.quota.* ファイルシステムのディスククォータに関する統計情報や設定。
fs.suid_dumpable (デフォルト値変更) SUID/SGIDが設定されたプロセスがコアダンプを生成するかの制御。2はより安全なモード(suidsafe)を示す。
fs.xfs.* XFSファイルシステム固有のチューニングパラメータ。

5. カーネルコア関連 (kernel)

カーネルのコア機能、プロセススケジューラ、メモリ、システム情報などに関する設定です。

パラメータ名 意味・概要
kernel.hostname システムのホスト名。
kernel.osrelease / kernel.ostype / kernel.version OSのリリース、タイプ、ビルド情報。
kernel.panic カーネルパニック発生後、何秒で自動的に再起動するか (0は無効)。
kernel.panic_on_oops カーネルが致命的なエラー (Oops) を検出した際にパニックさせるか。
kernel.core_pattern コアダンプファイルの命名規則とパス。RHEL 8のsystemd-coredumpへのパイプとは異なるデフォルト値。
kernel.ctrl-alt-del Ctrl+Alt+Deleteキーの組み合わせでシステムを即時再起動するかどうか (0: 無効)。
kernel.hung_task_* ハングタスク(長時間応答しないタスク)検出機能に関する設定。
kernel.io_uring_disabled (RHEL 9で追加) 高性能非同期I/Oインターフェースio_uringを無効にするか。セキュリティ上の理由で設定可能。
kernel.pid_max プロセスID (PID) の最大値。
kernel.threads-max システム全体で作成可能なスレッドの最大数。
kernel.randomize_va_space アドレス空間配置のランダム化 (ASLR) のレベル。セキュリティ機能。
kernel.sched_autogroup_enabled (デフォルト値変更) タスクをセッションごとに自動グループ化し、デスクトップ環境などの対話的応答性を向上させる。(RHEL 9ではデフォルトで有効)
kernel.sched_* CPUスケジューラに関する詳細な設定。
kernel.shmmax / kernel.shmall / kernel.shmmni System V 共有メモリの最大セグメントサイズ、総ページ数、最大セグメント数。
kernel.sem System V セマフォの設定。
kernel.sysrq Magic SysRqキーの機能を有効にするか。
kernel.task_delayacct (RHEL 9で追加) タスクの遅延(I/O待ち、CPU待ちなど)の統計情報を収集するか。
kernel.unprivileged_bpf_disabled (デフォルト値変更) 一般ユーザーによるBPFシステムコールの使用を制限する。2は全ての一般ユーザーに対して無効化する、より強力な設定。
kernel.yama.ptrace_scope ptrace システムコールの使用範囲を制限するセキュリティ設定。

6. ネットワーク関連 (net)

ネットワークスタック全般、TCP/IP (IPv4, IPv6)、ソケット、Netfilterなどに関する設定です。

6.1. コア設定 (net.core)
パラメータ名 意味・概要
net.core.somaxconn TCPリスニングソケットの接続要求バックログキューの最大長。
net.core.netdev_max_backlog ネットワークデバイスがカーネルにパケットを渡す際のキューの最大長。
net.core.rmem_default / net.core.wmem_default TCPソケットのデフォルト送受信バッファサイズ (バイト)。
net.core.rmem_max / net.core.wmem_max TCPソケットの最大送受信バッファサイズ (バイト)。
net.core.gro_normal_batch (RHEL 9で追加) GRO (Generic Receive Offload) が一度にまとめるパケット数の設定。
6.2. IPv4設定 (net.ipv4)
パラメータ名 意味・概要
net.ipv4.ip_forward IPv4パケット転送(ルーティング)を有効にするか (0: 無効, 1: 有効)。
net.ipv4.ip_local_port_range TCP/UDPが使用するエフェメラルポート(動的ポート)の範囲。
net.ipv4.tcp_syncookies SYN flood攻撃から保護するためのSYN Cookieを有効にするか。
net.ipv4.tcp_tw_reuse TIME_WAIT状態のソケットを新しい接続に再利用することを許可するか。
net.ipv4.tcp_fin_timeout TCP接続がFIN-WAIT-2状態に留まる最大時間(秒)。
net.ipv4.tcp_keepalive_time TCPキープアライブパケットを送信し始めるまでのアイドル時間(秒)。
net.ipv4.tcp_rmem / net.ipv4.tcp_wmem TCP送受信バッファサイズ [最小, デフォルト, 最大] (バイト)。
net.ipv4.conf.all.accept_redirects (デフォルト値変更) ICMPリダイレクトメッセージを受け入れるか。セキュリティのため、RHEL 9ではデフォルトで無効。
net.ipv4.conf.all.send_redirects (デフォルト値変更) ICMPリダイレクトメッセージを送信するか。ルーターでない限り不要で、RHEL 9ではデフォルトで無効。
net.ipv4.conf.all.rp_filter リバースパスフィルタリングを有効にし、IPスプーフィング攻撃を緩和する。
net.ipv4.tcp_mtu_probing (デフォルト値変更) Path MTU Discoveryを補助するMTU探索機能を有効にするか。RHEL 9ではデフォルトで有効。
6.3. IPv6設定 (net.ipv6)

IPv6プロトコルに関する設定です。net.ipv4 と同様のパラメータが多く存在します。

パラメータ名 意味・概要
net.ipv6.conf.all.forwarding IPv6パケット転送(ルーティング)を有効にするか。
net.ipv6.conf.all.accept_ra ルーター広告 (RA) を受け入れるか。
net.ipv6.conf.all.disable_ipv6 (値に注意) 全てのインターフェースでIPv6を無効にするか。この環境では1(無効)に設定されている。
6.4. MPTCP設定 (net.mptcp)

Multipath TCP (MPTCP) に関する設定です。

パラメータ名 意味・概要
net.mptcp.enabled MPTCPを有効にするか。
net.mptcp.scheduler MPTCPのパス(経路)選択スケジューラ。
6.5. Netfilter設定 (net.netfilter / net.nf_conntrack_max)

コネクショントラッキング(conntrack)に関する設定で、ファイアウォールやNATの動作に重要です。

パラメータ名 意味・概要
net.netfilter.nf_conntrack_max 追跡できるネットワーク接続の最大数。
net.netfilter.nf_conntrack_tcp_timeout_established ESTABLISHED状態のTCP接続がタイムアウトするまでの時間(秒)。

7. SunRPC関連 (sunrpc)

NFSなどで使用されるSun Remote Procedure Call (RPC) に関する設定です。

パラメータ名 意味・概要
sunrpc.tcp_slot_table_entries RPC over TCPの最大リクエスト数(スロットテーブルのエントリ数)。
sunrpc.udp_slot_table_entries RPC over UDPの最大リクエスト数。

8. ユーザーリソース関連 (user)

ユーザー名前空間やinotifyなどのユーザーごとのリソース上限に関する設定です。

パラメータ名 意味・概要
user.max_cgroup_namespaces 1ユーザーが作成できるcgroup名前空間の最大数。
user.max_fanotify_groups 1ユーザーが作成できるfanotifyグループの最大数。
user.max_inotify_instances 1ユーザーが作成できるinotifyインスタンスの最大数。
user.max_user_namespaces 1ユーザーが作成できるユーザー名前空間の最大数。

9. 仮想メモリ管理 (vm)

仮想メモリ、スワップ、ページキャッシュ、ディスクI/Oバッファなどに関する設定です。

パラメータ名 意味・概要
vm.swappiness (デフォルト値変更) メモリのスワップアウト傾向 (0-100)。RHEL 9では1と非常に低く、可能な限りスワップを避ける設定。
vm.dirty_ratio システム全体のメモリに対するダーティページ(ディスクに未書き込みのデータ)の割合の上限。
vm.dirty_background_ratio バックグラウンドでダーティページの書き込みを開始する、システム全体のメモリに対する割合。
vm.vfs_cache_pressure カーネルがdentryやinodeキャッシュを回収する傾向を制御する値。
vm.overcommit_memory (デフォルト値変更) メモリのオーバーコミットポリシー。1は常にオーバーコミットを許可する設定。RHEL 8の0(ヒューリスティック)から変更。
vm.min_free_kbytes システム全体で維持しようとする空きメモリの最小量 (KB)。
vm.max_map_count 1プロセスが持つことができるメモリマップ領域(VMA)の最大数。
vm.page-cluster ディスクI/Oの際に一度に読み書きするページ数 (2のpage-cluster乗)。
vm.panic_on_oom メモリ不足 (OOM) が発生した際に、プロセスをKILLする代わりにカーネルパニックを発生させるか。
vm.zone_reclaim_mode NUMAノードのローカルメモリが不足した際の挙動を制御します。
vm.unprivileged_userfaultfd (RHEL 9で追加) 一般ユーザーが userfaultfd システムコール(オンデマンドページング用)を使用できるか。

おわりに

rhel-system-roles.kernel_settings で設定する方法もありますが、定義ファイル(/etc/sysctl.d/99-ansible-custom.conf)を作成する方法などが異なっているようでした。

ひとまず最初に作成していた ansible.posix.sysctl モジュールで設定を確認しました。

参考:

RHEL システムロールを使用したカーネルパラメーターの永続的な設定

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?