Skip to content

Commit 8308f96

Browse files
committed
Make volumes, ports and labels explicitly configurable
1 parent 9f82e87 commit 8308f96

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,28 @@ Generic role for creating systemd services to manage docker containers.
2020
```
2121
2222
This will create:
23-
- a file containing the env vars (either `/etc/sysconfig/mysql` or `/etc/default/mysql`)
24-
- a systemd unit which starts and stops the container
23+
24+
* A file containing the env vars (either `/etc/sysconfig/mysql` or `/etc/default/mysql`).
25+
* A systemd unit which starts and stops the container. The unit will be called
26+
`<name>_container.service` to avoid name clashes.
2527

2628
### Role variables
2729

2830
* `name` (**required**) - name of the service
31+
32+
#### Docker container specifics
33+
2934
* `image` (**required**) - Docker image the service uses
30-
* `args` - arbitrary list of arguments to the `docker run` command; Put port
31-
bindings, mounts etc here
35+
* `args` - arbitrary list of arguments to the `docker run` command
36+
* `cmd` - optional command to the container run command (the part after the
37+
image name)
3238
* `env` - key/value pairs of ENV vars that need to be present
39+
* `volumes` (default: _[]_) - List of `-v` arguments
40+
* `ports` (default: _[]_) - List of `-p` arguments
41+
* `labels` (default: _[]_) - List of `-l` arguments
42+
43+
#### Systemd service specifics
44+
3345
* `enabled` (default: _yes_) - whether the service should be enabled
3446
* `masked` (default: _no_) - whether the service should be masked
3547
* `state` (default: _started_) - state the service should be in

defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
---
2+
volumes: []
3+
labels: []
4+
ports: []
25
enabled: yes
36
masked: no
47
state: started

templates/unit.j2

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# {{ ansible_managed }}
2+
{% macro params(name, vals) %}
3+
{% for v in vals %}-{{ name }} {{ v }} {% endfor %}
4+
{% endmacro %}
15
[Unit]
26
After=docker.service
37
PartOf=docker.service
@@ -8,7 +12,7 @@ Requires=docker.service
812
EnvironmentFile={{ sysconf_dir }}/{{ name }}
913
{% endif %}
1014
ExecStartPre=-/usr/bin/docker rm -f {{ name }}
11-
ExecStart=/usr/bin/docker run --name {{ name }} --rm {% if env is defined %}--env-file {{ sysconf_dir }}/{{ name }}{% endif %} {{ (args | trim) if args is defined }} {{ image }}
15+
ExecStart=/usr/bin/docker run --name {{ name }} --rm {% if env is defined %}--env-file {{ sysconf_dir }}/{{ name }} {% endif %}{{ params('v', volumes) }}{{ params('p', ports) }}{{ params('l', labels) }}{{ args | default('') |trim }} {{ image }} {{ cmd | default('') | trim }}
1216
ExecStop=/usr/bin/docker stop {{ name }}
1317

1418
SyslogIdentifier={{ name }}

0 commit comments

Comments
 (0)