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)通常用于通过为新服务器建立标准过程来简化服务器设置自动化的过程,同时还减少了与手动设置相关的人为错误。
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.
Ansible提供了一种简单的体系结构,不需要在节点上安装特殊软件。 它还提供了一组功能强大的功能和内置模块,可帮助编写自动化脚本。
This guide explains how to use Ansible to automate the steps contained in our guide on How To Install and Use Docker on Ubuntu 18.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.
本指南说明了如何使用Ansible来自动化我们的指南( 如何在Ubuntu 18.04上安装和使用Docker)中包含的步骤。 Docker是一种应用程序,可简化容器的管理过程,资源隔离的过程,这些过程的行为与虚拟机相似,但具有更强的可移植性,更友好的资源,并且更加依赖主机操作系统。
In order to execute the automated setup provided by the playbook we’re discussing in this guide, you’ll need:
为了执行我们在本指南中讨论的剧本提供的自动设置,您需要:
One Ansible control node: an Ubuntu 18.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 18.04.
一个Ansible控制节点 :安装了Ansible并配置为使用SSH密钥连接到您的Ansible主机的Ubuntu 18.04计算机。 确保控制节点具有sudo权限的常规用户并启用了防火墙,如“ 初始服务器设置”指南中所述。 要设置Ansible,请遵循有关如何在Ubuntu 18.04上安装和配置Ansible的指南。
One or more Ansible Hosts: one or more remote Ubuntu 18.04 servers previously set up following the guide on How to Use Ansible to Automate Initial Server Setup on Ubuntu 18.04.
一台或多台Ansible主机 :一台或多台远程Ubuntu 18.04服务器以前是按照有关如何在Ubuntu 18.04上使用Ansible自动化初始服务器设置的指南进行设置的 。
Before proceeding, you first need to make sure your Ansible control node is able to connect and execute commands on your Ansible host(s). For a connection test, please check step 3 of How to Install and Configure Ansible on Ubuntu 18.04.
在继续之前,您首先需要确保Ansible控制节点能够在Ansible主机上连接并执行命令。 对于连接测试,请检查如何在Ubuntu 18.04上安装和配置Ansible的步骤3。
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 18.04.
这本Ansible剧本提供了一种替代方法,可以手动运行我们的指南中有关如何在Ubuntu 18.04上安装和使用Docker概述的过程。
Running this playbook will perform the following actions on your Ansible hosts:
运行此剧本将在您的Ansible主机上执行以下操作:
Install aptitude
, which is preferred by Ansible as an alternative to the apt
package manager.
安装aptitude
,这是Ansible首选的替代apt
软件包管理器的工具。
Add the official Docker repository to the apt
sources.
将官方Docker存储库添加到apt
源码中。
Install the Python Docker module via pip
.
通过pip
安装Python Docker模块。
Pull the default image specified by default_container_image
from Docker Hub.
从Docker Hub中提取default_container_image
指定的默认映像。
Create the number of containers defined by the create_containers
variable, each using the image defined by default_container_image
, and execute the command defined in default_container_command
in each new container.
创建由create_containers
变量定义的容器数,每个容器使用default_container_image
定义的映像,并在每个新容器中执行default_container_command
定义的命令。
Once the playbook has finished running, you will have a number of containers created based on the options you defined within your configuration variables.
剧本运行完毕后,将根据您在配置变量中定义的选项创建多个容器。
The first thing we need to do is obtain the Docker playbook and its dependencies from the do-community/ansible-playbooks repository. We need to clone this repository to a local folder inside the Ansible Control Node.
我们需要做的第一件事是从do-community / ansible-playbooks存储库中获取Docker剧本及其依赖项。 我们需要将此存储库克隆到Ansible控制节点内的本地文件夹。
In case you have cloned this repository before while following a different guide, access your existing ansible-playbooks
copy and run a git pull
command to make sure you have updated contents:
如果您在遵循其他指南之前已经克隆了此存储库,请访问现有的ansible-playbooks
副本并运行git pull
命令以确保已更新内容:
If this is your first time using the do-community/ansible-playbooks
repository, you should start by cloning the repository to your home folder with:
如果这是您第一次使用do-community/ansible-playbooks
存储库,则应首先使用以下命令将存储库克隆到主文件夹中:
The files we’re interested in are located inside the docker_ubuntu1804
folder, which has the following structure:
我们感兴趣的文件位于docker_ubuntu1804
文件夹中,该文件夹具有以下结构:
docker_ubuntu1804
├── vars
│ └── default.yml
├── playbook.yml
└── readme.md
Here is what each of these files are:
以下是每个文件的含义:
vars/default.yml
: Variable file for customizing playbook settings.
vars/default.yml
:用于自定义剧本设置的变量文件。
playbook.yml
: The playbook file, containing the tasks to be executed on the remote server(s).
playbook.yml
:剧本文件,包含要在远程服务器上执行的任务。
readme.md
: A text file containing information about this playbook.
readme.md
:一个文本文件,包含有关此剧本的信息。
We’ll edit the playbook’s variable file to customize our Docker setup. Access the docker_ubuntu1804
directory and open the vars/default.yml
file using your command line editor of choice:
我们将编辑剧本的变量文件来自定义Docker设置。 访问docker_ubuntu1804
目录并使用您选择的命令行编辑器打开vars/default.yml
文件:
This file contains a few variables that require your attention:
该文件包含一些需要引起您注意的变量:
---
create_containers: 4
default_container_name: docker
default_container_image: ubuntu
default_container_command: sleep 1d
The following list contains a brief explanation of each of these variables and how you might want to change them:
以下列表简要说明了每个变量以及如何更改它们:
create_containers
: The number of containers to create.
create_containers
:要创建的容器数。
default_container_name
: Default container name.
default_container_name
:默认容器名称。
default_container_image
: Default Docker image to be used when creating containers.
default_container_image
:创建容器时要使用的默认Docker映像。
default_container_command
: Default command to run on new containers.
default_container_command
:在新容器上运行的默认命令。
Once you’re done updating the variables inside vars/default.yml
, save and close this file. If you used nano
, do so by pressing CTRL + X
, Y
, then ENTER
.
更新完vars/default.yml
的变量后,保存并关闭此文件。 如果您使用过nano
,请按CTRL + X
, Y
,然后按ENTER
。
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. We can use the -l
flag to make sure that only a subset of servers, or a single server, is affected by the playbook. We can also use the -u
flag to specify which user on the remote server we’re using to connect and execute the playbook commands on the remote hosts.
现在,您可以在一个或多个服务器上运行此剧本。 默认情况下,大多数剧本配置为在清单中的每台服务器上执行。 我们可以使用-l
标志来确保只有一部分服务器或单个服务器受该剧本的影响。 我们还可以使用-u
标志来指定用于连接和执行远程主机上的playbook命令的远程服务器上的哪个用户。
To execute the playbook only on server1
, connecting as sammy
, you can use the following command:
要仅在以sammy
连接的server1
上执行剧本,可以使用以下命令:
ansible-playbook playbook.yml -l server1 -u sammy
ansible-playbook playbook.yml -l server1 -u sammy
You will get output similar to this:
您将获得类似于以下的输出:
Output
...
TASK [Add Docker GPG apt Key] ********************************************************************************************************************
changed: [server1]
TASK [Add Docker Repository] *********************************************************************************************************************
changed: [server1]
TASK [Update apt and install docker-ce] **********************************************************************************************************
changed: [server1]
TASK [Install Docker Module for Python] **********************************************************************************************************
changed: [server1]
TASK [Pull default Docker image] *****************************************************************************************************************
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
Note: For more information on how to run Ansible playbooks, check our Ansible Cheat Sheet Guide.
注意 :有关如何运行Ansible剧本的更多信息,请参阅《 Ansible备忘单指南》 。
When the playbook is finished running, log in via SSH to the server provisioned by Ansible and run docker ps -a
to check if the containers were successfully created:
当剧本完成运行后,通过SSH登录到Ansible设置的服务器,然后运行docker docker ps -a
检查容器是否成功创建:
You should see output similar to this:
您应该看到类似于以下的输出:
Output
CONTAINER 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.
这意味着在剧本中定义的容器已成功创建。 由于这是剧本中的最后一项任务,因此它也确认剧本已在此服务器上完全执行。
You can find the Docker server setup featured in this tutorial in the docker_ubuntu1804
folder inside the DigitalOcean Community Playbooks repository. To copy or download the script contents directly, click the Raw button towards the top of each script.
您可以在DigitalOcean Community Playbooks存储库内的docker_ubuntu1804
文件夹中找到本教程中介绍的Docker服务器设置。 要直接复制或下载脚本内容,请单击每个脚本顶部的Raw按钮。
The full contents of the playbook as well as its associated files are also included here for your convenience.
为了方便起见,此处也包含了剧本的完整内容及其相关文件。
The default.yml
variable file contains values that will be used when setting up Docker on your server.
default.yml
变量文件包含在服务器上设置Docker时将使用的值。
---
create_containers: 4
default_container_name: docker
default_container_image: ubuntu
default_container_command: sleep 1d
The playbook.yml
file is where all tasks from this setup are defined. It starts by defining the group of servers that should be the target of this setup (all
), after which it uses become: true
to define that tasks should be executed with privilege escalation (sudo
) by default. Then, it includes the vars/default.yml
variable file to load configuration options.
在playbook.yml
文件中,定义了此设置中的所有任务。 它首先定义应作为此设置目标的服务器组( all
),然后使用become: true
定义默认情况下应以特权升级( sudo
)执行任务。 然后,它包含vars/default.yml
变量文件以加载配置选项。
---
- hosts: all
become: true
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: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools']
- 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 for Python
pip:
name: docker
- name: Pull default Docker image
docker_image:
name: "{{ default_container_image }}"
source: pull
# Creates the number of containers defined by the variable create_containers, using values from vars file
- name: Create default containers
docker_container:
name: "{{ default_container_name }}{{ item }}"
image: "{{ default_container_image }}"
command: "{{ default_container_command }}"
state: present
with_sequence: count={{ create_containers }}
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.
可以随意修改此剧本,以使其最适合您自己的工作流程中的个性化需求。 例如,您可以使用docker_image
模块将图像推送到Docker Hub或使用docker_container
模块设置容器网络。
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, we 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.
在本指南中,我们演示了如何使用Ansible自动化远程服务器上的Docker安装和设置过程。 因为每个人在使用容器时通常都有不同的需求,所以我们建议您查阅官方的Ansible文档以获取更多信息和docker_container
Ansible模块的用例。
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.
如果您想在此手册中包括其他任务以进一步自定义初始服务器设置,请参阅我们的Ansible入门指南Configuration Management 101:编写Ansible手册 。