Ansible Playbook으로 Docker 설치

RedHat 계열 리눅스에서 Ansible로 도커 설치하는 방법

Ansible 설정

  • install_docker.yaml

    • 10대 이상의 리눅스에 도커를 설치하는 playbook
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    
    - name: Install Docker CE on web/was hosts
      hosts: webwas
      become: true
      gather_facts: yes
    
      vars:
        conflict_pkgs:
          - docker.io        
          - docker-doc
          - docker-compose
          - docker-compose-v2
          - podman-docker
          - containerd
          - runc
    
        docker_pkgs:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-buildx-plugin
          - docker-compose-plugin
    
      tasks:
        - name: 01. Remove conflicting packages (ignore if not present)
          package:
            name: "{{ conflict_pkgs }}"
            state: absent
    
        - name: 02. Install prerequisites
          package:
            name: yum-utils
            state: present
          when: ansible_os_family == "RedHat"
    
        - name: 03. Add Docker CE yum repository (official)
          yum_repository:
            name: docker-ce-stable
            description: Docker CE Stable - $basearch
            baseurl: "https://download.docker.com/linux/centos/$releasever/$basearch/stable"
            gpgcheck: yes
            gpgkey: https://download.docker.com/linux/centos/gpg
            enabled: yes
          when: ansible_os_family == "RedHat"
    
        - name: 04. Install Docker CE packages
          package:
            name: "{{ docker_pkgs }}"
            state: present
    
        - name: 05. Enable and start Docker
          service:
            name: docker
            state: started
            enabled: yes
    
    • task01: 기존 충돌 패키지 제거
    • task02: 필수 패키지 설치
    • task03: Docker CE 패키지 리포지토리 추가
      • OS 버전에 맞는 리포지토리 자동 설정
    • task04: 도커 설치
    • task05: 도커 서비스 시작 및 자동 실행