Skip to content

Commit 1e4049b

Browse files
authored
Merge pull request #3 from mhutter/renamevars
Prefix role variable names
2 parents a3e1453 + 22495f0 commit 1e4049b

File tree

6 files changed

+40
-36
lines changed

6 files changed

+40
-36
lines changed

README.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ Generic role for creating systemd services to manage docker containers.
1010
name: docker-systemd-service
1111
vars:
1212
name: myapp
13-
image: myapp:latest
14-
args: >
15-
--link mysql
16-
-v /data/uploads:/data/uploads
17-
-p 3000:3000
18-
env:
13+
container_image: myapp:latest
14+
container_links: [ 'mysql' ]
15+
container_volumes:
16+
- '/data/uploads:/data/uploads'
17+
container_ports:
18+
- '3000:3000'
19+
container_env:
1920
MYSQL_ROOT_PASSWORD: "{{ mysql_root_pw }}"
2021
```
2122
@@ -27,20 +28,20 @@ This will create:
2728

2829
### Role variables
2930

30-
* `name` (**required**) - name of the service
31+
* `name` (**required**) - name of the container
3132

3233
#### Docker container specifics
3334

34-
* `image` (**required**) - Docker image the service uses
35-
* `args` - arbitrary list of arguments to the `docker run` command
36-
* `cmd` - optional command to the container run command (the part after the
35+
* `container_image` (**required**) - Docker image the service uses
36+
* `container_args` - arbitrary list of arguments to the `docker run` command
37+
* `container_cmd` - optional command to the container run command (the part after the
3738
image name)
38-
* `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-
* `link` (default: _[]_) - List of `--link` arguments
42-
* `labels` (default: _[]_) - List of `-l` arguments
43-
* `docker_pull` (default: _yes_) - whether the docker image should be pulled
39+
* `container_env` - key/value pairs of ENV vars that need to be present
40+
* `container_volumes` (default: _[]_) - List of `-v` arguments
41+
* `container_ports` (default: _[]_) - List of `-p` arguments
42+
* `container_link` (default: _[]_) - List of `--link` arguments
43+
* `container_labels` (default: _[]_) - List of `-l` arguments
44+
* `container_docker_pull` (default: _yes_) - whether the docker image should be pulled
4445

4546
#### Systemd service specifics
4647

@@ -64,6 +65,9 @@ and run `ansible-galaxy install -r requirements.yml`.
6465

6566
* When the unit or env file is changed, systemd gets reloaded but existing
6667
containers are NOT restarted.
68+
* Make sure to quote values for `container_ports`, `container_volumes` and so
69+
on, especially if they contain colons (`:`). Otherwise YAML will interpret
70+
them as hashes/maps and ansible will throw up.
6771

6872
## About orchestrating Docker containers using systemd.
6973

defaults/main.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
volumes: []
3-
labels: []
4-
ports: []
5-
links: []
6-
enabled: yes
7-
masked: no
8-
state: started
9-
docker_pull: yes
2+
container_docker_pull: yes
3+
container_labels: []
4+
container_links: []
5+
container_ports: []
6+
container_volumes: []
7+
service_enabled: yes
8+
service_masked: no
9+
service_state: started

tasks/install.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
owner: root
77
group: root
88
mode: '0600'
9-
when: env is defined
9+
when: container_env is defined
1010

1111
# use `command` instead of `docker_image` so we don't have to install docker-py
12-
- name: Pull image {{ image }}
13-
command: docker pull {{ image }}
14-
when: docker_pull
12+
- name: Pull image {{ container_image }}
13+
command: docker pull {{ container_image }}
14+
when: container_docker_pull
1515

1616
# TODO: Add handler to restart service after new image has been pulled
1717
- name: Create unit {{ name }}_container.service
@@ -26,6 +26,6 @@
2626
systemd:
2727
name: '{{ name }}_container.service'
2828
daemon_reload: true
29-
enabled: "{{ enabled }}"
30-
masked: "{{ masked }}"
31-
state: "{{ state }}"
29+
enabled: "{{ container_enabled }}"
30+
masked: "{{ container_masked }}"
31+
state: "{{ container_state }}"

tasks/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
tags: always
55

66
- include: install.yml
7-
when: state != "absent"
7+
when: service_state != "absent"
88
- include: uninstall.yml
9-
when: state == "absent"
9+
when: service_state == "absent"

templates/env.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
{% for k,v in env.iteritems() %}
1+
{% for k,v in container_env.iteritems() %}
22
{{ k }}={{ v }}
33
{% endfor %}

templates/unit.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ PartOf=docker.service
88
Requires=docker.service
99

1010
[Service]
11-
{% if env is defined %}
11+
{% if container_env is defined %}
1212
EnvironmentFile={{ sysconf_dir }}/{{ name }}
1313
{% endif %}
1414
ExecStartPre=-/usr/bin/docker rm -f {{ name }}
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('-link', links) }}{{ params('l', labels) }}{{ args | default('') |trim }} {{ image }} {{ cmd | default('') | trim }}
15+
ExecStart=/usr/bin/docker run --name {{ name }} --rm {% if container_env is defined %}--env-file {{ sysconf_dir }}/{{ name }} {% endif %}{{ params('v', container_volumes) }}{{ params('p', container_ports) }}{{ params('-link', container_links) }}{{ params('l', container_labels) }}{{ container_args | default('') |trim }} {{ container_image }} {{ container_cmd | default('') | trim }}
1616
ExecStop=/usr/bin/docker stop {{ name }}
1717

1818
SyslogIdentifier={{ name }}

0 commit comments

Comments
 (0)