In the last guide, you learned how to set up, install, and configure Ansible on Ubuntu 18.04. Now, you will use the Ansible to install and set Docker on a remote machine. To begin this guide, you need the following:

  • One Ansible Control Node: You need 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 ansible all -m ping command.

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:

  1. Ansible prefers aptitude package manager over the default apt.
  2. Install the required system packages like python3-pipcurl, and other such packages.
  3. Install Docker GPG APT key to the system and add the official Docker repository to the apt source.
  4. Install Docker on the remote machine.
  5. Install Python Docker module via pip.
  6. 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:

Shell

$ mkdir docker_server && cd $_ 
$ mkdir vars && cd $_ && touch default.yml 
$ cd .. && touch main.yml

The directory layout should look like:

Plain Text

docker_server/ 
|-- main.yml 
`-- vars4
   `-- default.yml 
1 directory, 2 files

Let’s see what each of these files are:

  1. docker_server: This is the project root directory containing all variable files and main playbook.
  2. vars/default.yml: Variable file resides in vars directory through which you are going to customize the playbook settings.
  3. main.yml: Here, you are going to define the task that is going to execute on the remote server.

vars/default.yml

Now first begin with the playbook’s variable file. Here you are going to customize your Docker setup. Open vars/default.yml in your editor of choice:

Shell

$ cd docke_server && nano vars/default.yml

Copy the below lines and paste it in vars/default.yml:

YAML

--- 
containers: 2 
container_name: docker_ubuntu 
container_image: ubuntu:18.04 
container_command: sleep 1d

#tutorial #programming #docker #ubuntu #ansible

Install and Setup Docker Using Ansible on Ubuntu 18.04 (Part 2)
1.25 GEEK