1
2

More than 5 years have passed since last update.

systemdにサービスを登録するAnsible role

Last updated at Posted at 2015-12-27

サービスsystemdへ登録するためのsystemd-serviceというAnsibleロールを書いたので公開します。Ubuntu Wily用に書いてますがsystemd_service_root_dirを設定すればRedHatにも対応できるはず。

インストール

ansible-galaxyで以下のようにします。

$ ansible-galaxy install tumf.systemd-service

プレイブック

プレイブックに以下の例ように書いてロールを実行します。

- hosts: servers
  roles:
    - role: systemd-service
      systemd_service_name: "swarm-manager"
      systemd_service_envs:
          - "DOCKER_HOST=tcp://127.0.0.1:2375"
      systemd_service_Unit_Description: Docker Swarm Manager
      systemd_service_Unit_Requires: docker.service
      systemd_service_Unit_After: docker.service
      systemd_service_Service_ExecStartPre:
          - -/usr/bin/docker stop swarm-manager
          - -/usr/bin/docker rm swarm-manager
          - /usr/bin/docker pull swarm
      systemd_service_Service_ExecStart: /usr/bin/docker run -p 2377:2375 --name swarm-manage

パラメータ

パラメータ名 デフォルト 説明
systemd_service_root_dir String "" デプロイ先のルートパス(注1)
systemd_service_default_dir String "/etc/default" 環境変数用パス
systemd_service_systemd_dir String "/etc/systemd/system" systemdのパス
systemd_service_name★ String サービス名
systemd_service_envs Array [] 環境変数 (/etc/default/:name)
systemd_service_Unit_Description String [Unit]のDescription
systemd_service_Unit_Requires String [Unit]のRequires
systemd_service_Unit_After String [Unit]のAfter
systemd_service_Service_Type String "simple" [Service]のType
systemd_service_Service_ExecStartPre Array [Service]のExecStartPre
systemd_service_Service_ExecStart★ String [Service]のExecStart
systemd_service_Service_ExecStartPost Array [Service]のExecStartPost
systemd_service_Install_WantedBy String "multi-user.target" [Install]のWantedBy

★必須

注1
systemd_service_root_dirによりデプロイ先を替えることによりユニットテストしやすくしています。

パラメータ自分が使う分しか用意していないので、適宜追加します。プルリクエストお待ちしております。

テスト(開発者向け)

以下のコマンドでローカルで単体テストを実行します。

./tests/run
1
2
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
1
2