Friday, 29 April 2022

Install Docker Container using Ansible YAML File on Ubuntu 22.04


cat ansible_docker.yml
---
 - name: Setup Docker 
   hosts: localhost
   become: yes
   tasks:
     - name: Install docker dependencies
       include: docker_dependency.yml

     - name: update apt
       apt:
         update_cache: yes

     - name: Install docker
       apt:
         name: docker-ce
         state: latest
         update_cache: yes

     - name: Service status - docker
       service:
         name: docker
         state: started

     - name: docker ps -a
       shell: sudo docker ps -a
       register: docker_ps

     - debug:
         var: docker_ps.stdout_lines

     - name: Run Hello World
       shell: sudo docker run hello-world
       register: docker_run

     - debug:
         var: docker_run.stdout_lines

     - name: docker ps -a run
       shell: sudo docker ps -a
       register: docker_ps_run

     - debug:
         var: docker_ps_run.stdout_lines


cat docker_dependency.yml
---
- name: Ensure old versions of Docker are not installed.
  package:
    name:
      - docker
      - docker-engine
    state: absent

- name: Ensure dependencies are installed.
  apt:
    name:
      - apt-transport-https
      - ca-certificates
      - curl
      - software-properties-common
      - gnupg
      - lsb-release
    state: present

- name: Add Docker GPG apt Key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: Add Docker Repository
  apt_repository:
    repo: deb https://download.docker.com/linux/ubuntu focal stable
    state: present

ansible-playbook ansible_docker.yml

No comments:

Post a Comment