2024-07-30 20:20:04 -04:00
|
|
|
---
|
|
|
|
- name: Deploy application
|
|
|
|
hosts: "{{ target_host }}"
|
|
|
|
vars:
|
2024-07-30 20:33:00 -04:00
|
|
|
repo_path: "/home/{{ ansible_user }}/iac/docker/{{ folder }}"
|
2024-12-31 22:01:38 -05:00
|
|
|
secrets_mapping_file: "/home/{{ ansible_user }}/iac/secret-mapping.yml"
|
2024-07-30 20:20:04 -04:00
|
|
|
tasks:
|
2024-12-31 22:01:38 -05:00
|
|
|
- name: Read secret mapping
|
|
|
|
ansible.builtin.slurp:
|
|
|
|
src: "{{ secrets_mapping_file }}"
|
|
|
|
register: secret_mapping_content
|
|
|
|
|
|
|
|
- name: Parse secret mapping
|
|
|
|
ansible.builtin.set_fact:
|
|
|
|
secret_mapping: "{{ secret_mapping_content['content'] | b64decode | from_yaml }}"
|
|
|
|
|
|
|
|
- name: Generate .env content
|
|
|
|
vars:
|
|
|
|
env_variables: "{{ secret_mapping[target_host]['env_variables'] | default({}) }}"
|
|
|
|
ansible.builtin.shell: |
|
|
|
|
#!/bin/bash
|
|
|
|
echo "Generating .env for {{ target_host }} at {{ repo_path }}/.env"
|
|
|
|
for var in "${!env_variables[@]}"; do
|
|
|
|
secret_id="${env_variables[$var]}"
|
|
|
|
if [ -n "$secret_id" ]; then
|
|
|
|
value=$(bws secret get "$secret_id" | jq -r '.value')
|
|
|
|
echo "$var=$value"
|
|
|
|
else
|
|
|
|
echo "$var="
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
args:
|
|
|
|
executable: /bin/bash
|
|
|
|
register: env_file_content
|
|
|
|
|
|
|
|
- name: Write .env file to target host
|
|
|
|
ansible.builtin.copy:
|
|
|
|
dest: "{{ repo_path }}/.env"
|
|
|
|
content: "{{ env_file_content.stdout }}"
|
|
|
|
mode: '0644'
|
|
|
|
|
2024-07-30 20:20:04 -04:00
|
|
|
- name: Ensure the repository is up-to-date
|
2024-12-19 21:44:47 -05:00
|
|
|
ansible.builtin.shell: git pull
|
2024-07-30 20:20:04 -04:00
|
|
|
args:
|
|
|
|
chdir: "{{ repo_path }}"
|
|
|
|
register: git_pull_output
|
|
|
|
|
|
|
|
- name: Display git pull output
|
2024-12-19 21:44:47 -05:00
|
|
|
ansible.builtin.debug:
|
2024-07-30 20:20:04 -04:00
|
|
|
var: git_pull_output.stdout_lines
|
|
|
|
|
2024-12-19 21:44:47 -05:00
|
|
|
- name: Restart services using Docker Compose
|
|
|
|
community.docker.docker_compose_v2:
|
|
|
|
project_src: "{{ repo_path }}"
|
|
|
|
state: present
|
|
|
|
remove_orphans: true
|
2024-07-30 20:20:04 -04:00
|
|
|
|
2024-12-31 22:01:38 -05:00
|
|
|
- name: Run Docker Command
|
2024-12-20 01:34:07 -05:00
|
|
|
command: docker compose ps
|
|
|
|
args:
|
|
|
|
chdir: "{{ repo_path }}"
|
2024-12-31 22:01:38 -05:00
|
|
|
register: docker_output
|
2024-12-20 01:20:15 -05:00
|
|
|
|
2024-12-31 22:01:38 -05:00
|
|
|
- name: Display Docker Output
|
|
|
|
debug:
|
|
|
|
var: docker_output.stdout_lines
|