Install Docker on Ubuntu with Ansible using the official repository.
IntermediateHands-onWrite-an-Ansible-playbook tasks
Answer
- hosts: docker_hosts
become: true
tasks:
- name: Prereqs
ansible.builtin.apt: { name: [ca-certificates, curl], state: present, update_cache: true }
- name: Docker GPG key
ansible.builtin.get_url:
url: https://download.docker.com/linux/ubuntu/gpg
dest: /etc/apt/keyrings/docker.asc
mode: "0644"
- name: Docker repo
ansible.builtin.apt_repository:
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {{ ansible_facts['distribution_release'] }} stable"
state: present
- name: Install Docker
ansible.builtin.apt:
name: [docker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin, docker-compose-plugin]
state: present
update_cache: true
- name: Let ubuntu user run docker
ansible.builtin.user: { name: ubuntu, groups: docker, append: true }
Key trick → signed-by scopes the key to this one repo. apt-key is deprecated, so don't use it.
More on Write-an-Ansible-playbook tasks
Write a playbook that installs and starts Nginx on Ubuntu web servers.BeginnerWrite an inventory with groups, a group of groups, and per-group variables.BeginnerWrite a playbook that deploys a config file from a Jinja2 template and restarts the service only if it changed.BeginnerWrite a role structure for a Node.js app and show what goes in each folder.IntermediateWrite a task that creates multiple users with SSH keys from a list.BeginnerWrite a rolling deploy playbook: 2 servers at a time, removed from the load balancer during the update.Advanced