systemd モジュール

2019/05/03

Ansible

 Ansible Documentation

systemd – Manage services

 機能

systemd サービスを管理します

 パラメータ

パラメータ選択肢/
Default
説明
daemon_reloadno
・yes
yes を指定すると、他の操作を行う前にdaemon-reloadを実行する
enabled・no
・yes
yes を指定すると、起動時にサービスを自動起動する
name-サービス名。chroot 環境では常にフルネーム(例 crond.service)を指定しなければならない
state・reloaded
・restarted
・started
・stopped
・reloaded
サービスの状態に関係なくサービスをリロードする
・restarted
サービスの状態に関係なくサービスを再起動する
・started
サービスを起動する。すでにサービスが起動しているときは何もしない
・stopped
サービスを停止する。すでにサービスが停止しているときは何もしない

 例

    - name: httpd サービスを起動する
      systemd:
        state: stopped
        name: httpd
実行前
<[root@node-c0706 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)
[root@node-c0706 ~]# 
実行後
[root@node-c0706 ~]# systemctl status httpd
 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-05-03 23:35:54 JST; 6s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 11149 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─11149 /usr/sbin/httpd -DFOREGROUND
           ├─11150 /usr/sbin/httpd -DFOREGROUND
           ├─11151 /usr/sbin/httpd -DFOREGROUND
           ├─11152 /usr/sbin/httpd -DFOREGROUND
           ├─11153 /usr/sbin/httpd -DFOREGROUND
           └─11154 /usr/sbin/httpd -DFOREGROUND

May 03 23:35:54 node-c0706.exam.local systemd[1]: Starting The Apache HTTP Se...
May 03 23:35:54 node-c0706.exam.local systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
[root@node-c0706 ~]# 

    - name: httpd サービスを停止する
      systemd:
        state: stopped
        name: httpd
実行前
[root@node-c0706 ~]# systemctl status httpd
 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-05-03 23:35:54 JST; 2min 34s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 11149 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─11149 /usr/sbin/httpd -DFOREGROUND
           ├─11150 /usr/sbin/httpd -DFOREGROUND
           ├─11151 /usr/sbin/httpd -DFOREGROUND
           ├─11152 /usr/sbin/httpd -DFOREGROUND
           ├─11153 /usr/sbin/httpd -DFOREGROUND
           └─11154 /usr/sbin/httpd -DFOREGROUND

May 03 23:35:54 node-c0706.exam.local systemd[1]: Starting The Apache HTTP Se...
May 03 23:35:54 node-c0706.exam.local systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
[root@node-c0706 ~]# 
実行後
[root@node-c0706 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)

May 03 23:35:54 node-c0706.exam.local systemd[1]: Starting The Apache HTTP Se...
May 03 23:35:54 node-c0706.exam.local systemd[1]: Started The Apache HTTP Ser...
May 03 23:38:51 node-c0706.exam.local systemd[1]: Stopping The Apache HTTP Se...
May 03 23:38:52 node-c0706.exam.local systemd[1]: Stopped The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
[root@node-c0706 ~]# 

    - name: httpd サービスを起動する。自動起動を有効にする
      systemd:
        state: started
        name: httpd
        enabled: yes
実行前
[root@node-c0706 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)

May 03 23:35:54 node-c0706.exam.local systemd[1]: Starting The Apache HTTP Se...
May 03 23:35:54 node-c0706.exam.local systemd[1]: Started The Apache HTTP Ser...
May 03 23:38:51 node-c0706.exam.local systemd[1]: Stopping The Apache HTTP Se...
May 03 23:38:52 node-c0706.exam.local systemd[1]: Stopped The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
[root@node-c0706 ~]# 
[root@node-c0706 ~]# systemctl is-enabled httpd
disabled
[root@node-c0706 ~]# 
実行後
[root@node-c0706 ~]# systemctl status httpd
 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-05-03 23:43:16 JST; 8s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 11407 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─11407 /usr/sbin/httpd -DFOREGROUND
           ├─11408 /usr/sbin/httpd -DFOREGROUND
           ├─11409 /usr/sbin/httpd -DFOREGROUND
           ├─11410 /usr/sbin/httpd -DFOREGROUND
           ├─11411 /usr/sbin/httpd -DFOREGROUND
           └─11412 /usr/sbin/httpd -DFOREGROUND

