Skip to content

Commit 57e787f

Browse files
committedJan 1, 2025
init add docker bws
1 parent 5a76b83 commit 57e787f

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed
 

‎ansible/playbooks/deploy-docker.yml

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,42 @@
33
hosts: "{{ target_host }}"
44
vars:
55
repo_path: "/home/{{ ansible_user }}/iac/docker/{{ folder }}"
6+
secrets_mapping_file: "/home/{{ ansible_user }}/iac/secret-mapping.yml"
67
tasks:
8+
- name: Read secret mapping
9+
ansible.builtin.slurp:
10+
src: "{{ secrets_mapping_file }}"
11+
register: secret_mapping_content
12+
13+
- name: Parse secret mapping
14+
ansible.builtin.set_fact:
15+
secret_mapping: "{{ secret_mapping_content['content'] | b64decode | from_yaml }}"
16+
17+
- name: Generate .env content
18+
vars:
19+
env_variables: "{{ secret_mapping[target_host]['env_variables'] | default({}) }}"
20+
ansible.builtin.shell: |
21+
#!/bin/bash
22+
echo "Generating .env for {{ target_host }} at {{ repo_path }}/.env"
23+
for var in "${!env_variables[@]}"; do
24+
secret_id="${env_variables[$var]}"
25+
if [ -n "$secret_id" ]; then
26+
value=$(bws secret get "$secret_id" | jq -r '.value')
27+
echo "$var=$value"
28+
else
29+
echo "$var="
30+
fi
31+
done
32+
args:
33+
executable: /bin/bash
34+
register: env_file_content
35+
36+
- name: Write .env file to target host
37+
ansible.builtin.copy:
38+
dest: "{{ repo_path }}/.env"
39+
content: "{{ env_file_content.stdout }}"
40+
mode: '0644'
41+
742
- name: Ensure the repository is up-to-date
843
ansible.builtin.shell: git pull
944
args:
@@ -20,12 +55,12 @@
2055
state: present
2156
remove_orphans: true
2257

23-
- name: Run Docker Command
58+
- name: Run Docker Command
2459
command: docker compose ps
2560
args:
2661
chdir: "{{ repo_path }}"
27-
register: docker_output
62+
register: docker_output
2863

29-
- name: Display Docker Output
30-
debug:
31-
var: docker_output.stdout_lines
64+
- name: Display Docker Output
65+
debug:
66+
var: docker_output.stdout_lines

0 commit comments

Comments
 (0)
Please sign in to comment.