AKAI TSUKI

System development or Technical something

Install docker using ansible.

I create playbook file "docker_install.yml" to install docker-ce.

---
- name: Install Docker
  hosts: grp_node
  tasks:
  - name: Install package needed for docker-ce
    yum:
      name: '{{ item }}'
      state: installed
    with_items:
      - "yum-utils"
      - "device-mapper-persistent-data"
      - "lvm2"

  - name: Add Docker GPG key.
    rpm_key:
      key: https://download.docker.com/linux/centos/gpg
      state: present

  - name: Check repository
    shell: yum repolist enabled
    args:
      warn: false
    register: repolist_info
    changed_when: false

  - name: debug
    debug:
      var: repolist_info

  - name: Add repository, if don't add yet.
    shell: yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    when: "'Docker CE Stable' not in repolist_info.stdout"
    ## You can also perform the following writing.
    # when: repolist_info.stdout.find('Docker CE Stable') == -1

  - name: Install docker-ce package
    yum:
      name: '{{ item }}'
      state: installed
    with_items:
      - "docker-ce"

To install docker, I execute following command.

[root@cent7devops ansible-test]# ansible-playbook docker_install.yml -l node01