diff --git a/roles/registrator/defaults/main.yml b/roles/registrator/defaults/main.yml new file mode 100644 index 0000000..ee1f39e --- /dev/null +++ b/roles/registrator/defaults/main.yml @@ -0,0 +1,7 @@ +registrator_service: registrator + +registrator_container_name: "{{registrator_service}}" +registrator_docker_image: progrium/registrator +registrator_docker_tag: latest + +registrator_consul_connect: "consul://consul:8500" diff --git a/roles/registrator/handlers/main.yml b/roles/registrator/handlers/main.yml new file mode 100644 index 0000000..fa067ef --- /dev/null +++ b/roles/registrator/handlers/main.yml @@ -0,0 +1,6 @@ +--- +- name: systemd daemon-reload + sudo: yes + command: /bin/systemctl daemon-reload + tags: + - systemd-daemon-reload diff --git a/roles/registrator/tasks/main.yml b/roles/registrator/tasks/main.yml index 9878c01..f66c0e0 100644 --- a/roles/registrator/tasks/main.yml +++ b/roles/registrator/tasks/main.yml @@ -1,12 +1,21 @@ --- -- name: run registrator container +- name: generate registrator systemd service file sudo: yes - docker: - name: registrator - image: progrium/registrator:latest - state: running - hostname: "{{ inventory_hostname }}" - volumes: /var/run/docker.sock:/tmp/docker.sock - privileged: yes - links: consul:consul - command: consul://consul:8500 + template: + src: registrator.service.j2 + dest: "/usr/lib/systemd/system/{{registrator_service}}.service" + notify: systemd daemon-reload + register: registrator_unit + tags: + - registrator + - registrator-docker-create-systemd-sevice + +- name: register registrator service with systemd + sudo: yes + service: + enabled: yes + name: "{{registrator_service}}" + state: started + tags: + - registrator + - registrator-docker-systemd-start diff --git a/roles/registrator/templates/registrator.service.j2 b/roles/registrator/templates/registrator.service.j2 new file mode 100644 index 0000000..abed09a --- /dev/null +++ b/roles/registrator/templates/registrator.service.j2 @@ -0,0 +1,24 @@ +[Unit] +Description=registrator +Documentation=https://github.com/progrium/registrator +After=docker.service +After=dnsmasq.service +Requires=docker.service +Requires=dnsmasq.service + +[Service] +Restart=on-failure + +ExecStart=/bin/bash -c \ + "/usr/bin/docker start {{registrator_container_name}} || \ + /usr/bin/docker run --name={{registrator_container_name}} \ + --link consul:consul \ + --privileged=true \ + -v /var/run/docker.sock:/tmp/docker.sock \ + -h {{inventory_hostname}} \ + {{registrator_docker_image }}:{{registrator_docker_tag}} {{registrator_consul_connect}}" + +ExecStop=/usr/bin/docker kill {{registrator_container_name}} + +[Install] +WantedBy=multi-user.target