-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_setup.yml
122 lines (108 loc) · 3.87 KB
/
docker_setup.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
- name: Setup Docker with custom configuration from .dev folder (cross-platform)
hosts: localhost
become: true
vars:
project_root: "{{ ansible_env.PWD }}"
dev_folder: "{{ project_root }}/.dev"
docker_env_vars:
DOCKER_BUILDKIT: "1"
BUILDX_EXPERIMENTAL: "1"
BUILDKIT_MULTI_PLATFORM: "1"
COMPOSE_EXPERIMENTAL: "1"
BUILDKIT_PROGRESS: "plain"
DOCKER_CLI_EXPERIMENTAL: "enabled"
# DOCKER_BUILDX_PLATFORM: "linux/amd64"
PROJECT_DEBUG: "1"
docker_bash_script: "{{ project_root }}/app"
custom_daemon_json: "{{ project_root }}/config/daemon.json"
tasks:
- name: Ensure the .dev directory exists
ansible.builtin.file:
path: "{{ dev_folder }}"
state: directory
mode: "0755"
- name: Ensure the .dev/config directory exists
ansible.builtin.file:
path: "{{ dev_folder }}/config"
state: directory
mode: "0755"
- name: Install Docker (cross-platform check)
block:
- name: Install Docker on Ubuntu/WSL
ansible.builtin.apt:
name: docker.io
state: present
when: ansible_facts['os_family'] == "Debian"
- name: Install Docker on macOS
ansible.builtin.homebrew:
name: docker
state: present
when: ansible_facts['os_family'] == "Darwin"
- name: Add user to Docker group
ansible.builtin.user:
name: "{{ ansible_user_id }}"
groups: docker
append: true
become: true
- name: Ensure Buildx dependencies installed (cross-platform)
block:
- name: Install dependencies on Ubuntu/WSL
ansible.builtin.apt:
name:
- curl
- apt-transport-https
- ca-certificates
- software-properties-common
state: present
when: ansible_facts['os_family'] == "Debian"
- name: Install dependencies on macOS
ansible.builtin.homebrew:
name:
- curl
- ca-certificates
state: present
when: ansible_facts['os_family'] == "Darwin"
- name: Copy custom daemon.json from project config directory to .dev folder
copy:
src: "{{ custom_daemon_json }}"
dest: "{{ dev_folder }}/config/daemon.json"
# - name: Start Docker with custom configuration from project folder
# ansible.builtin.shell: |
# export DOCKER_CONFIG="{{ dev_folder }}/docker_config"
# mkdir -p {{ dev_folder }}/docker_config
# cp {{ dev_folder }}/config/daemon.json {{ dev_folder }}/docker_config/daemon.json
# sudo systemctl restart docker || sudo service docker restart
# environment:
# DOCKER_CONFIG: "{{ dev_folder }}/docker_config"
# become: true
# when: ansible_facts['os_family'] == "Debian"
- name: Ensure the shebang is at the top of docker_env.sh
lineinfile:
path: "{{ dev_folder }}/docker_env.sh"
create: yes
line: "#!/usr/bin/env bash"
insertafter: BOF
state: present
- name: Set Docker environment variables in the .dev environment
lineinfile:
path: "{{ dev_folder }}/docker_env.sh"
create: yes
line: "export {{ item.key }}={{ item.value }}"
loop: "{{ docker_env_vars | dict2items }}"
become: true
- name: Run the bash script with Docker environment variables and print output
ansible.builtin.shell: |
. {{ dev_folder }}/docker_env.sh && {{ docker_bash_script }}
environment: "{{ docker_env_vars | dict2items | items2dict }}"
become: true
register: script_output
- name: Print the output of the app script
ansible.builtin.debug:
msg: "{{ script_output.stdout }}"
handlers:
- name: Restart Docker
ansible.builtin.service:
name: docker
state: restarted
become: true