iac/ansible/playbooks/deploy-docker.yml

57 lines
1.6 KiB
YAML
Raw Normal View History

---
- name: Deploy application
hosts: "{{ target_host }}"
vars:
repo_path: "/home/{{ ansible_user }}/iac/docker/{{ folder }}"
2024-12-31 22:15:04 -05:00
secrets_mapping_file: "/home/{{ ansible_user }}/iac/secret-mappings.yml"
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({}) }}"
2024-12-31 22:22:40 -05:00
ansible.builtin.template:
src: env_template.j2
dest: "{{ repo_path }}/.env"
- name: Verify .env file content
ansible.builtin.shell: cat "{{ repo_path }}/.env"
2024-12-31 22:01:38 -05:00
register: env_file_content
2024-12-31 22:22:40 -05:00
- name: Display .env content
ansible.builtin.debug:
var: env_file_content.stdout_lines
2024-12-31 22:01:38 -05:00
- name: Ensure the repository is up-to-date
ansible.builtin.shell: git pull
args:
chdir: "{{ repo_path }}"
register: git_pull_output
- name: Display git pull output
ansible.builtin.debug:
var: git_pull_output.stdout_lines
- name: Restart services using Docker Compose
community.docker.docker_compose_v2:
project_src: "{{ repo_path }}"
state: present
remove_orphans: true
2024-12-31 22:01:38 -05:00
- name: Run Docker Command
command: docker compose ps
args:
chdir: "{{ repo_path }}"
2024-12-31 22:01:38 -05:00
register: docker_output
2024-12-31 22:01:38 -05:00
- name: Display Docker Output
debug:
var: docker_output.stdout_lines