#!/bin/bash
# {{ ansible_managed }}

declare -a arr=({{ containers | map(attribute='name') | flatten | map('regex_replace', '^(.*)$', '"\\1"') | flatten | join(' ') }})

for i in "${arr[@]}"; do
  output=$(docker{% if ansible_distribution_major_version|int == 9 %} {% else %}-{% endif %}compose -f /root/docker/$i/docker-compose.yml pull)

  if [[ $(echo $output | grep 'Downloaded newer image') ]]; then
    systemctl restart $i
  fi
done