2020-10-02 06:07:29 +00:00
# Define an empty list for the mappings to be assigned to
- set_fact :
mappings : [ ]
tags : always
# Container name always equals the Dict name
2021-03-05 03:18:25 +00:00
- set_fact : container_Name="{{ item.value.name | default(item.key) }}"
2020-10-02 06:07:29 +00:00
tags : always
# Container hostname is the container name by default but can be overridden by setting 'hostname'
2021-03-05 03:18:25 +00:00
- set_fact : container_hostname="{{ item.value.hostname | default (container_Name) }}"
2020-10-02 06:07:29 +00:00
tags : always
2021-03-05 03:18:25 +00:00
- set_fact : container_configDir="{{ containerConfigDir }}/container-configs/{{ container_Name }}/"
2020-10-02 06:07:29 +00:00
tags : always
# Create the config storage directory if it doesnt already exist
- name : "Create config dir"
file :
2021-03-05 03:18:25 +00:00
path : "{{ container_configDir }}"
2020-10-02 06:07:29 +00:00
state : directory
tags : always
#----------------------------------Check NETWORK----------------------------------
#If the network varibale isnt defined then dont error out, set the network variable to an empty list
- name : Setup a blank network variable
set_fact : net_checked=[]
when : item.value.networks is not defined
tags : always
- name : Copy network variables into config
2021-03-05 03:18:25 +00:00
set_fact : net_checked="{{ item.value.networks }}"
2020-10-02 06:07:29 +00:00
when : item.value.networks is defined
tags : always
#- name: Show IP
# debug: msg="{{item.value.networks[0].ipv4_address | default(item.value.hostIP)}}"
#----------------------------------/Check NETWORK----------------------------------
#----------------------------------VOLUMES----------------------------------
#Do all of the defined mappings exist?
#Create them if not
- set_fact : volumes_Defined=0
tags : always
- name : Check if volumes have been defined or not
set_fact :
volumes_Defined : 1
when : item.value.mappings.volumes is defined
tags : always
2021-03-05 03:18:25 +00:00
- name : "Volume mappings for {{ container_Name }}"
2020-10-02 06:07:29 +00:00
include : dockerVolume.yml
2021-03-05 03:18:25 +00:00
with_dict : "{{ item.value.mappings.volumes }}"
2020-10-02 06:07:29 +00:00
when : volumes_Defined == 1
tags : always
#Note, the volume gets added to the mapping list in the dockerVolume.yml file becuase in there it knows the actual name of the volume
#----------------------------------/VOLUMES----------------------------------
#----------------------------------TEMPLATES----------------------------------
#Only copy template files if there are templates defined in the incoming defintion dict
- set_fact : templates_Defined=0
tags : always
- name : Check if templates have been defined or not
set_fact :
templates_Defined : 1
when : item.value.mappings.templates is defined
tags : always
#- debug: msg="Templates not defined"
# when: templates_Defined is not defined
#
2021-03-05 03:18:25 +00:00
#- debug: msg="{{ item.value.mappings.templates }}"
2020-10-02 06:07:29 +00:00
2021-03-05 03:18:25 +00:00
#Copy all of the defined templates to the container host into the {{ container_configDir }} ready for mapping to the container later
- name : "Template mappings for {{ container_Name }}"
2020-10-02 06:07:29 +00:00
#Files will be copied to the 'config' dir as defined by container_configDir, they will be stored using their template file name but with the '.j2' stripped
#Need to ensure you dont have duplicate files names here
2021-03-05 03:18:25 +00:00
template : src="{{ container_Name }}/{{ outer_item.value.src }}" dest="{{ container_configDir }}/{{ outer_item.value.src.split(".j2")[0] }}" mode=0644
with_dict : "{{ item.value.mappings.templates }}"
2020-10-02 06:07:29 +00:00
loop_control :
loop_var : outer_item
when : templates_Defined ==1 and outer_item.value.generic is not defined
2022-10-10 04:09:36 +00:00
register : file_changed
2020-10-02 06:07:29 +00:00
tags : always
2021-06-16 16:23:42 +00:00
- name : Set the flag to restart the container if file was changed
2022-10-10 04:09:36 +00:00
set_fact : container_restart='yes'
when : file_changed is defined and file_changed.changed
2021-06-16 16:23:42 +00:00
tags : always
2020-10-02 06:07:29 +00:00
#Copy templates form a generic dir, allow ansible to search
2021-03-05 03:18:25 +00:00
- name : "Generic template mappings for {{ container_Name }}"
2020-10-02 06:07:29 +00:00
#Files will be copied to the 'config' dir as defined by container_configDir, they will be stored using their template file name but with the '.j2' stripped
#Need to ensure you dont have duplicate files names here
2021-03-05 03:18:25 +00:00
template : src="{{ outer_item.value.src }}" dest="{{ container_configDir }}/{{ outer_item.value.src.split(".j2")[0] }}" mode=0644
with_dict : "{{ item.value.mappings.templates }}"
2020-10-02 06:07:29 +00:00
loop_control :
loop_var : outer_item
when : templates_Defined ==1 and outer_item.value.generic is defined
2022-10-10 04:09:36 +00:00
register : file_changed
2020-10-02 06:07:29 +00:00
tags : always
2021-06-16 16:23:42 +00:00
- name : Set the flag to restart the container if file was changed
2022-10-10 04:09:36 +00:00
set_fact : container_restart='yes'
when : file_changed is defined and file_changed.changed
2021-06-16 16:23:42 +00:00
tags : always
2020-10-02 06:07:29 +00:00
- name : Append templated files to the mapping list
set_fact :
#build a list to pass to docker containing the source of the file(As on disk on the host) and the destination mount within the container
#Also allows the passing of 'params' which is rw or ro, defaults to rw
#mappings: "{{ mappings + ['type=\"bind\", source=\"' + container_configDir + item.value.src.split('.j2')[0] + '\",target=\"' + item.value.dest + '\",readonly=\"' + item.value.readonly | default('false') +'\"'] }}"
mappings : "{{ mappings + [ container_configDir + outer_item.value.src.split('.j2')[0] + ':' + outer_item.value.dest + ':' + outer_item.value.params | default('rw') ] }}"
2021-03-05 03:18:25 +00:00
with_dict : "{{ item.value.mappings.templates }}"
2020-10-02 06:07:29 +00:00
loop_control :
loop_var : outer_item
when : templates_Defined ==1
tags : always
#type = "bind",target = "/usr/share/nginx/html",source = "/home/bfnadmin/www",readonly = false
#----------------------------------/TEMPLATES----------------------------------
#----------------------------------FILES----------------------------------
- set_fact : files_Defined=0
tags : always
#Only copy files if there are templates defined in the incoming defintion dict
- name : Check if Files have been defined or not
set_fact :
files_Defined : 1
when : item.value.mappings.files is defined
tags : always
2021-03-05 03:18:25 +00:00
#Copy all of the defined files to the container host into the {{ container_configDir }} ready for mapping to the container later
- name : "File mappings for {{ container_Name }}"
2020-10-02 06:07:29 +00:00
#Files will be copied to the 'config' dir as defined by container_configDir, they will be stored using their file name
#Need to ensure you dont have duplicate files names here
2021-03-05 03:18:25 +00:00
copy : src="{{ container_Name }}/{{ outer_item.value.src }}" dest="{{ container_configDir }}/{{ outer_item.key }}" mode=0644
with_dict : "{{ item.value.mappings.files }}"
2020-10-02 06:07:29 +00:00
loop_control :
loop_var : outer_item
when : files_Defined == 1 and outer_item.value.generic is not defined and outer_item.value.src_text is not defined
2022-10-10 04:09:36 +00:00
register : file_changed
2020-10-02 06:07:29 +00:00
tags : always
2021-06-16 16:23:42 +00:00
- name : Set the flag to restart the container if file was changed
2022-10-10 04:09:36 +00:00
set_fact : container_restart='yes'
when : file_changed is defined and file_changed.changed
2021-06-16 16:23:42 +00:00
tags : always
2020-10-02 06:07:29 +00:00
#Copy files from a generic dir, allow ansible to search
2021-03-05 03:18:25 +00:00
- name : "Generic File mappings for {{ container_Name }}"
2020-10-02 06:07:29 +00:00
#Files will be copied to the 'config' dir as defined by container_configDir, they will be stored using their file name
#Need to ensure you dont have duplicate files names here
2021-03-05 03:18:25 +00:00
copy : src="{{ outer_item.value.src }}" dest="{{ container_configDir }}/{{ outer_item.key }}" mode=0644
with_dict : "{{ item.value.mappings.files }}"
2020-10-02 06:07:29 +00:00
loop_control :
loop_var : outer_item
when : files_Defined == 1 and outer_item.value.generic is defined and outer_item.value.src_text is not defined
2022-10-10 04:09:36 +00:00
register : file_changed
2020-10-02 06:07:29 +00:00
tags : always
2021-06-16 16:23:42 +00:00
- name : Set the flag to restart the container if file was changed
2022-10-10 04:09:36 +00:00
set_fact : container_restart='yes'
when : file_changed is defined and file_changed.changed
2021-06-16 16:23:42 +00:00
tags : always
2022-10-10 04:09:36 +00:00
# Create files based on the suplied src_text param
2021-03-05 03:18:25 +00:00
- name : "Text based file creation for {{ container_Name }}"
2022-10-10 04:09:36 +00:00
# Files will be copied to the 'config' dir as defined by container_configDir, they will be stored using their file name
# Need to ensure you dont have duplicate files names here
2021-03-05 03:18:25 +00:00
copy : content="{{ outer_item.value.src_text }}" dest="{{ container_configDir }}/{{ outer_item.key }}" mode=0644
with_dict : "{{ item.value.mappings.files }}"
2020-10-02 06:07:29 +00:00
loop_control :
loop_var : outer_item
when : files_Defined == 1 and outer_item.value.src_text is defined
2022-10-10 04:09:36 +00:00
register : file_changed
2020-10-02 06:07:29 +00:00
tags : always
2021-06-16 16:23:42 +00:00
- name : Set the flag to restart the container if file was changed
2022-10-10 04:09:36 +00:00
set_fact : container_restart='yes'
when : file_changed is defined and file_changed.changed
2021-06-16 16:23:42 +00:00
tags : always
2020-10-02 06:07:29 +00:00
- name : Append static remote files to the mapping list
set_fact :
2022-10-10 04:09:36 +00:00
# build a list to pass to docker containing the source of the file(As on disk on the host) and the destination mount within the container
# Also allows the passing of 'params' which is rw or ro, defaults to rw
2020-10-02 06:07:29 +00:00
mappings : "{{ mappings + [ container_configDir + outer_item.key + ':' + outer_item.value.dest + ':' + outer_item.value.params | default('rw') ] }}"
2021-03-05 03:18:25 +00:00
with_dict : "{{ item.value.mappings.files }}"
2020-10-02 06:07:29 +00:00
loop_control :
loop_var : outer_item
when : files_Defined == 1
tags : always
2022-10-10 04:09:36 +00:00
# ----------------------------------/FILES----------------------------------
2020-10-02 06:07:29 +00:00
2022-10-10 04:09:36 +00:00
# ----------------------------------LOCAL----------------------------------
# Local files alreeady exist on the host, commonly local time and or netns for namespace, could also be locl disk drives?
# Nothing gets copied for local mappings, just defintiions added to the mapping list
# Only copy files if there are local files defined in the incoming defintion dict
2020-10-02 06:07:29 +00:00
- set_fact : local_Defined=0
tags : always
- name : Check if Local files have been defined or not
set_fact :
local_Defined : 1
when : item.value.mappings.local is defined
tags : always
2022-10-10 04:09:36 +00:00
# - debug: msg="local not defined"
2020-10-02 06:07:29 +00:00
# when: local_Defined is not defined
- name : Append static local files to the mapping list
set_fact :
2022-10-10 04:09:36 +00:00
# build a list to pass to docker containing the source of the file(As on disk on the host) and the destination mount within the container
# Also allows the passing of 'params' which is rw or ro, defaults to rw
# mappings: "{{ mappings + ['type=\"bind\", source=\"' + item.value.src + '\",target=\"' + item.value.dest + '\",readonly=\"' + item.value.readonly | default('false') +'\"'] }}"
2020-10-02 06:07:29 +00:00
mappings : "{{ mappings + [ outer_item.value.src + ':' + outer_item.value.dest + ':' + outer_item.value.params | default('rw') ] }}"
2021-03-05 03:18:25 +00:00
with_dict : "{{ item.value.mappings.local }}"
2020-10-02 06:07:29 +00:00
loop_control :
loop_var : outer_item
when : local_Defined == 1
2022-10-10 04:09:36 +00:00
register : file_changed
2020-10-02 06:07:29 +00:00
tags : always
2021-06-16 16:23:42 +00:00
- name : Set the flag to restart the container if file was changed
2022-10-10 04:09:36 +00:00
set_fact : container_restart='yes'
when : file_changed is defined and file_changed.changed
2021-06-16 16:23:42 +00:00
tags : always
2022-10-10 04:09:36 +00:00
# ----------------------------------/LOCAL----------------------------------
2020-10-02 06:07:29 +00:00
2022-10-10 04:09:36 +00:00
# ----------------------------------Check ENV----------------------------------
# If the env varibale isnt defined then dont error out, set the network variable to an empty list
2020-10-02 06:07:29 +00:00
- name : Setup a blank env variable
set_fact : env_checked={}
when : item.value.env is not defined
tags : always
- name : Copy env variables into config
2021-03-05 03:18:25 +00:00
set_fact : env_checked="{{ item.value.env }}"
2020-10-02 06:07:29 +00:00
when : item.value.env is defined
tags : always
2022-10-10 04:09:36 +00:00
# ----------------------------------/Check ENV----------------------------------
2020-10-02 06:07:29 +00:00
2022-10-10 04:09:36 +00:00
- name : Restart the container if file_changed is defined
set_fact : container_restart1='yes'
when : (container_restart is defined and container_restart=='yes') or (item.value.restart is defined and item.value.restart =='yes')
2020-10-02 06:07:29 +00:00
2021-03-05 03:18:25 +00:00
- name : "Container {{ container_Name }}"
2022-10-10 04:09:36 +00:00
become : true
2020-10-02 06:07:29 +00:00
docker_container :
2021-03-05 03:18:25 +00:00
name : "{{ container_Name }}"
hostname : "{{ item.value.hostname | default(container_Name) }}"
command : "{{ item.value.command | default('') }}"
pull : "{{ item.value.pull | default('false') }}"
recreate : "{{ item.value.recreate | default('false') }}"
image : "{{ item.value.image }}"
state : "{{ item.value.start | default ('started') }}"
2022-10-10 04:09:36 +00:00
restart : "{{ container_restart1 | default ('no') }}"
2021-03-05 03:18:25 +00:00
restart_policy : "{{ item.value.restart_policy | default ('always') }}"
purge_networks : "{{ item.value.purge_networks | default ('yes') }}"
networks : "{{ net_checked }}"
volumes : "{{ mappings }}"
env : "{{ env_checked }}"
privileged : "{{ item.value.privileged | default ('no') }}"
network_mode : "{{ item.value.network_mode | default ('default') }}"
shm_size : "{{ item.value.shm_size | default ('64M') }}"
ports : "{{ item.value.ports | default ([]) }}"
2022-10-10 13:48:19 +00:00
# ulimits: "{{ item.value.ulimits | default ('') }}"
2022-10-21 14:48:28 +00:00
devices : "{{ item.value.devices | default (None) }}"
2022-10-10 04:09:36 +00:00
tags : always