126 lines
3.9 KiB
YAML
126 lines
3.9 KiB
YAML
---
|
|
# Semaphore v2: prompts via Survey for vm_ip, vm_user, vm_pass
|
|
# Installs Docker on Ubuntu 24.04 (noble) using Ubuntu repo packages (docker.io + docker-compose-v2)
|
|
# Includes strong checks so failures are obvious.
|
|
|
|
- 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
|
|
- 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 (force password SSH)
|
|
ansible.builtin.add_host:
|
|
name: target_vm
|
|
groups: runtime
|
|
ansible_host: "{{ vm_ip }}"
|
|
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
|
|
-o IdentitiesOnly=yes
|
|
-o StrictHostKeyChecking=no
|
|
-o UserKnownHostsFile=/dev/null
|
|
|
|
- name: Show connection target
|
|
ansible.builtin.debug:
|
|
msg: "Targeting {{ vm_user }}@{{ vm_ip }} using PASSWORD SSH"
|
|
|
|
- name: Install Docker on chosen VM (Ubuntu repo method)
|
|
hosts: runtime
|
|
become: true
|
|
gather_facts: true
|
|
|
|
tasks:
|
|
- name: Update apt cache
|
|
ansible.builtin.apt:
|
|
update_cache: true
|
|
|
|
# Enable universe (often needed on minimal/cloud images)
|
|
- name: Ensure universe repository is enabled (Ubuntu)
|
|
ansible.builtin.apt_repository:
|
|
repo: "deb http://archive.ubuntu.com/ubuntu {{ ansible_distribution_release }} universe"
|
|
state: present
|
|
when: ansible_distribution == "Ubuntu"
|
|
|
|
- name: Update apt cache after enabling universe
|
|
ansible.builtin.apt:
|
|
update_cache: true
|
|
|
|
- name: Install Docker from Ubuntu repositories
|
|
ansible.builtin.apt:
|
|
name:
|
|
- docker.io
|
|
- containerd
|
|
- docker-compose-v2
|
|
state: present
|
|
update_cache: true
|
|
|
|
- name: List docker/containerd systemd units (debug)
|
|
ansible.builtin.command: systemctl list-unit-files | grep -E '^(docker|containerd)\.service'
|
|
register: docker_units
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: Show units found (debug)
|
|
ansible.builtin.debug:
|
|
var: docker_units.stdout_lines
|
|
|
|
- name: Enable and start containerd
|
|
ansible.builtin.systemd:
|
|
name: containerd
|
|
enabled: true
|
|
state: started
|
|
when: docker_units.stdout is search("containerd.service")
|
|
|
|
- name: Enable and start docker
|
|
ansible.builtin.systemd:
|
|
name: docker
|
|
enabled: true
|
|
state: started
|
|
when: docker_units.stdout is search("docker.service")
|
|
|
|
- name: Fail clearly if docker.service is missing
|
|
ansible.builtin.fail:
|
|
msg: >
|
|
docker.service not found after installation.
|
|
Run on VM: dpkg -l | grep -E 'docker|containerd' ;
|
|
systemctl list-unit-files | grep -E 'docker|containerd' ;
|
|
which docker
|
|
when: docker_units.stdout is not search("docker.service")
|
|
|
|
- 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 v2 version
|
|
ansible.builtin.command: docker compose version
|
|
register: compose_version
|
|
changed_when: false
|
|
failed_when: false
|
|
|
|
- name: Print versions
|
|
ansible.builtin.debug:
|
|
msg:
|
|
- "{{ docker_version.stdout }}"
|
|
- "{{ compose_version.stdout | default('docker compose not available') }}"
|
|
|