使用 Ansible 安装 Docker
Using Ansible to install Docker
前段时间我问了一个关于 Ansible 和 Docker 的问题并收到了 。我现在正试图更好地理解这个答案。本质上,我有一堆 Ubuntu 14.04 虚拟机,我希望 Ansible 在所有这些虚拟机上 install/maintain Docker。
我的 Ansible 项目:
myansible01.example.com:/opt/ansible/
site.yml
allservers.yml
roles/
common/
tasks/
main.yml
其中 site.yml
是:
---
# file: site.yml
- include: allservers.yml
其中 allservers.yml
是:
---
# file: allservers.yml
- hosts: all
user: {{ privileged_user }}
gather_facts: false
roles:
- common
而 roles/common/tasks/main.yml
是:
---
# file: roles/common/tasks/main.yml
- name: Add docker apt keys
apt_key: keyserver=keyserver.ubuntu.com id=36A1D7869245C8950F966E92D8576A8BA88D21E9
- name: Update apt
apt_repository: repo='deb https://get.docker.com docker main' state=present
- name: Install Docker
apt: pkg=lxc-docker update_cache=yes
最后,我的 /etc/ansible/hosts
:
[allservers]
server01.example.com
server02.example.com
server03.example.com
...etc.
server49.example.com
server50.example.com
这里有几个问题:
- 我实际上如何 运行 以便 Ansible 在所有 50 台服务器上安装 Docker?类似于(来自项目根目录)
ansible-playbook site.yml -f 10
?
- 如何将 "inject" 的
privileged_user
插入到上面的命令中?
- 为什么第一个任务“添加docker apt keys”是必要的?根据
apt_key
docs, we are fetching an apt_key
from the Ubuntu repos, but where does that long key come from? What role does this play in installing Docker (I ask because if you go to the Docker Ubuntu installation page 它根本没有提到这些 apt_keys
)?
- 为了正确 Docker 安装我还需要做些什么吗?
更合乎逻辑的顺序:
为什么 apt-key
?
apt-key
用于管理 apt
用来验证包的密钥列表(如 docker)。使用这些密钥进行身份验证的包将被视为受信任的。当您添加一个存储库,其中包含您想要的软件包的更新/更好/最新版本时,它很有用。
(来源:apt-key 的手册页)
如果不先添加repo的key,apt
就拿不到包,哭着跟"W: GPG error: ... The following signatures couldn't be verified because the public key is not available: NO_PUBKEY whatever key number".所以这基本上是为了让您可以获取软件包并安装它。
在 docker Ubuntu installation 页面中,注释中的 3 点明确提到了 apt-key
:
curl -sSL https://get.docker.com/gpg | sudo apt-key add -
变量
要在您的配置中注入 {{ privileged_user }}
:您可以通过在 <INVENTORY_FILE_LOCATION>/group_vars/all
中定义 privileged_user
来使用全局变量:
---
# Your user
privileged_user: "root"
如果此 group_vars/all
文件不存在,请创建它。
NB : the default inventory file location is /etc/ansible/hosts
, but /usr/local/etc/ansible/hosts
on Mac OS X
(请参阅 here 了解更多信息)
运行一本剧本
然后,运行 你的剧本,做:
ansible-playbook -l server49.example.com allservers.yml -v
-l
是如果你想特别限制一个服务器,但是你可以省略它
-v
是如果您想要详细的输出(或 -vv
、-vvv
...)
编辑:启动 docker 守护程序
如果你想确保 docker 守护程序在那之后启动,我建议无论如何重新启动它:
- name: Start Docker
service: name=docker state=restarted
根据发行版的不同,它可能以前是自动启动的,但据我在docs这里的理解,"Once Docker is installed, you will need to start the Docker daemon".
而且......你很高兴去;)
前段时间我问了一个关于 Ansible 和 Docker 的问题并收到了
我的 Ansible 项目:
myansible01.example.com:/opt/ansible/
site.yml
allservers.yml
roles/
common/
tasks/
main.yml
其中 site.yml
是:
---
# file: site.yml
- include: allservers.yml
其中 allservers.yml
是:
---
# file: allservers.yml
- hosts: all
user: {{ privileged_user }}
gather_facts: false
roles:
- common
而 roles/common/tasks/main.yml
是:
---
# file: roles/common/tasks/main.yml
- name: Add docker apt keys
apt_key: keyserver=keyserver.ubuntu.com id=36A1D7869245C8950F966E92D8576A8BA88D21E9
- name: Update apt
apt_repository: repo='deb https://get.docker.com docker main' state=present
- name: Install Docker
apt: pkg=lxc-docker update_cache=yes
最后,我的 /etc/ansible/hosts
:
[allservers]
server01.example.com
server02.example.com
server03.example.com
...etc.
server49.example.com
server50.example.com
这里有几个问题:
- 我实际上如何 运行 以便 Ansible 在所有 50 台服务器上安装 Docker?类似于(来自项目根目录)
ansible-playbook site.yml -f 10
? - 如何将 "inject" 的
privileged_user
插入到上面的命令中? - 为什么第一个任务“添加docker apt keys”是必要的?根据
apt_key
docs, we are fetching anapt_key
from the Ubuntu repos, but where does that long key come from? What role does this play in installing Docker (I ask because if you go to the Docker Ubuntu installation page 它根本没有提到这些apt_keys
)? - 为了正确 Docker 安装我还需要做些什么吗?
更合乎逻辑的顺序:
为什么 apt-key
?
apt-key
用于管理 apt
用来验证包的密钥列表(如 docker)。使用这些密钥进行身份验证的包将被视为受信任的。当您添加一个存储库,其中包含您想要的软件包的更新/更好/最新版本时,它很有用。
(来源:apt-key 的手册页)
如果不先添加repo的key,apt
就拿不到包,哭着跟"W: GPG error: ... The following signatures couldn't be verified because the public key is not available: NO_PUBKEY whatever key number".所以这基本上是为了让您可以获取软件包并安装它。
在 docker Ubuntu installation 页面中,注释中的 3 点明确提到了 apt-key
:
curl -sSL https://get.docker.com/gpg | sudo apt-key add -
变量
要在您的配置中注入 {{ privileged_user }}
:您可以通过在 <INVENTORY_FILE_LOCATION>/group_vars/all
中定义 privileged_user
来使用全局变量:
---
# Your user
privileged_user: "root"
如果此 group_vars/all
文件不存在,请创建它。
NB : the default inventory file location is
/etc/ansible/hosts
, but/usr/local/etc/ansible/hosts
on Mac OS X
(请参阅 here 了解更多信息)
运行一本剧本
然后,运行 你的剧本,做:
ansible-playbook -l server49.example.com allservers.yml -v
-l
是如果你想特别限制一个服务器,但是你可以省略它
-v
是如果您想要详细的输出(或 -vv
、-vvv
...)
编辑:启动 docker 守护程序
如果你想确保 docker 守护程序在那之后启动,我建议无论如何重新启动它:
- name: Start Docker
service: name=docker state=restarted
根据发行版的不同,它可能以前是自动启动的,但据我在docs这里的理解,"Once Docker is installed, you will need to start the Docker daemon".
而且......你很高兴去;)