May 03 23:43:16 node-c0706.exam.local systemd[1]: Starting The Apache HTTP Se...
May 03 23:43:16 node-c0706.exam.local systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
[root@node-c0706 ~]# 
[root@node-c0706 ~]# systemctl is-enabled httpd
enabled
[root@node-c0706 ~]# 

    - name: 設定変更を反映して、httpd サービスを再起動する
      systemd:
        state: restarted
        daemon_reload: yes
        name: httpd
実行前
[root@node-c0706 ~]# systemctl status httpd
 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-05-03 23:43:16 JST; 5min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 11407 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─11407 /usr/sbin/httpd -DFOREGROUND
           ├─11408 /usr/sbin/httpd -DFOREGROUND
           ├─11409 /usr/sbin/httpd -DFOREGROUND
           ├─11410 /usr/sbin/httpd -DFOREGROUND
           ├─11411 /usr/sbin/httpd -DFOREGROUND
           └─11412 /usr/sbin/httpd -DFOREGROUND

May 03 23:43:16 node-c0706.exam.local systemd[1]: Starting The Apache HTTP Se...
May 03 23:43:16 node-c0706.exam.local systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
[root@node-c0706 ~]# 
実行後
ログに stopped → Starting → Started とあり、サービスが再起動していることがわかる
[root@node-c0706 ~]# systemctl status httpd
 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-05-03 23:49:04 JST; 8s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 11512 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 11516 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─11516 /usr/sbin/httpd -DFOREGROUND
           ├─11517 /usr/sbin/httpd -DFOREGROUND
           ├─11518 /usr/sbin/httpd -DFOREGROUND
           ├─11519 /usr/sbin/httpd -DFOREGROUND
           ├─11520 /usr/sbin/httpd -DFOREGROUND
           └─11521 /usr/sbin/httpd -DFOREGROUND

May 03 23:49:04 node-c0706.exam.local systemd[1]: Stopped The Apache HTTP Ser...
May 03 23:49:04 node-c0706.exam.local systemd[1]: Starting The Apache HTTP Se...
May 03 23:49:04 node-c0706.exam.local systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
[root@node-c0706 ~]# 

    - name: httpd サービスをリロードする
      systemd:
        state: reloaded
        name: httpd
実行前
[root@node-c0706 ~]# systemctl status httpd
 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-05-03 23:49:04 JST; 5min ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 11512 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 11516 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─11516 /usr/sbin/httpd -DFOREGROUND
           ├─11517 /usr/sbin/httpd -DFOREGROUND
           ├─11518 /usr/sbin/httpd -DFOREGROUND
           ├─11519 /usr/sbin/httpd -DFOREGROUND
           ├─11520 /usr/sbin/httpd -DFOREGROUND
           └─11521 /usr/sbin/httpd -DFOREGROUND

May 03 23:49:04 node-c0706.exam.local systemd[1]: Stopped The Apache HTTP Ser...
May 03 23:49:04 node-c0706.exam.local systemd[1]: Starting The Apache HTTP Se...
May 03 23:49:04 node-c0706.exam.local systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
[root@node-c0706 ~]# 
実行後
ログに Reloading → Reloaded とあり、サービスがリロードしていることがわかる
[root@node-c0706 ~]# systemctl status httpd
 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-05-03 23:49:04 JST; 5min ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 11512 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 11605 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
 Main PID: 11516 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─11516 /usr/sbin/httpd -DFOREGROUND
           ├─11607 /usr/sbin/httpd -DFOREGROUND
           ├─11608 /usr/sbin/httpd -DFOREGROUND
           ├─11609 /usr/sbin/httpd -DFOREGROUND
           ├─11610 /usr/sbin/httpd -DFOREGROUND
           └─11611 /usr/sbin/httpd -DFOREGROUND

May 03 23:49:04 node-c0706.exam.local systemd[1]: Stopped The Apache HTTP Ser...
May 03 23:49:04 node-c0706.exam.local systemd[1]: Starting The Apache HTTP Se...
May 03 23:49:04 node-c0706.exam.local systemd[1]: Started The Apache HTTP Ser...
May 03 23:54:38 node-c0706.exam.local systemd[1]: Reloading The Apache HTTP S...
May 03 23:54:39 node-c0706.exam.local systemd[1]: Reloaded The Apache HTTP Se...
Hint: Some lines were ellipsized, use -l to show in full.
[root@node-c0706 ~]# 

カテゴリー

目次

QooQ