Add proxmox-docker-install.yml

This commit is contained in:
2025-12-11 15:03:00 +00:00
parent fe4616a768
commit f8c1ceac05

107
proxmox-docker-install.yml Normal file
View File

@@ -0,0 +1,107 @@
---
# Play 1: prompt once, on localhost
- name: Ask for node and VMID
hosts: localhost
gather_facts: false
vars_prompt:
- name: target_node
prompt: "Enter Proxmox node (loki / thanos / odin)"
private: false
- name: target_vmid
prompt: "Enter VMID"
private: false
tasks:
- name: Store chosen values as facts on localhost
ansible.builtin.set_fact:
chosen_node: "{{ target_node }}"
chosen_vmid: "{{ target_vmid }}"
# Play 2: run on the chosen node and install Docker
- name: Install Docker and Docker Compose on chosen node
hosts: "{{ hostvars['localhost'].chosen_node }}"
become: true
gather_facts: true
vars:
vmid: "{{ hostvars['localhost'].chosen_vmid }}"
tasks:
- name: Show which node and VMID are selected
ansible.builtin.debug:
msg: "Installing Docker on node {{ inventory_hostname }} for VMID {{ vmid }}"
- name: Update apt cache
ansible.builtin.apt:
update_cache: yes
- name: Install prerequisites
ansible.builtin.apt:
name:
- ca-certificates
- curl
- gnupg
- lsb-release
state: present
- name: Create keyrings directory
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
- name: Add Docker GPG key
ansible.builtin.get_url:
url: https://download.docker.com/linux/ubuntu/gpg
dest: /etc/apt/keyrings/docker.asc
mode: '0644'
- name: Add Docker repository
ansible.builtin.apt_repository:
repo: "deb [arch={{ ansible_architecture }} signed-by=/etc/apt/keyrings/docker.asc] 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: yes
- 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: Start and enable Docker service
ansible.builtin.systemd:
name: docker
state: started
enabled: yes
- name: Add SSH user to docker group
ansible.builtin.user:
name: "{{ ansible_user }}"
groups: docker
append: yes
- name: Show Docker versions
ansible.builtin.command: docker version
register: docker_version
changed_when: false
- name: Show Docker Compose version
ansible.builtin.command: docker compose version
register: docker_compose_version
changed_when: false
- name: Print Docker info
ansible.builtin.debug:
msg:
- "Docker version: {{ docker_version.stdout_lines | default([]) }}"
- "Docker Compose: {{ docker_compose_version.stdout_lines | default([]) }}"