--- # Deployment Playbook for Debian Linux # This playbook deploys applications and services - name: Deploy applications on Debian Linux hosts: alpine become: yes gather_facts: yes tasks: - name: Update apt package index apt: update_cache: yes cache_valid_time: 3600 - name: Install Docker apt: name: - docker.io - docker-compose state: present - name: Add vagrant user to docker group user: name: vagrant groups: docker append: yes - name: Start and enable Docker service systemd: name: docker state: started enabled: yes - name: Test Docker installation command: docker --version register: docker_version changed_when: false - name: Show Docker version debug: msg: "{{ docker_version.stdout }}" - name: Pull a lightweight test image docker_image: name: alpine:latest source: pull - name: Run a test container docker_container: name: test-container image: alpine:latest command: echo "Docker is working on {{ inventory_hostname }}!" state: present auto_remove: yes - name: Create application directory file: path: /opt/app state: directory mode: '0755' - name: Create sample application copy: content: | #!/bin/bash echo "Hello from {{ inventory_hostname }}!" echo "Running on Debian Linux" echo "Memory: $(free -m | grep Mem | awk '{print $2}')MB" echo "Disk: $(df -h / | tail -1 | awk '{print $2}')" dest: /opt/app/hello.sh mode: '0755' - name: Create systemd service for sample app copy: content: | [Unit] Description=Sample Application After=network.target [Service] Type=simple User=vagrant ExecStart=/opt/app/hello.sh Restart=always RestartSec=10 [Install] WantedBy=multi-user.target dest: /etc/systemd/system/sample-app.service mode: '0644' - name: Reload systemd daemon systemd: daemon_reload: yes - name: Enable sample application service systemd: name: sample-app enabled: yes state: started - name: Check service status command: systemctl status sample-app register: service_status changed_when: false ignore_errors: yes - name: Show service status debug: msg: "{{ service_status.stdout_lines }}" - name: Create deployment info file copy: content: | Deployment completed on {{ inventory_hostname }} Date: {{ ansible_date_time.iso8601 }} OS: {{ ansible_distribution }} {{ ansible_distribution_version }} Architecture: {{ ansible_architecture }} Memory: {{ ansible_memtotal_mb }}MB Docker: {{ docker_version.stdout }} dest: /opt/app/deployment-info.txt mode: '0644' - name: Display deployment info command: cat /opt/app/deployment-info.txt register: deployment_info changed_when: false - name: Show deployment info debug: msg: "{{ deployment_info.stdout_lines }}"