Last active
          May 23, 2025 12:59 
        
      - 
      
 - 
        
Save rbq/886587980894e98b23d0eee2a1d84933 to your computer and use it in GitHub Desktop.  
    Install Docker CE on Ubuntu using Ansible 
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | --- | |
| - hosts: all | |
| tasks: | |
| - name: Install prerequisites for Docker repository | |
| apt: | |
| name: ['apt-transport-https', 'ca-certificates', 'curl', 'gnupg2', 'software-properties-common'] | |
| update_cache: yes | |
| - name: Add Docker GPG key | |
| apt_key: | |
| url: https://download.docker.com/linux/ubuntu/gpg | |
| - name: Add Docker APT repository | |
| apt_repository: | |
| repo: deb [arch=amd64] https://download.docker.com/{{ ansible_system | lower }}/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable | |
| - name: Install Docker CE | |
| apt: | |
| name: ['docker-ce', 'docker-ce-cli', 'containerd.io'] | |
| update_cache: yes | |
| - name: Install prerequisites for docker-compose | |
| apt: | |
| name: ['python3-pip', 'python3-setuptools', 'virtualenv'] | |
| - name: Install docker-compose | |
| pip: | |
| name: docker-compose | 
I got error "Unable to find any of pip2, pip to use. pip needs to be installed." on ubuntu bionic using command ansible-playbook --connection=local --inventory 127.0.0.1, --limit 127.0.0.1 docker.yaml. It only has python 3. It worked after I setup explicitly the pip version with the parameter "executable".
This works for me
$ ansible --version
ansible 2.10.2
---
- name: apt update
  apt:
    update_cache: yes
- name: Install prerequisites for Docker repository
  apt:
    pkg:
    - apt-transport-https
    - ca-certificates
    - curl
    - gnupg2
    - software-properties-common
- name: add docker apt key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present
- name: add docker apt repo
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution|lower }} {{ ansible_distribution_release }} stable"
    state: present
    update_cache: yes
  
- name: install docker and it's dependencies
  apt:
    pkg:
      - docker-ce
      - docker-ce-cli
      - containerd.io
    state: present
- name: start and enable docker daemon
  service:
    name: docker
    state: started
    enabled: yes
    - name: fetch docker-compose checksum
  uri:
    url: https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64.sha256
    return_content: yes
  register: docker_compose_checksum
- name: install docker-compose
  get_url:
    url: https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64
    checksum: "sha256:{{ docker_compose_checksum.content.split(' ') | first }}"
    dest: /usr/local/bin/docker-compose
    mode: '0755'
you can use {{ ansible_machine }} and {{ ansible_system }} instead of above hardcoded x86_64 and Linux
@mstevanic If you have gather_facts: True :)
Docker on Ubuntu 22.04 AWS AMI from Canonical, sticking as close to the official install page as possible, and leaning on work from @yum-dev above:
- name: install prerequisites for Docker repository
  become: yes
  ansible.builtin.apt:
    pkg:
      - ca-certificates
      - curl
      - gnupg2
      - lsb-release
- name: add docker apt key
  become: yes
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present
- name: add docker apt repo
  become: yes
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
    state: present
    update_cache: yes
- name: install docker and its dependencies
  become: yes
  apt:
    pkg:
      - docker-ce
      - docker-ce-cli
      - containerd.io
      - docker-buildx-plugin
      - docker-compose-plugin
    state: present
- name: start and enable docker daemon
  become: yes
  service:
    name: docker
    state: started
    enabled: yes
- name: start and enable containerd daemon
  become: yes
  service:
    name: containerd
    state: started
    enabled: yes
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
https://github.com/kurarrr/docker-ansible/blob/master/playbook.yml
This worked for me with ansible 2.9.9.
But I got the
if I added
remote_user: "{{ deploy_user_name }}".How can I use this variable?