てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible] service モジュールの基本的な使い方(サービスの起動・停止・自動起動の有効化など)

■ はじめに

Ansible には、sytemctlservice コマンドなどによるサービスの管理(起動、停止、再起動削除など)をする service モジュール があります。

この記事では、 service モジュールの公式ドキュメントに記載されている使用例をベースにして、使い方を説明します。

なお、公式ドキュメントの使用例は、Playbook 単位ではなくtask 単位で記載されています。この記事では Playbook 単位で例示します。

動作確認環境

  • Ansible 2.3.0, 2.7.8
  • CentOS 7.6 (Ansible 側、管理対象ホスト側とも)

目次


■ サービスが起動された状態にする

Playbook

apache httpd が起動された状態にする Playbook です。

- hosts: linux
  gather_facts: no
  become: yes

  tasks:
    - name: Start service httpd, if not started
      service:
        name: httpd
        state: started
  • name オプション
    • 対象のサービス名を指定します。(必須)
  • state オプション
    • name で指定したサービスどのような状態としたいかを指定します。
    • state: started を指定するとサービスが起動された状態になります。
      • 起動していない状態だった場合は、起動します。
        • systemctl start httpd のイメージ
      • 起動している状態だった場合は、何もしません。
      • start (起動そのものを指示)ではなく、started (起動された状態なっていることを指示)であることが、他のモジュール、オプションにも通じる特徴です。
      • 他に指定できる値(stopped など)については後述の使用例で説明します。

実行ログ

ここでは、サービスが起動していない状態で Playbook を実行します。

$ ansible-playbook -i inventory service_started.yml 

PLAY [linux] ***********************************************************************************

TASK [Start service httpd, if not started] *****************************************************
changed: [linux1]

PLAY RECAP *************************************************************************************
linux1                     : ok=1    changed=1    unreachable=0    failed=0   

$ sudo 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 Sun 2019-02-24 08:04:47 UTC; 2s ago
   (...略...)

Feb 24 08:04:47 centos7 systemd[1]: Starting The Apache HTTP Server...
Feb 24 08:04:47 centos7 httpd[14442]: AH00558: httpd: Could not reliably determine the ser...age
Feb 24 08:04:47 centos7 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

無事にサービスが起動された状態になりました。


■ サービスが停止された状態にする

Playbook

apache httpd が停止された状態にする Playbook です。

- hosts: linux
  gather_facts: no
  become: yes

  tasks:
    - name: Stop service httpd, if started
      service:
        name: httpd
        state: stopped
  • name オプション
    • 対象のサービス名を指定します。(必須)
  • state オプション
    • name で指定したサービスどのような状態としたいかを指定します。
    • state: stopped を指定するとサービスが起動された状態になります。
      • 起動している状態だった場合は、起動します。
        • systemctl stop httpd のイメージ
      • 起動していない状態だった場合は、何もしません。
  • enabled オプション(後述)
    • もし、自動起動を有効にしたい場合は、 enabled: yes も併用して指定します。本オプションの詳細は後述します。

実行ログ

ここでは、サービスが起動している状態で Playbook を実行します。

$ ansible-playbook -i inventory service_stopped.yml 

PLAY [linux] ***********************************************************************************

TASK [Stop service httpd, if started] **********************************************************
changed: [linux1]

PLAY RECAP *************************************************************************************
linux1                     : ok=1    changed=1    unreachable=0    failed=0   
(ansible2780) [vagrant@centos7 service]$ sudo 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)

Feb 24 07:55:54 centos7 systemd[1]: Starting The Apache HTTP Server...
Feb 24 07:55:54 centos7 httpd[14291]: AH00558: httpd: Could not reliably determine the ser...age
Feb 24 07:55:54 centos7 systemd[1]: Started The Apache HTTP Server.
Feb 24 08:04:16 centos7 systemd[1]: Stopping The Apache HTTP Server...
Feb 24 08:04:17 centos7 systemd[1]: Stopped The Apache HTTP Server.
Feb 24 08:04:47 centos7 systemd[1]: Starting The Apache HTTP Server...
Feb 24 08:04:47 centos7 httpd[14442]: AH00558: httpd: Could not reliably determine the ser...age
Feb 24 08:04:47 centos7 systemd[1]: Started The Apache HTTP Server.
Feb 24 08:06:34 centos7 systemd[1]: Stopping The Apache HTTP Server...
Feb 24 08:06:35 centos7 systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

