In the last guide, you learned how to set up, install, and configure . Now, you will use the Ansible to install and set Docker on a remote machine. To begin this guide, you need the following: Ansible on Ubuntu 18.04 One Ansible Control Node: You need a Ansible installed and configured machine. One or more Ansible Hots: At least one remote host with Ubuntu 18.04 with sudo permissions. Please make sure that your Ansible control node is able to connect to your Ansible remote machines. To test the connection, you can use ping command. ansible all -m Creating Playbook for Operations You will be using Ansible Playbook to perform a set of actions on your Ansible remote machine which are as following: Ansible prefers package manager over the default . aptitude apt Install the required system packages like , , and other such packages. python3-pip curl Install Docker GPG APT key to the system and add the official Docker repository to the apt source. Install Docker on the remote machine. Install Python Docker module via . pip Pull an image from Docker Registry. Once you are through with this guide, you will be running a defined number of containers on your remote host. Let’s begin this guide. Create an Ansible Playbook: First, you’ve to create a working directory where all your files will reside: $ mkdir docker_server && $ mkdir vars && && touch default.yml $ .. && touch main.yml cd $_ cd $_ cd The directory layout should look like: docker_server/ |-- main.yml `-- vars `-- default.yml 1 directory, 2 files Let’s see what each of these files are: : This is the project root directory containing all variable files and main playbook. docker_server : Variable file resides in directory through which you are going to customize the playbook settings. vars/default.yml vars : Here, you are going to define the task that is going to execute on the remote server. main.yml vars/default.yml Now first begin with the playbook’s variable file. Here you are going to customize your Docker setup. Open in your editor of choice: vars/default.yml $ docke_server && nano vars/default.yml cd Copy the below lines and paste it in : vars/default.yml --- containers: 2 container_name: docker_ubuntu container_image: ubuntu:18.04 container_command: sleep 1d A brief explanation of each of these variables: : You can define n number of containers you want to launch. Just make sure that your remote system has enough juice to run it smoothly. containers : This variable is used to name the running containers. container_name : Image that you use when creating containers. container_image : Command that is going to run inside the new containers. container_command main.yml In this file, you are going to define all tasks, where you are going to define the group of servers that should be targeted with privilege sudo. Here you are also going to load the variable file you created previously. Again paste the following lines, make sure that file is in a format that follows the YAML standards. vars/default.yml --- - hosts: all become: vars_files: - vars/default.yml tasks: - name: Install aptitude using apt apt: name=aptitude state=latest update_cache=yes force_apt_get=yes - name: Install required system packages apt: name={{ item }} state=latest update_cache=yes loop: [ , , , , , , ] - 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 bionic stable state: present - name: Update apt and install docker-ce apt: update_cache=yes name=docker-ce state=latest - name: Install Docker Module Python pip: name: docker - name: Pull default Docker image docker_image: name: : pull - name: Create default containers docker_container: name: image: : state: present with_sequence: count={{ containers }} true 'apt-transport-https' 'ca-certificates' 'curl' 'software-properties-common' 'python3-pip' 'virtualenv' 'python3-setuptools' for "{{ container_image }}" source "{{ container_name }}{{ item }}" "{{ container_image }}" command "{{ container_command }}" Execute The Ansible Playbook: Now, execute the playbook you created previously. For example, our playbook is on , and you are going to connect it as the user, then use the following command: remote1 root $ ansible-playbook main.yml -l remote1 -u root You will see a similar output: ... TASK [Add Docker GPG apt Key] ************************************************************************************** changed: [remote1] TASK [Add Docker Repository] ************************************************************************************** changed: [remote1] TASK [Update apt and install docker-ce] ************************************************************************************** changed: [remote1] TASK [Install Docker Module Python] ************************************************************************************** changed: [remote1] TASK [Pull default Docker image] ************************************************************************************** changed: [remote1] TASK [Create default containers] ************************************************************************************** changed: [remote1] => (item=1) changed: [remote1] => (item=2) PLAY RECAP ************************************************************************************** remote1 : ok=8 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 for Once your playbook is finished running, you can log in to your remote server via SSH and confirm if docker container was created successfully: $ ssh -i remote1-key.pem -p 4576 remote1@youripaddresshere $ sudo docker ps -a Flag to include your private key and to specify the port number SSH is listening. -i -p You should see output similar to the following: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES t3gejb7o82dy ubuntu 3 minutes ago Created docker_ubuntu1 9df96gced2fg ubuntu 3 minutes ago Created docker_ubuntu2 "sleep 1d" "sleep 1d" Conclusion: In this guide, you used Ansible to automate the process of installing and setting up Docker on a remote server. You can modify the playbook as per your need and workflow; it is also recommended that you do visit Ansible user guide for module. docker_container About the author - Sudip is a Solution Architect with more than 15 years of working experience, and is the founder of Javelynn . He likes sharing his knowledge by regularly writing for Hackernoon , DZone , Appfleet and many more. And while he is not doing that, he must be fishing or playing chess. Previously posted at https://appfleet.com/ .