13
13

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 5 years have passed since last update.

Ansible で Systemd の設定を書き換えた際の reload ハンドラの書き方

Last updated at Posted at 2016-04-16

とりあえず、手元の Ansible 2.0.0.2 の “service” モジュール(service - Manage services. — Ansible Documentation)では、自動的には全く systemctl daemon-reload をしてくれないので、Systemd の設定を書き換えた後に、勝手には反映してくれない。

2015 年の暮れくらいでも右記のようなやりとりが。まだ、特に Ansible 側に機能は無い模様。 ∥ make the system module call daemon-reload on systemctl control by oaken-source · Pull Request #2676 · ansible/ansible-modules-core

仕方がないので、下記のようなイディオムで対応します。SysV Init や Upstart では単純にハンドラで “service” に “restart” をさせますが、Systemd では二段構えのハンドラを使い、“daemon-reload“ をしてから “restart” をさせます。

handlers/main.yml
- name: Restart Docker (systemd)
  shell: systemctl daemon-reload
  notify: Restart Docker
- name: Restart Docker
  service: name=docker state=restarted

なお、Ansible では残念ながら、複文というか、一つのハンドラで二つ以上のモジュールは列挙して実行できないみたいなんだな(えー…)。 ∥ How do I write an Ansible handler with multiple tasks? - Stack Overflow

呼び方としては、以下のようになる。Docker サーバのストレージドライバとして、標準の aufs ではなく devicemapper を使わせる設定の、Upstart と Systemd 両対応。

tasks/main.yml
## For SysV Init and Upstart
- lineinfile:
    dest: "/etc/default/docker"
    regexp: '^DOCKER_OPTS='
    line: 'DOCKER_OPTS="--storage-driver=devicemapper"'
  notify: "Restart Docker"
## For Systemd
- file:
    dest: /etc/systemd/system/docker.service.d/
    state: directory
    mode: 0755
- copy:
    content: |
      [Service]
      ExecStart=
      ExecStart=/usr/bin/docker daemon -H fd:// --storage-driver=devicemapper
    dest: /etc/systemd/system/docker.service.d/default-file.conf
    mode: 0755
  notify: "Restart Docker (systemd)"
- meta: flush_handlers
- name: “devicemapper” is being used for storage driver
  shell: 'docker info 2>&1 | grep -q "Storage Driver: devicemapper"'
  changed_when: false

では。

13
13
1

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
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?