How to Use Ansible to Install and Set Up Docker on Ubuntu 22.04
Server automation now plays an essential role in systems administration, due to the disposable nature of modern application environments. Configuration management tools such as Ansible are typically used to streamline the process of automating server setup by establishing standard procedures for new servers while also reducing human error associated with manual setups.
Ansible offers a simple architecture that doesn’t require special software to be installed on nodes. It also provides a robust set of features and built-in modules which facilitate writing automation scripts.
This guide explains how to use Ansible to automate the steps contained in our guide on How To Install and Use Docker on Ubuntu 22.04. Docker is an application that simplifies the process of managing containers, resource-isolated processes that behave in a similar way to virtual machines, but are more portable, more resource-friendly, and depend more heavily on the host operating system.
In order to execute the automated setup provided by the playbook in this guide, you’ll need:
- One Ansible control node: an Ubuntu 22.04 machine with Ansible installed and configured to connect to your Ansible hosts using SSH keys. Make sure the control node has a regular user with sudo permissions and a firewall enabled, as explained in our Initial Server Setup guide. To set up Ansible, please follow our guide on How to Install and Configure Ansible on Ubuntu 22.04.
- One or more Ansible Hosts: one or more remote Ubuntu 22.04 servers previously set up following the guide on How to Use Ansible to Automate Initial Server Setup on Ubuntu 22.04.
What Does this Playbook Do?
This Ansible playbook provides an alternative to manually running through the procedure outlined in our guide on How To Install and Use Docker on Ubuntu 22.04. Set up your playbook once, and use it for every installation after.
Running this playbook will perform the following actions on your Ansible hosts:
aptitude, which is preferred by Ansible as an alternative to the
- Install the required system packages.
- Install the Docker GPG APT key.
- Add the official Docker repository to the
- Install Docker.
- Install the Python Docker module via
- Pull the default image specified by
default_container_imagefrom Docker Hub.
- Create the number of containers defined by the
container_countvariable, each using the image defined by
default_container_image, and execute the command defined in
default_container_commandin each new container.
Once the playbook has finished running, you will have a number of containers created based on the options you defined within your configuration variables.
To begin, log into a
sudo enabled user on your Ansible control node server.
Step 1 — Preparing your Playbook
playbook.yml file is where all your tasks are defined. A task is the smallest unit of action you can automate using an Ansible playbook. But first, create your playbook file using your preferred text editor:
This will open an empty YAML file. Before diving into adding tasks to your playbook, start by adding the following:
all docker ubuntu sleep 1
Almost every playbook you come across will begin with declarations similar to this.
hosts declares which servers the Ansible control node will target with this playbook.
become states whether all commands will be done with escalated root privileges.
vars allows you to store data in variables. If you decide to change these in the future, you will only have to edit these single lines in your file. Here’s a brief explanation of each variable:
container_count: The number of containers to create.
default_container_name: Default container name.
default_container_image: Default Docker image to be used when creating containers.
default_container_command: Default command to run on new containers.
Step 2 — Adding Packages Installation Tasks to your Playbook
By default, tasks are executed synchronously by Ansible in order from top to bottom in your playbook. This means task ordering is important, and you can safely assume one task will finish executing before the next task begins.
All tasks in this playbook can stand alone and be re-used in your other playbooks.
Add your first tasks of installing
aptitude, a tool for interfacing with the Linux package manager, and installing the required system packages. Ansible will ensure these packages are always installed on your server:
Install aptitude aptitude latest Install required system packages apt transport https ca certificates curl software properties common python3 pip virtualenv python3 setuptools latest
Here, you’re using the
apt Ansible built-in module to direct Ansible to install your packages. Modules in Ansible are shortcuts to execute operations that you would otherwise have to run as raw bash commands. Ansible safely falls back onto
apt for installing packages if
aptitude is not available, but Ansible has historically preferred
You can add or remove packages to your liking. This will ensure all packages are not only present, but on the latest version, and do after an update with
apt is called.
Step 3 — Adding Docker Installation Tasks to your Playbook
Your task will install the latest version of Docker from the official repository. The Docker GPG key is added to verify the download, the official repository is added as a new package source, and Docker will be installed. Additionally, the Docker module for Python will be installed as well:
Add Docker GPG apt Key https //download.docker.com/linux/ubuntu/gpg present Add Docker Repository deb https //download.docker.com/linux/ubuntu jammy stable present Update apt and install docker ce docker ce latest Install Docker Module for Python docker
You’ll see that
apt_repository built-in Ansible modules are first pointed at the correct URLs, then tasked to ensure they are present. This allows installation of the latest version of Docker, along with using
pip to install of the module for Python.
Step 4 — Adding Docker Image and Container Tasks to your Playbook
The actual creation of your Docker containers starts here with the pulling of your desired Docker image. By default, these images come from the official Docker Hub. Using this image, containers will be created according to the specifications laid out by the variables declared at the top of your playbook:
Pull default Docker image pull Create default containers present count= container_count
docker_image is used to pull the Docker image you want to use as the base for your containers.
docker_container allows you to specify the specifics of the containers you create, along with the command you want to pass them.
with_sequence is the Ansible way of creating a loop, and in this case, it will loop the creation of your containers according to the count you specified. This is a basic count loop, so the
item variable here provides a number representing the current loop iteration. This number is used here to name your containers.
Step 5 — Reviewing your Complete Playbook
Your playbook should look roughly like the following, with minor differences depending on your customizations:
all docker ubuntu sleep 1d Install aptitude aptitude latest Install required system packages apt transport https ca certificates curl software properties common python3 pip virtualenv python3 setuptools latest Add Docker GPG apt Key https //download.docker.com/linux/ubuntu/gpg present Add Docker Repository deb https //download.docker.com/linux/ubuntu jammy stable present Update apt and install docker ce docker ce latest Install Docker Module for Python docker Pull default Docker image pull Create default containers present count= container_count
Feel free to modify this playbook to best suit your individual needs within your own workflow. For example, you could use the docker_image module to push images to Docker Hub or the docker_container module to set up container networks.
Once you’re satisfied with your playbook, you can exit your text editor and save.
Step 6 — Running your Playbook
You’re now ready to run this playbook on one or more servers. Most playbooks are configured to be executed on every server in your inventory by default, but you’ll specify your server this time.
To execute the playbook only on
server1, connecting as
sammy, you can use the following command:
- ansible-playbook playbook.yml -l server1 -u sammy
-l flag specifies your server and the
-u flag specifies which user to log into on the remote server. You will get output similar to this:
Output. . . changed: [server1] TASK [Create default containers] ***************************************************************************************************************** changed: [server1] => (item=1) changed: [server1] => (item=2) changed: [server1] => (item=3) changed: [server1] => (item=4) PLAY RECAP *************************************************************************************************************************************** server1 : ok=9 changed=8 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
This indicates your server setup is complete! Your output doesn’t have to be exactly the same, but it is important that you have zero failures.
When the playbook is finished running, log in via SSH to the server provisioned by Ansible to check if the containers were successfully created.
Log in to the remote server with:
And list your Docker containers on the remote server:
You should see output similar to this:
OutputCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a3fe9bfb89cf ubuntu "sleep 1d" 5 minutes ago Created docker4 8799c16cde1e ubuntu "sleep 1d" 5 minutes ago Created docker3 ad0c2123b183 ubuntu "sleep 1d" 5 minutes ago Created docker2 b9350916ffd8 ubuntu "sleep 1d" 5 minutes ago Created docker1
This means the containers defined in the playbook were created successfully. Since this was the last task in the playbook, it also confirms that the playbook was fully executed on this server.
Automating your infrastructure setup can not only save you time, but it also helps to ensure that your servers will follow a standard configuration that can be customized to your needs. With the distributed nature of modern applications and the need for consistency between different staging environments, automation like this has become a central component in many teams’ development processes.
In this guide, you demonstrated how to use Ansible to automate the process of installing and setting up Docker on a remote server. Because each individual typically has different needs when working with containers, we encourage you to check out the official Ansible documentation for more information and use cases of the
docker_container Ansible module.
If you’d like to include other tasks in this playbook to further customize your initial server setup, please refer to our introductory Ansible guide Configuration Management 101: Writing Ansible Playbooks.
Top Five Free Ai Content Generator: Free Ai Content Generator 2023
The soul of a website is called the content. So it is easy to understand that the website has no value without content. And writing content takes a lot of research. There is a lot to think about. Whether it is WordPress or Blogspot, everyone knows how hard it is to write content. But what if these contents become automatically generated? Yes, you are right, today I will talk about something like this in this article. Today I will share with you five free AI content generators that are completely free.What Is Ai Generated Content?Before knowing what AI content is, we need to know what AI is. AI is Artificial Intelligence. Many people have more or fewer ideas about artificial intelligence. But for those who don't know. Artificial intelligence is when a machine such as a computer can make decisions by itself, it is called artificial intelligence. Now let me tell you what AI content is. AI content means you just give a few hints and it will automatically write the entire content within seconds. This content is called AI-generated content.Is Ai Generated Content Unique?Yes, Of course. Because these AI-generated contents are generated by different AI content generators completely by themselves. So it will give you completely unique content. AI content generators will write the content based on the topic which are you give.How Does Ai Content Generator Work?The way Ai content generators work is that You first give the generator little hints about the topic you want to write about like it might ask you for a headline. Give some keywords in the topic and it will generate unique content for you in a few seconds.Best Free Ai Content Generator ToolsThere are many AI generator tools. But today I will talk about 5 content generators that are better than other content generators and easy to use. And these content generators will give you much better content than any other content generator.Simplified- Content Generator ToolSimplified is one of the best content generator tools. There are a lot of free tools you'll find here. You can generate up to five thousand words by Simplified which is huge. And also there are a lot of free features available in Simplified.1. Ryter: An All In One Content WriterRyter is a next-level content generator tool Because it can understand the language model deeply. For this reason, it can generate the best content. You can use all the features for free but you can only generate 5000 words per month.2. Copy Ai Free Ai Content GeneratorCopy AI is the best content generator of any other tool. Because it is built on the world's best ai technology. So it can generate more creative and best content very fast. You can generate 100 pieces of content in the first months by Copy Ai Free Content Generator.3. Content Bot Advance AI WriterContent Bot Advance AI Writer tool can generate content from any short hints or keywords. And it's a very powerful content generator tool. You can generate 500 pieces of content per month and one thousand five hundred long-form editor words every month. But it's a little bit hard to use.4. Smart Copy By UnbounceSmart Copy is one of the advanced level AI writing tools. You can use it to generate your content You can use all features fully free but you can generate only 5 articles per day.So here is the top five best AI content generator tool you can use a lot of free features without purchasing it but there is a few limitations. So try to use free features. Read More
Initial Server Setup with Ubuntu 20.04
IntroductionWhen you first create a new Ubuntu 20.04 server, you should perform some important configuration steps as part of the initial setup. These steps will increase the security and usability of your server, and will give you a solid foundation for subsequent actions.Step 1 — Logging in as rootTo log into your server, you will need to know your server’s public IP address. You will also need the password or — if you installed an SSH key for authentication — the private key for the root user’s account. If you have not already logged into your server, you may want to follow our guide on how to Connect to Droplets with SSH, which covers this process in detail.If you are not already connected to your server, log in now as the root user using the following command (substitute the highlighted portion of the command with your server’s public IP address):ssh root@your_server_ip CopyAccept the warning about host authenticity if it appears. If you are using password authentication, provide your root password to log in. If you are using an SSH key that is passphrase protected, you may be prompted to enter the passphrase the first time you use the key each session. If this is your first time logging into the server with a password, you may also be prompted to change the root password.About rootThe root user is the administrative user in a Linux environment that has very broad privileges. Because of the heightened privileges of the root account, you are discouraged from using it on a regular basis. This is because the root account is able to make very destructive changes, even by accident.The next step is setting up a new user account with reduced privileges for day-to-day use. Later, we’ll show you how to temporarily gain increased privileges for the times when you need them.Step 2 — Creating a New UserOnce you are logged in as root, you’ll be able to add the new user account. In the future, we’ll log in with this new account instead of root.This example creates a new user called sammy, but you should replace that with a username that you like:adduser sammy CopyYou will be asked a few questions, starting with the account password.Enter a strong password and, optionally, fill in any of the additional information if you would like. This is not required and you can just hit ENTER in any field you wish to skip.Step 3 — Granting Administrative PrivilegesNow we have a new user account with regular account privileges. However, we may sometimes need to do administrative tasks.To avoid having to log out of our normal user and log back in as the root account, we can set up what is known as superuser or root privileges for our normal account. This will allow our normal user to run commands with administrative privileges by putting the word sudo before the command.To add these privileges to our new user, we need to add the user to the sudo group. By default, on Ubuntu 20.04, users who are members of the sudo group are allowed to use the sudo command.As root, run this command to add your new user to the sudo group (substitute the highlighted username with your new user):usermod -aG sudo sammy CopyNow, when logged in as your regular user, you can type sudo before commands to run them with superuser privileges.Step 4 — Setting Up a Basic FirewallUbuntu 20.04 servers can use the UFW firewall to make sure only connections to certain services are allowed. We can set up a basic firewall using this application.Note: If your servers are running on DigitalOcean, you can optionally use DigitalOcean Cloud Firewalls instead of the UFW firewall. We recommend using only one firewall at a time to avoid conflicting rules that may be difficult to debug.Applications can register their profiles with UFW upon installation. These profiles allow UFW to manage these applications by name. OpenSSH, the service allowing us to connect to our server now, has a profile registered with UFW.You can see this by typing:ufw app list CopyOutputAvailable applications: OpenSSH We need to make sure that the firewall allows SSH connections so that we can log back in next time. We can allow these connections by typing:ufw allow OpenSSH CopyAfterwards, we can enable the firewall by typing:ufw enable CopyType y and press ENTER to proceed. You can see that SSH connections are still allowed by typing:ufw status CopyOutputStatus: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) As the firewall is currently blocking all connections except for SSH, if you install and configure additional services, you will need to adjust the firewall settings to allow traffic in. You can learn some common UFW operations in our UFW Essentials guide.Step 5 — Enabling External Access for Your Regular UserNow that we have a regular user for daily use, we need to make sure we can SSH into the account directly.Note: Until verifying that you can log in and use sudo with your new user, we recommend staying logged in as root. This way, if you have problems, you can troubleshoot and make any necessary changes as root. If you are using a DigitalOcean Droplet and experience problems with your root SSH connection, you can regain access to Droplets using the Recovery Console.The process for configuring SSH access for your new user depends on whether your server’s root account uses a password or SSH keys for authentication.If the root Account Uses Password AuthenticationIf you logged in to your root account using a password, then password authentication is enabled for SSH. You can SSH to your new user account by opening up a new terminal session and using SSH with your new username:ssh sammy@your_server_ip CopyAfter entering your regular user’s password, you will be logged in. Remember, if you need to run a command with administrative privileges, type sudo before it like this:sudo command_to_run CopyYou will be prompted for your regular user password when using sudo for the first time each session (and periodically afterwards).To enhance your server’s security, we strongly recommend setting up SSH keys instead of using password authentication. Follow our guide on setting up SSH keys on Ubuntu 20.04 to learn how to configure key-based authentication.If the root Account Uses SSH Key AuthenticationIf you logged in to your root account using SSH keys, then password authentication is disabled for SSH. You will need to add a copy of your local public key to the new user’s ~/.ssh/authorized_keys file to log in successfully.Since your public key is already in the root account’s ~/.ssh/authorized_keys file on the server, we can copy that file and directory structure to our new user account in our existing session.The simplest way to copy the files with the correct ownership and permissions is with the rsync command. This will copy the root user’s .ssh directory, preserve the permissions, and modify the file owners, all in a single command. Make sure to change the highlighted portions of the command below to match your regular user’s name:Note: The rsync command treats sources and destinations that end with a trailing slash differently than those without a trailing slash. When using rsync below, be sure that the source directory (~/.ssh) does not include a trailing slash (check to make sure you are not using ~/.ssh/).If you accidentally add a trailing slash to the command, rsync will copy the contents of the root account’s ~/.ssh directory to the sudo user’s home directory instead of copying the entire ~/.ssh directory structure. The files will be in the wrong location and SSH will not be able to find and use them.rsync --archive --chown=sammy:sammy ~/.ssh /home/sammy CopyNow, open up a new terminal session on your local machine, and use SSH with your new username:ssh sammy@your_server_ip CopyYou should be logged in to the new user account without using a password. Remember, if you need to run a command with administrative privileges, type sudo before it like this:sudo command_to_run CopyYou will be prompted for your regular user password when using sudo for the first time each session (and periodically afterward).Where To Go From Here?At this point, you have a solid foundation for your server. You can install any of the software you need on your server now.Read More