diff --git a/proxmox-docker-install.yml b/proxmox-docker-install.yml index 42ba26b..995d1f5 100644 --- a/proxmox-docker-install.yml +++ b/proxmox-docker-install.yml @@ -1,20 +1,28 @@ --- +# Play 1: runs locally on the Semaphore host and creates a runtime target - name: Build runtime target from Survey variables (Semaphore v2) hosts: localhost gather_facts: false + tasks: - name: Validate required inputs ansible.builtin.assert: that: - vm_ip is defined - fail_msg: "Missing vm_ip in Survey." + - vm_user is defined + - vm_pass is defined + fail_msg: "Missing required vars. Supply vm_ip, vm_user, vm_pass in the Task Template Survey." - - name: Add VM as in-memory inventory host + - name: Add VM as in-memory inventory host (force password SSH) ansible.builtin.add_host: name: target_vm groups: runtime ansible_host: "{{ vm_ip }}" - # user/pass will come from Semaphore credentials + ansible_user: "{{ vm_user }}" + ansible_password: "{{ vm_pass }}" + ansible_become: true + ansible_become_method: sudo + ansible_become_password: "{{ vm_pass }}" ansible_ssh_common_args: >- -o PreferredAuthentications=password -o PubkeyAuthentication=no @@ -24,39 +32,103 @@ - name: Show connection target ansible.builtin.debug: - msg: "Targeting {{ vm_ip }} using PASSWORD SSH (credentials from Semaphore inventory)" + msg: "Targeting {{ vm_user }}@{{ vm_ip }} using PASSWORD SSH" +# Play 2: connects to the chosen VM and installs Docker +- name: Install Docker on chosen VM + hosts: runtime + become: true + gather_facts: true -- name: Get system architecture - ansible.builtin.command: dpkg --print-architecture - register: deb_arch - changed_when: false + tasks: + - name: Update apt cache + ansible.builtin.apt: + update_cache: true -- name: Download Docker GPG key - ansible.builtin.get_url: - url: https://download.docker.com/linux/ubuntu/gpg - dest: /etc/apt/keyrings/docker.gpg - mode: "0644" + - name: Install prerequisites + ansible.builtin.apt: + name: + - ca-certificates + - curl + - gnupg + - lsb-release + state: present -- name: Add Docker apt repository - ansible.builtin.apt_repository: - repo: >- - deb [arch={{ deb_arch.stdout }} signed-by=/etc/apt/keyrings/docker.gpg] - https://download.docker.com/linux/ubuntu - {{ ansible_distribution_release }} stable - state: present - filename: docker + - name: Ensure /etc/apt/keyrings exists + ansible.builtin.file: + path: /etc/apt/keyrings + state: directory + mode: "0755" -- name: Update apt cache after Docker repo - ansible.builtin.apt: - update_cache: yes + - name: Get Debian architecture (amd64/arm64/etc) + ansible.builtin.command: dpkg --print-architecture + register: deb_arch + changed_when: false -- name: Install Docker engine and plugins - ansible.builtin.apt: - name: - - docker-ce - - docker-ce-cli - - containerd.io - - docker-buildx-plugin - - docker-compose-plugin - state: present + - name: Download Docker GPG key (keyring) + ansible.builtin.get_url: + url: https://download.docker.com/linux/ubuntu/gpg + dest: /etc/apt/keyrings/docker.gpg + mode: "0644" + + - name: Add Docker apt repository + ansible.builtin.apt_repository: + repo: >- + deb [arch={{ deb_arch.stdout }} signed-by=/etc/apt/keyrings/docker.gpg] + https://download.docker.com/linux/ubuntu + {{ ansible_distribution_release }} stable + state: present + filename: docker + + - name: Update apt cache after adding Docker repo + ansible.builtin.apt: + update_cache: true + + # If docker-ce still isn't available on some releases/arches, we fall back to docker.io + - name: Try install Docker CE + plugins (preferred) + ansible.builtin.apt: + name: + - docker-ce + - docker-ce-cli + - containerd.io + - docker-buildx-plugin + - docker-compose-plugin + state: present + register: docker_ce_install + ignore_errors: true + + - name: Fallback to Ubuntu docker.io if Docker CE not available + ansible.builtin.apt: + name: + - docker.io + - docker-compose-plugin + state: present + when: docker_ce_install is failed + + - name: Enable and start Docker + ansible.builtin.systemd: + name: docker + enabled: true + state: started + + - name: Add login user to docker group + ansible.builtin.user: + name: "{{ ansible_user_id }}" + groups: docker + append: true + + - name: Docker version + ansible.builtin.command: docker --version + register: docker_version + changed_when: false + + - name: Docker Compose version + ansible.builtin.command: docker compose version + register: compose_version + changed_when: false + + - name: Print versions + ansible.builtin.debug: + msg: + - "{{ docker_version.stdout }}" + - "{{ compose_version.stdout }}"