Skip to content

Commit

Permalink
Add support for --device and --privileged
Browse files Browse the repository at this point in the history
  • Loading branch information
a0s committed Jun 26, 2020
1 parent df92e26 commit 4f717ef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ This will create:
* `container_docker_pull` (default: _yes_) - whether the docker image should be pulled
* `container_cap_add` (default _[]_) - List of capabilities to add
* `container_cap_drop` (default _{}_) - List of capabilities to drop
* `container_devices` (default _[]_) - List of devices to add
* `container_privileged` (default _false_) - Whether the container should be privileged
* `container_start_post` - Optional command to be run by systemd after the container has started

#### Systemd service specifics
Expand Down
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ container_ports: []
container_volumes: []
container_cap_add: []
container_cap_drop: []
container_devices: []
container_privileged: false
docker_path: "/usr/bin/docker"
service_name: "{{ container_name }}_container"
service_systemd_options: []
Expand Down
17 changes: 15 additions & 2 deletions templates/unit.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# {{ ansible_managed }}
{% macro params(name, vals) %}
{% for v in vals %}-{{ name }} {{ v }} {% endfor %}
{% for v in vals %}{{ name }} {{ v }} {% endfor %}
{% endmacro %}
{% set service_systemd_options_keys = service_systemd_options | selectattr("key") | map(attribute="key") | list %}
[Unit]
Expand All @@ -21,7 +21,20 @@ EnvironmentFile={{ sysconf_dir }}/{{ container_name }}
ExecStartPre=-{{ docker_path }} rm -f {{ container_name }}
{% endif %}
{% if not 'ExecStart' in service_systemd_options_keys %}
ExecStart={{ docker_path }} run --name {{ container_name }} --rm {% if container_env is defined %}--env-file {{ sysconf_dir }}/{{ container_name }} {% endif %}{{ params('v', container_volumes) }}{% if container_host_network == true %} --network host {% else %}{{ params('p', container_ports) }}{% endif %}{{ params('-link', container_links) }}{{ params('l', container_labels) }}{{ params('-cap-add', container_cap_add) }}{{ params('-cap-drop', container_cap_drop) }}{{ container_args | default('') |trim }} {{ container_image }} {{ container_cmd | default('') | trim }}
ExecStart={{ docker_path }} run \
--name {{ container_name }} \
--rm \
{% if container_env is defined %}--env-file {{ sysconf_dir }}/{{ container_name }} {% endif %}\
{{ params('--volume', container_volumes) }}\
{% if container_host_network == true %}--network host {% else %}{{ params('--publish', container_ports) }}{% endif %}\
{{ params('--link', container_links) }}\
{{ params('--label', container_labels) }}\
{{ params('--cap-add', container_cap_add) }}\
{{ params('--cap-drop', container_cap_drop) }}\
{{ params('--device', container_devices) }}\
{% if container_privileged == true %}--privileged{% endif %}\
{{ container_args | default('') | trim }} \
{{ container_image }} {{ container_cmd | default('') | trim }}
{% endif %}
{% if not 'ExecStop' in service_systemd_options_keys %}
ExecStop=/usr/bin/docker stop {{ container_name }}
Expand Down

0 comments on commit 4f717ef

Please sign in to comment.