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?

More than 1 year has passed since last update.

Ansibleのsystemdモジュールを使ってサービスを操作してみた

Last updated at Posted at 2023-06-20

検証環境

コントロールノード・・・RHバージョン8.6にインストールしたansible 2.9.25を使っています
ターゲットノード ・・・RHバージョン8.6

検証内容

複数のサービスに対して起動/停止、及び、自動起動設定のON/OFF行う
基本的にサーバー構築時は役割に応じて必要なサービスを起動したり、リソース節約の観点から不要なサービスを停止する必要があるため、サービス開始・停止の処理をそれぞれ準備しました

変数

複数のサービスを処理できるように、リストとして定義しました

---
# vars file for os_base
enable_services:
  - crond.service
  - kdump.service
  - chronyd.service

disable_services:
  - iptables.service
  - postfix.service

playbook

オプション 説明
name 操作するサービス名
state started・・・サービス起動
stopped・・・サービス停止
enabled OS起動時に自動起動するか
---
- name: enable_services
  systemd:
    name: "{{ item }}"
    state: started
    enabled: yes
  loop: "{{ enable_services }}"

- name: disable_services
  systemd:
    name: "{{ item }}"
    state: stopped
    enabled: no
  loop: "{{ disable_services }}"

実行結果

image.png

参考サイト

所感

今回は処理対象サーバーがRHEL7以上の為、systemdモジュールを使っていますが、RHEL7未満の場合はSysVinitなのでserviceモジュールを使う必要があるのでそこは注意が必要だと思いました

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?