Files
2026-07-20 09:23:17 -04:00

125 lines
3.4 KiB
YAML

---
- name: Install docker
hosts: "{{ my_hosts | d([]) }}"
become: true
tasks:
tasks:
# Install SSH Public Key
- name: Install public keys
ansible.posix.authorized_key:
user: "{{ lookup('env', 'USER') }}"
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
- name: Change sudoers file
ansible.builtin.lineinfile:
path: /etc/sudoers
state: present
regexp: '^%sudo'
line: '%sudo ALL=(ALL) NOPASSWD: ALL'
validate: /usr/sbin/visudo -cf %s
# Update the system
- name: Update package index
apt:
update_cache: yes
- name: Upgrade packages
apt:
upgrade: yes
- name: Perform a distro upgrade
ansible.builtin.apt:
upgrade: dist
update_cache: yes
- name: Remove dependencies that are no longer needed
ansible.builtin.apt:
autoremove: yes
purge: true
- name: Update all packages to their latest version
ansible.builtin.apt:
name: "*"
state: latest
- name: Run the equivalent of "apt-get clean" as a separate step
ansible.builtin.apt:
clean: yes
# Install Docker and all its dependencies
- name: Install docker dependencies
ansible.builtin.apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
update_cache: true
- name: Add docker gpg key
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
keyring: /etc/apt/keyrings/docker.gpg
- name: Add docker repository
ansible.builtin.apt_repository:
filename: docker
repo: deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename | lower }} stable
state: present
- name: Update package index
apt:
update_cache: yes
- name: Install docker engine
ansible.builtin.apt:
name:
- docker-ce
- docker-buildx-plugin
- docker-compose-plugin
update_cache: true
# Install Portainer and its volume
- name: Create new volume
community.docker.docker_volume:
name: portainer-data
- name: Deploy portainer
community.docker.docker_container:
name: portainer
image: "docker.io/portainer/portainer-ce"
ports:
- "9445:9443"
volumes:
- /run/docker.sock:/var/run/docker.sock
- portainer-data:/data
restart_policy: unless-stopped
# Install Watch Tower
tasks:
- name: Ensure Docker is running
systemd:
name: docker
state: started
enabled: yes
- name: Create Watchtower directory for configuration (optional, for persistent configuration)
file:
path: /opt/watchtower
state: directory
mode: '0755'
- name: Run Watchtower container
community.docker.docker_container:
name: watchtower
image: containrrr/watchtower
restart_policy: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock # Required for Watchtower to interact with Docker
# - /opt/watchtower/config.json:/config.json # Optional: for persistent configuration
# command: --interval 300 # Optional: specify update interval in seconds
state: started