無事にサービスが停止された状態になりました。


■ サービスが再起動された状態にする

Playbook

apache httpd が再起動された状態にする Playbook です。

- hosts: linux
  gather_facts: no
  become: yes

  tasks:
    - name: Restart service httpd, in all cases
      service:
        name: httpd
        state: restarted
  • name オプション
    • 対象のサービス名を指定します。
  • state オプション
    • state: restarted を指定すると、再起動された状態になります。
      • 起動している状態だった場合は、再起動(停止、起動)します。
        • systemctl restart httpd のイメージ
      • 起動していない状態だった場合は、起動します。
        • systemctl restart httpd のイメージ

実行ログ

ここでは、サービスが起動している状態で Playbook を実行します。

$ ansible-playbook -i inventory service_restarted.yml 

PLAY [linux] ***********************************************************************************

TASK [Restart service httpd, in all cases] *****************************************************
changed: [linux1]

PLAY RECAP *************************************************************************************
linux1                     : ok=1    changed=1    unreachable=0    failed=0   

(ansible2780) [vagrant@centos7 service]$ sudo 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 Sun 2019-02-24 08:13:09 UTC; 4s ago
   (...略...)

Feb 24 08:13:09 centos7 systemd[1]: Stopped The Apache HTTP Server.
Feb 24 08:13:09 centos7 systemd[1]: Starting The Apache HTTP Server...
Feb 24 08:13:09 centos7 httpd[14645]: AH00558: httpd: Could not reliably determine the ser...age
Feb 24 08:13:09 centos7 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

無事にサービスが再起動された状態になりました。(ログに StoppedStarting がある)

応用編: 再起動が必要な場合のみ再起動する

設定ファイルを変更した場合にのみ、サービスを再起動する、ということも指定できます。

以下の Playbook では、httpd.conf を管理対象ホスト側にコピーします。もし、そのコピーによって、httpd.conf が変更された場合(changed)だけ、handers 配下に定義された httpd_restarted というハンドラー(タスク)が呼び出され、サービスの再起動が実行されます。

- hosts: linux
  gather_facts: no
  become: yes

  tasks:
    - name: deploy httpd.conf
      copy:
        src: httpd.conf
        dest: /etc/httpd/conf/httpd.conf 
      notify: httpd_restarted   # handlers の httpd_restarted に対応

  handlers:
    - name: httpd_restarted  # notify の httpd_restarted に対応
      service:
        name: httpd
        state: restarted

以下に、2回分の実行ログを載せます。1回目は httpd.conf が変更されたため、ハンドラー経由でサービスが再起動しています。 続く2回目は、httpd.conf が前回実行分から変更されていないため、特に何もしていません。

$ ansible-playbook -i inventory service_restarted2.yml   # 1回目 (httpd.conf 変更あり)

PLAY [linux] ***********************************************************************************

TASK [deploy httpd.conf] ***********************************************************************
changed: [linux1]

RUNNING HANDLER [httpd_restarted] **************************************************************
changed: [linux1]

PLAY RECAP *************************************************************************************
linux1                     : ok=2    changed=2    unreachable=0    failed=0   

$ ansible-playbook -i inventory service_restarted2.yml  # 2回目 (httpd.conf 変更なし)

PLAY [linux] ***********************************************************************************

TASK [deploy httpd.conf] ***********************************************************************
ok: [linux1]

PLAY RECAP *************************************************************************************
linux1                     : ok=1    changed=0    unreachable=0    failed=0


■ サービスのコンフィグが再読み込みされた状態にする

Playbook

apache httpd のコンフィグが再読み込みされた状態にする Playbook です。

