From 82b0b0520cc5963f1d5a419f3b8eb50ca73b50e5 Mon Sep 17 00:00:00 2001 From: Elisabeth Deister Date: Tue, 12 Mar 2024 14:01:27 +0100 Subject: [PATCH 01/12] added support for static ip --- templates/systemd/container.service | 3 +++ 1 file changed, 3 insertions(+) diff --git a/templates/systemd/container.service b/templates/systemd/container.service index 59be71f..5327e0d 100644 --- a/templates/systemd/container.service +++ b/templates/systemd/container.service @@ -52,6 +52,9 @@ ExecStartPre=docker create --name={{ container.container_name }} --rm \ {%- endfor %} {%- if container.network|default(none) %} --network={{ container.network|quote }} \ + {%- endif %} + {%- if container.ip|default(none) %} + --ip={{ container.ip|quote }} \ {%- endif %} {%- if container.entrypoint|default(none) %} --entrypoint={{ container.entrypoint|quote }} \ From 2baa99b320da68fb772e4864b82af3f6290be52b Mon Sep 17 00:00:00 2001 From: Elisabeth Deister Date: Tue, 12 Mar 2024 14:22:10 +0100 Subject: [PATCH 02/12] fixed dependecy in network --- templates/systemd/container.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/systemd/container.service b/templates/systemd/container.service index 5327e0d..eefa7b9 100644 --- a/templates/systemd/container.service +++ b/templates/systemd/container.service @@ -53,7 +53,7 @@ ExecStartPre=docker create --name={{ container.container_name }} --rm \ {%- if container.network|default(none) %} --network={{ container.network|quote }} \ {%- endif %} - {%- if container.ip|default(none) %} + {%- if (container.ip|default(none)) and (container.network|default(none)) %} --ip={{ container.ip|quote }} \ {%- endif %} {%- if container.entrypoint|default(none) %} From 95b5e4e838073f73c97d0586abf155caf59f889f Mon Sep 17 00:00:00 2001 From: Wolfgang Medina-Erhardt Date: Wed, 13 Mar 2024 10:02:03 +0100 Subject: [PATCH 03/12] Edit README --- README.md | 70 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 7433327..b03af21 100644 --- a/README.md +++ b/README.md @@ -4,38 +4,56 @@ A Docker role which can be used to deploy Docker containers as SystemD services. Installs the latest version of Docker from the official repos -Compatible with Ubuntu 20.04 and 22.04 +Compatible with Ubuntu 20.04 & 22.04, as well as Debian 12. ### Usage -Create a template in the role that manages your docker container with the following contents: -``` -#jinja2: trim_blocks: False +The following example describes setting up a Keycloak container. + +For a full example, please refer to our [ansible-keycloak](https://github.com/punktDe/ansible-keycloak) role + +* Create a template in the role that manages your docker container with the following contents: +```jinja2 {%- import (role_path + "/../docker/templates/systemd/container.service")|relpath(playbook_dir) as service with context -%} -{{ service.All(example_container) }} +{{ service.All(keycloak) }} ``` -Configure the container parameters using Ansible variables -``` -example_container: - container_name: example - image: example:latest - container_stop_timeout: 55 - volumes: - "/etc/config.cfg": { host_dir: "/var/example/config.cfg", relabel: unshared, read_only: yes } - ports: - 8080: 80 - environment: - KEY: "value" - entrypoint: - /etc/entrypoint - command: - echo "hello world" +* Configure the container parameters using Ansible variables. You can add other arbitrary variables to the root of the `keycloak` dictionary (in this case, `domain` and `prefix`), and refer to them inside the same dictionary using the `vars.` prefix: +```yaml +keycloak: + domain: auth.example.com + prefix: + opt: /var/opt/keycloak + container_name: keycloak + image: quay.io/keycloak/keycloak:latest + container_stop_timeout: 55 + depends_on: + - postgresql + - nginx + volumes: + "/opt/keycloak/conf": + host_dir: "{{ vars.keycloak.prefix.opt | quote }}/conf" + relabel: unshared + read_only: yes + "/opt/keycloak/themes": + host_dir: "{{ vars.keycloak.prefix.opt | quote }}/current/themes" + "/opt/keycloak/providers": + host_dir: "{{ vars.keycloak.prefix.opt | quote }}/current/providers" + ports: + 127.0.0.1:8080: 8080 + environment: + KEYCLOAK_FRONTEND_URL: "https://{{ vars.keycloak.domain }}/auth" + KC_PROXY: "edge" + entrypoint: + /bin/kc.sh start-dev + command: + echo "hello world" ``` -Finally, provision the service file: -``` -- name: Install systemd service for example_container +* Finally, provision the service file: +```yaml +- name: Install systemd service for Keycloak template: - src: example_container.service - dest: "/etc/systemd/system/example_container.service" + src: keycloak.service + dest: "/etc/systemd/system/keycloak.service" + trim_blocks: no ``` From 8826c9f5d6e0ef1b9df1bcef0ec5cfca31a44a77 Mon Sep 17 00:00:00 2001 From: Wolfgang Medina-Erhardt Date: Tue, 21 May 2024 13:14:48 +0200 Subject: [PATCH 04/12] Modify testing workflow --- .github/workflows/test.yml | 11 +++-- README.md | 60 ++++++++++++++++++------ defaults/main.yaml | 1 + meta/.requirements.yml | 6 +++ molecule/default/molecule.yml | 3 +- requirements.txt | 6 +++ tasks/main.yaml | 4 ++ tasks/networks.yaml | 31 ++++++++++++ templates/docker-network.sh | 41 ++++++++++++++++ templates/systemd/container.service | 28 +++++++++-- templates/systemd/docker-network.service | 11 +++++ 11 files changed, 177 insertions(+), 25 deletions(-) create mode 100644 meta/.requirements.yml create mode 100644 requirements.txt create mode 100644 tasks/networks.yaml create mode 100644 templates/docker-network.sh create mode 100644 templates/systemd/docker-network.service diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 38d433c..25863ab 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,6 +5,10 @@ on: push: workflow_dispatch: +env: + ANSIBLE_ROLES_PATH: roles + ANSIBLE_HASH_BEHAVIOUR: merge + jobs: lint: runs-on: ubuntu-latest @@ -18,7 +22,7 @@ jobs: python-version: '3.x' - name: Install dependencies. - run: pip install netaddr yamllint ansible-lint ansible + run: pip install -r requirements.txt - name: Run ansible-lint run: "ansible-lint" @@ -37,10 +41,7 @@ jobs: - name: Install dependencies. run: | python -m pip install --upgrade pip - pip install netaddr ansible docker molecule molecule-plugins - - - name: Install Galaxy dependencies. - run: ansible-galaxy collection install community.docker + pip install -r requirements.txt - name: Run molecule run: "molecule test" diff --git a/README.md b/README.md index 7433327..748c2fa 100644 --- a/README.md +++ b/README.md @@ -15,27 +15,57 @@ Create a template in the role that manages your docker container with the follow ``` Configure the container parameters using Ansible variables -``` +```yaml example_container: - container_name: example - image: example:latest - container_stop_timeout: 55 - volumes: - "/etc/config.cfg": { host_dir: "/var/example/config.cfg", relabel: unshared, read_only: yes } - ports: - 8080: 80 - environment: - KEY: "value" - entrypoint: - /etc/entrypoint - command: - echo "hello world" + container_name: example + image: example:latest + container_stop_timeout: 55 + volumes: + "/etc/config.cfg": { host_dir: "/var/example/config.cfg", relabel: unshared, read_only: yes } + ports: + 8080: 80 + environment: + KEY: "value" + entrypoint: + /etc/entrypoint + command: + echo "hello world" ``` Finally, provision the service file: -``` +```yaml - name: Install systemd service for example_container template: src: example_container.service dest: "/etc/systemd/system/example_container.service" ``` + + +### Custom networks +This role can be used to create custom Docker networks in the following format: +```yaml +docker: + networks: + - name: example_network + subnet: 10.22.11.0/24 + - name: example_network_2 + subnet: 172.156.11.0/24 +``` + +The networks will then be created automatically on system boot using SystemD services. + +A container can then be connected to a network as follows: +```yaml +example_container: + network: + name: example_network + ip: 10.22.11.21 +``` + +If the appropriate network exists, its SystemD service will be added as a dependency to the container's service. + +Alternatively, if you'd like to omit the IP address (for example, with `host` network), the following structure can be used: +```yaml +example_container: + network: example_network +``` diff --git a/defaults/main.yaml b/defaults/main.yaml index f37b986..187a1dd 100644 --- a/defaults/main.yaml +++ b/defaults/main.yaml @@ -1,4 +1,5 @@ docker: + networks: [] repository: apt: "deb [arch={{ 'arm64' if ansible_architecture == 'aarch64' else 'amd64' }}] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable" key: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg" diff --git a/meta/.requirements.yml b/meta/.requirements.yml new file mode 100644 index 0000000..f6b3964 --- /dev/null +++ b/meta/.requirements.yml @@ -0,0 +1,6 @@ +--- +collections: + - name: https://github.com/ansible-collections/community.general + type: git + - name: https://github.com/ansible-collections/community.docker + type: git diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 59b98ae..b9be573 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -1,6 +1,7 @@ --- dependency: - name: galaxy + name: shell + command: ansible-galaxy install -r meta/.requirements.yml -p roles/ --force driver: name: docker platforms: diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..58d974d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +ansible +docker +molecule +molecule-plugins +ansible-lint +yamllint diff --git a/tasks/main.yaml b/tasks/main.yaml index d610a4d..3a81a99 100644 --- a/tasks/main.yaml +++ b/tasks/main.yaml @@ -10,3 +10,7 @@ - name: Configure DNS for docker ansible.builtin.include_tasks: dns.yaml + +- name: Configure docker networks + when: docker.networks + ansible.builtin.include_tasks: networks.yaml diff --git a/tasks/networks.yaml b/tasks/networks.yaml new file mode 100644 index 0000000..dfad4a2 --- /dev/null +++ b/tasks/networks.yaml @@ -0,0 +1,31 @@ +--- +- name: Create docker networks + loop: "{{ docker.networks }}" + register: docker_network_template_config_result + ansible.builtin.template: + src: systemd/docker-network.service + dest: "/etc/systemd/system/docker-network@{{ item.name }}" + owner: root + mode: "0644" + +- name: Template the docket-network script + ansible.builtin.file: + path: "/usr/local/bin/docker-network.sh" + src: "docker-network.sh" + mode: "0755" + owner: root + +- name: Activate the docker network services + loop: "{{ docker.networks }}" + ansible.builtin.service: + name: "docker-network@{{ item.name }}.service" + state: started + daemon_reload: yes + +- name: Reload docker-network@{{ item.name }}.service + loop: "{{ docker.networks }}" + when: docker_network_template_config_result.changed + ansible.builtin.service: + name: docker-network@{{ item.name }}.service + daemon_reload: yes + state: reloaded diff --git a/templates/docker-network.sh b/templates/docker-network.sh new file mode 100644 index 0000000..39500b4 --- /dev/null +++ b/templates/docker-network.sh @@ -0,0 +1,41 @@ +#! /usr/bin/env bash +action=$1 +network_name=$2 +subnet=$3 +driver=$4 + +tempfolder="/tmp/docker-networks" +mkdir -p $tempfolder + +case $action in + start) + docker network create --driver=$driver --subnet=$subnet $network_name + containers_in_network="$tempfolder/containers_in_network_$network_name" + if [ -f "$containers_in_network" ]; then + for i in `cat $containers_in_network`; do + echo $i + docker network connect $network_name $i; + done; + fi; + rm -f $containers_in_network + ;; + reload) + containers_in_network=`docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' $network_name` + for i in $containers_in_network; do + docker network disconnect -f $network_name $i; + done; + docker network rm $network_name + docker network create --driver=$driver --subnet=$subnet $network_name + for i in $containers_in_network; do + docker network connect $network_name $i; + done; + ;; + stop) + containers_in_network=`docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' $network_name` + echo $containers_in_network > "$tempfolder/containers_in_network_$network_name" + for i in $containers_in_network; do + docker network disconnect -f $network_name $i; + done; + docker network rm $network_name + ;; +esac diff --git a/templates/systemd/container.service b/templates/systemd/container.service index eefa7b9..1a3680f 100644 --- a/templates/systemd/container.service +++ b/templates/systemd/container.service @@ -50,12 +50,20 @@ ExecStartPre=docker create --name={{ container.container_name }} --rm \ {%- for variable, value in (container.environment.items()|rejectattr('1', 'eq', none) if container.environment|default(none) else []) %} --env={{ variable|quote }}={{ value|quote }} \ {%- endfor %} - {%- if container.network|default(none) %} - --network={{ container.network|quote }} \ + + {%- if container.network is mapping %} + {%- if container.network.name|default(none) %} + --network={{ container.network.name|quote }} \ + {%- endif %} + {%- if container.network.ip|default(none) %} + --ip={{ container.network.ip|quote }} \ {%- endif %} - {%- if (container.ip|default(none)) and (container.network|default(none)) %} - --ip={{ container.ip|quote }} \ + {%- else %} + {%- if container.network | default(none) %} + --network={{ container.network | quote }} \ {%- endif %} + {%- endif %} + {%- if container.entrypoint|default(none) %} --entrypoint={{ container.entrypoint|quote }} \ {%- endif %} @@ -76,6 +84,18 @@ WantedBy=docker.service {% macro All(container) -%} {{ Unit(container) }} +{%- if container.network | default(none) %} +{%- if container.network is mapping and (container.network.name | default(none)) %} +Requires={{ container.network.name }} +{%- else %} +{%- if container.network is not mapping and container.network != host %} +Requires={{ container.network }} +{% -endif %} + + +{% endif %} + + {{ Service(container) }} {{ Install(container) }} diff --git a/templates/systemd/docker-network.service b/templates/systemd/docker-network.service new file mode 100644 index 0000000..7cb67f8 --- /dev/null +++ b/templates/systemd/docker-network.service @@ -0,0 +1,11 @@ +[Unit] +Requires=docker.service +After=docker.service + +[Service] +Restart=always +RestartSec=10 +TimeoutStopSec=60 +ExecStart=/usr/local/bin/docker-network.sh start {{ item.name }} {{ item.subnet }} {{ item.driver | default("bridge") }} +ExecReload=/usr/local/bin/docker-network.sh reload {{ item.name }} {{ item.subnet }} {{ item.driver | default("bridge") }} +ExecStop=/usr/local/bin/docker-network.sh stop {{ item.name }} {{ item.subnet }} {{ item.driver | default("bridge") }} From 60288c32d15188c47948101e7365a1d23dcf1e8f Mon Sep 17 00:00:00 2001 From: Wolfgang Medina-Erhardt Date: Tue, 21 May 2024 13:44:53 +0200 Subject: [PATCH 05/12] Fix incorrect ansible module in networks.yaml --- tasks/networks.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/networks.yaml b/tasks/networks.yaml index dfad4a2..833c563 100644 --- a/tasks/networks.yaml +++ b/tasks/networks.yaml @@ -9,7 +9,7 @@ mode: "0644" - name: Template the docket-network script - ansible.builtin.file: + ansible.builtin.template: path: "/usr/local/bin/docker-network.sh" src: "docker-network.sh" mode: "0755" From 6b94fad35880e5e03bdd49f010d613f09cc54f83 Mon Sep 17 00:00:00 2001 From: Wolfgang Medina-Erhardt Date: Tue, 21 May 2024 13:47:00 +0200 Subject: [PATCH 06/12] Replace path with dest in a template task in networks.yaml --- tasks/networks.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/networks.yaml b/tasks/networks.yaml index 833c563..8a19764 100644 --- a/tasks/networks.yaml +++ b/tasks/networks.yaml @@ -10,7 +10,7 @@ - name: Template the docket-network script ansible.builtin.template: - path: "/usr/local/bin/docker-network.sh" + dest: "/usr/local/bin/docker-network.sh" src: "docker-network.sh" mode: "0755" owner: root From 9b3f7545e45fd3b867fe82d25118f312b0c2b0d8 Mon Sep 17 00:00:00 2001 From: Wolfgang Medina-Erhardt Date: Tue, 21 May 2024 13:49:28 +0200 Subject: [PATCH 07/12] Move the docker networks script from templates to files --- {templates => files}/docker-network.sh | 0 tasks/networks.yaml | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename {templates => files}/docker-network.sh (100%) diff --git a/templates/docker-network.sh b/files/docker-network.sh similarity index 100% rename from templates/docker-network.sh rename to files/docker-network.sh diff --git a/tasks/networks.yaml b/tasks/networks.yaml index 8a19764..4209a0e 100644 --- a/tasks/networks.yaml +++ b/tasks/networks.yaml @@ -9,9 +9,9 @@ mode: "0644" - name: Template the docket-network script - ansible.builtin.template: + ansible.builtin.copy: dest: "/usr/local/bin/docker-network.sh" - src: "docker-network.sh" + src: "files/docker-network.sh" mode: "0755" owner: root From 19dec2c499918f08559691e42c64d89f08134428 Mon Sep 17 00:00:00 2001 From: Wolfgang Medina-Erhardt Date: Tue, 21 May 2024 13:51:41 +0200 Subject: [PATCH 08/12] Fixup --- tasks/networks.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/networks.yaml b/tasks/networks.yaml index 4209a0e..aeeb748 100644 --- a/tasks/networks.yaml +++ b/tasks/networks.yaml @@ -1,10 +1,10 @@ --- -- name: Create docker networks +- name: Template the docker network creation services loop: "{{ docker.networks }}" register: docker_network_template_config_result ansible.builtin.template: src: systemd/docker-network.service - dest: "/etc/systemd/system/docker-network@{{ item.name }}" + dest: "/etc/systemd/system/docker-network@{{ item.name }}.service" owner: root mode: "0644" From f9dcc5d30179e819bb3550ab01b48cf6b1be7a1a Mon Sep 17 00:00:00 2001 From: Wolfgang Medina-Erhardt Date: Tue, 21 May 2024 13:53:58 +0200 Subject: [PATCH 09/12] Modify systemd service --- templates/systemd/docker-network.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/systemd/docker-network.service b/templates/systemd/docker-network.service index 7cb67f8..afbef12 100644 --- a/templates/systemd/docker-network.service +++ b/templates/systemd/docker-network.service @@ -3,7 +3,7 @@ Requires=docker.service After=docker.service [Service] -Restart=always +Type=simple RestartSec=10 TimeoutStopSec=60 ExecStart=/usr/local/bin/docker-network.sh start {{ item.name }} {{ item.subnet }} {{ item.driver | default("bridge") }} From 6b255be494555b9365dca2a08887137d88573bd8 Mon Sep 17 00:00:00 2001 From: Wolfgang Medina-Erhardt Date: Tue, 21 May 2024 14:20:22 +0200 Subject: [PATCH 10/12] Modify systemd services --- templates/systemd/container.service | 19 +++++++++++-------- templates/systemd/docker-network.service | 3 ++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/templates/systemd/container.service b/templates/systemd/container.service index 8153268..4b5ae76 100644 --- a/templates/systemd/container.service +++ b/templates/systemd/container.service @@ -51,16 +51,22 @@ ExecStartPre=docker create --name={{ container.container_name }} --rm \ --env={{ variable|quote }}={{ value|quote }} \ {%- endfor %} + + {%- if container.network | default(none) %} {%- if container.network is mapping %} + {%- if container.network.name|default(none) %} --network={{ container.network.name|quote }} \ {%- endif %} + {%- if container.network.ip|default(none) %} --ip={{ container.network.ip|quote }} \ {%- endif %} + {%- else %} - {%- if container.network | default(none) %} + --network={{ container.network | quote }} \ + {%- endif %} {%- endif %} @@ -89,14 +95,11 @@ WantedBy=docker.service {%- if container.network | default(none) %} {%- if container.network is mapping and (container.network.name | default(none)) %} -Requires={{ container.network.name }} +Requires=docker-network@{{ container.network.name }}.service {%- else %} -{%- if container.network is not mapping and container.network != host %} -Requires={{ container.network }} -{% -endif %} - - -{% endif %} +Requires=docker-network@{{ container.network }}.service +{%- endif %} +{%- endif %} {{ Service(container) }} diff --git a/templates/systemd/docker-network.service b/templates/systemd/docker-network.service index afbef12..823a37d 100644 --- a/templates/systemd/docker-network.service +++ b/templates/systemd/docker-network.service @@ -3,7 +3,8 @@ Requires=docker.service After=docker.service [Service] -Type=simple +Type=oneshot +RemainAfterExit=true RestartSec=10 TimeoutStopSec=60 ExecStart=/usr/local/bin/docker-network.sh start {{ item.name }} {{ item.subnet }} {{ item.driver | default("bridge") }} From a8376b6059183c4da50e1b802fbc11e0ddf4f142 Mon Sep 17 00:00:00 2001 From: Wolfgang Medina-Erhardt Date: Tue, 21 May 2024 14:59:09 +0200 Subject: [PATCH 11/12] Restore the IP addresses of containers correctly --- files/docker-network.sh | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/files/docker-network.sh b/files/docker-network.sh index 39500b4..fcdef5f 100644 --- a/files/docker-network.sh +++ b/files/docker-network.sh @@ -20,22 +20,33 @@ case $action in rm -f $containers_in_network ;; reload) - containers_in_network=`docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' $network_name` - for i in $containers_in_network; do - docker network disconnect -f $network_name $i; - done; + containers_in_network=`docker network inspect -f '{{range .Containers}}{{.Name}} {{.IPv4Address}} {{end}}' $network_name` + + while IFS= read -r line; do + container=`echo $line | awk '{print $1}'` + docker network disconnect -f $network_name $container; + done <<< "$containers_in_network" + docker network rm $network_name docker network create --driver=$driver --subnet=$subnet $network_name - for i in $containers_in_network; do - docker network connect $network_name $i; - done; + + while IFS= read -r line; do + container=`echo $line | awk '{print $1}'` + ip=`echo $line | awk '{print substr($2, 1, (length($2)-3))}'` + docker network connect $network_name $container --ip $ip; + done <<< "$containers_in_network" ;; stop) - containers_in_network=`docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' $network_name` + containers_in_network=`docker network inspect -f '{{range .Containers}}{{.Name}} {{.IPv4Address}} {{end}}' $network_name` + + echo $containers_in_network > "$tempfolder/containers_in_network_$network_name" - for i in $containers_in_network; do - docker network disconnect -f $network_name $i; - done; + + while IFS= read -r line; do + container=`echo $line | awk '{print $1}'` + docker network disconnect -f $network_name $container; + done <<< "$containers_in_network" + docker network rm $network_name ;; esac From 2d752b092c8f4f48667d5842b65d05534bb05ec4 Mon Sep 17 00:00:00 2001 From: Wolfgang Medina-Erhardt Date: Wed, 29 May 2024 15:49:54 +0200 Subject: [PATCH 12/12] Modify the script --- files/docker-network.sh | 32 +++++++++++++++++++------------- tasks/networks.yaml | 2 ++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/files/docker-network.sh b/files/docker-network.sh index fcdef5f..65873d0 100644 --- a/files/docker-network.sh +++ b/files/docker-network.sh @@ -22,19 +22,23 @@ case $action in reload) containers_in_network=`docker network inspect -f '{{range .Containers}}{{.Name}} {{.IPv4Address}} {{end}}' $network_name` - while IFS= read -r line; do - container=`echo $line | awk '{print $1}'` - docker network disconnect -f $network_name $container; - done <<< "$containers_in_network" + if [[ -n "$containers_in_network" ]]; then + while IFS= read -r line; do + container=`echo $line | awk '{print $1}'` + docker network disconnect -f $network_name $container; + done <<< "$containers_in_network" + fi docker network rm $network_name docker network create --driver=$driver --subnet=$subnet $network_name - while IFS= read -r line; do - container=`echo $line | awk '{print $1}'` - ip=`echo $line | awk '{print substr($2, 1, (length($2)-3))}'` - docker network connect $network_name $container --ip $ip; - done <<< "$containers_in_network" + if [[ -n "$containers_in_network" ]]; then + while IFS= read -r line; do + container=`echo $line | awk '{print $1}'` + ip=`echo $line | awk '{print substr($2, 1, (length($2)-3))}'` + docker network connect $network_name $container --ip $ip; + done <<< "$containers_in_network" + fi ;; stop) containers_in_network=`docker network inspect -f '{{range .Containers}}{{.Name}} {{.IPv4Address}} {{end}}' $network_name` @@ -42,10 +46,12 @@ case $action in echo $containers_in_network > "$tempfolder/containers_in_network_$network_name" - while IFS= read -r line; do - container=`echo $line | awk '{print $1}'` - docker network disconnect -f $network_name $container; - done <<< "$containers_in_network" + if [[ -n "$containers_in_network" ]]; then + while IFS= read -r line; do + container=`echo $line | awk '{print $1}'` + docker network disconnect -f $network_name $container; + done <<< "$containers_in_network" + fi docker network rm $network_name ;; diff --git a/tasks/networks.yaml b/tasks/networks.yaml index aeeb748..38cb5a5 100644 --- a/tasks/networks.yaml +++ b/tasks/networks.yaml @@ -17,8 +17,10 @@ - name: Activate the docker network services loop: "{{ docker.networks }}" + register: docker_network_activated ansible.builtin.service: name: "docker-network@{{ item.name }}.service" + enabled: yes state: started daemon_reload: yes