- hosts: linux
  gather_facts: no
  become: yes

  tasks:
    - name: Reload service httpd, in all cases
      service:
        name: httpd
        state: reloaded
  • name オプション
    • 対象のサービス名を指定します。
  • state オプション
    • state: reloaded を指定すると、コンフィグが再読み込みされた状態になります。
      • 起動している状態だった場合は、realod します。
        • systemctl reload httpd のイメージ
      • 起動していない状態だった場合は、起動します。
        • systemctl start httpd のイメージ

実行ログ

ここでは、サービスが起動している状態で Playbook を実行します。

$ ansible-playbook -i inventory service_reloaded.yml 

PLAY [linux] ***********************************************************************************

TASK [Reload service httpd, in all cases] ******************************************************
changed: [linux1]

PLAY RECAP *************************************************************************************
linux1                     : ok=1    changed=1    unreachable=0    failed=0   

$ sudo 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 Sun 2019-02-24 08:24:17 UTC; 7s ago
   (...略...)

Feb 24 08:24:17 centos7 systemd[1]: Stopped The Apache HTTP Server.
Feb 24 08:24:17 centos7 systemd[1]: Starting The Apache HTTP Server...
Feb 24 08:24:17 centos7 httpd[14978]: AH00558: httpd: Could not reliably determine the ser...age
Feb 24 08:24:17 centos7 systemd[1]: Started The Apache HTTP Server.
Feb 24 08:24:22 centos7 systemd[1]: Reloading The Apache HTTP Server.
Feb 24 08:24:22 centos7 httpd[15029]: AH00558: httpd: Could not reliably determine the ser...age
Feb 24 08:24:22 centos7 systemd[1]: Reloaded The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

無事にサービスが reload された状態になりました。(ログに Reloaded がある)


■ サービスの自動起動が有効化された状態にする

Playbook

apache httpd自動起動が有効化された状態にする Playbook です。

- hosts: linux
  gather_facts: no
  become: yes

  tasks:
    - name: Enable service httpd, and not touch the state
      service:
        name: httpd
        enabled: yes
  • name オプション
    • 対象のサービス名を指定します。
  • enabled オプション
    • state: restarted を指定すると、自動起動が有効化された状態になります。
      • 無効化されている状態だった場合は、有効化します。
        • systemctl enable httpd のイメージ
      • 有効化されている状態だった場合は、何もしません。
    • もし、enabled: no と指定した場合は、無効化された状態にします。(逆の動作)

実行ログ

ここでは、サービスの自動起動が無効化されている状態で Playbook を実行します。

$ ansible-playbook -i inventory service_enabled.yml  

PLAY [linux] ***********************************************************************************

TASK [Enable service httpd, and not touch the state] *******************************************
changed: [linux1]

PLAY RECAP *************************************************************************************
linux1                     : ok=1    changed=1    unreachable=0    failed=0   

(ansible2780) [vagrant@centos7 service]$ sudo 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 Sun 2019-02-24 08:26:41 UTC; 8min ago
   (...略...)

Feb 24 08:26:41 centos7 systemd[1]: Starting The Apache HTTP Server...
Feb 24 08:26:41 centos7 httpd[15161]: AH00558: httpd: Could not reliably determine the ser...age
Feb 24 08:26:41 centos7 systemd[1]: Started The Apache HTTP Server.
Feb 24 08:26:45 centos7 systemd[1]: Reloading The Apache HTTP Server.
Feb 24 08:26:45 centos7 httpd[15175]: AH00558: httpd: Could not reliably determine the ser...age
Feb 24 08:26:45 centos7 systemd[1]: Reloaded The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

無事にサービスの自動起動が有効化された状態になりました。(ログに httpd.service; enabled がある)


■ まとめ

公式ドキュメントの使用例をベースにして、service モジュール の使い方を説明しました。

他に「こんなことできるかな?」と気になる事がありましたら、公式ドキュメントで詳細をご確認ください。

docs.ansible.com

また、service モジュールは System modulesに分類されています。System modules には、他にもusertimezonehostnamefirewalld などのモジュールがあります。詳細は System modules の一覧からご確認ください。