如何使用Docker测试Ansible playbook

And*_*dre 17 ansible docker

我是ansible(和docker)的新手.我想在任何临时/生产服务器上使用之前测试我的ansible playbook.

由于我无法访问空的远程服务器,我认为最简单的测试方法是使用Docker容器,然后以Docker容器作为主机运行我的playbook.

我有一个基本的DockerFile,可以创建一个标准的ubuntu容器.我如何配置ansible主机以便在docker容器上运行它?此外,我怀疑我需要"运行"docker容器以允许ansible连接到它.

Wil*_*Yeh 15

关于这个有一个有效的例子:https://github.com/William-Yeh/docker-ansible

首先,从以下列表中选择您要开始的基本图像:

  • williamyeh/ansible:debian8-onbuild
  • williamyeh/ansible:debian7-onbuild
  • williamyeh/ansible:ubuntu14.04-onbuild
  • williamyeh/ansible:ubuntu12.04-onbuild
  • williamyeh/ansible:centos7-onbuild
  • williamyeh/ansible:centos6-onbuild

其次,将以下内容Dockerfile与您的playbook目录一起放入:

FROM williamyeh/ansible:ubuntu14.04-onbuild

# ==> Specify playbook filename;   default = "playbook.yml"
#ENV PLAYBOOK   playbook.yml

# ==> Specify inventory filename;  default = "/etc/ansible/hosts"
#ENV INVENTORY  inventory.ini

# ==> Executing Ansible...
RUN ansible-playbook-wrapper
Run Code Online (Sandbox Code Playgroud)

第三, docker build .

对于更高级的用法,Ansible Galaxy中的角色williamyeh/nginx还演示了如何对Travis CI的Ubuntu 12.04工作器实例上的各种Linux发行版进行简单的集成测试.

披露:我的作者docker-ansiblewiliamyeh/nginx项目.


Ben*_*ley 14

除非您的舞台和生产服务器也是Docker容器,否则在docker容器中运行playbook实际上可能不是最好的方法.Docker ubuntu映像被剥离,与完整安装有一些区别.更好的选择可能是在与您的登台和生产安装相匹配的Ubuntu VM中运行playbook.

也就是说,为了在容器中运行ansible playbook,你应该编写一个运行你的playbook的Dockerfile.这是一个示例Dockerfile:

 # Start with the ubuntu image
 FROM ubuntu
 # Update apt cache
 RUN apt-get -y update
 # Install ansible dependencies
 RUN apt-get install -y python-yaml python-jinja2 git
 # Clone ansible repo (could also add the ansible PPA and do an apt-get install instead)
 RUN git clone http://github.com/ansible/ansible.git /tmp/ansible

 # Set variables for ansible
 WORKDIR /tmp/ansible
 ENV PATH /tmp/ansible/bin:/sbin:/usr/sbin:/usr/bin
 ENV ANSIBLE_LIBRARY /tmp/ansible/library
 ENV PYTHONPATH /tmp/ansible/lib:$PYTHON_PATH

 # add playbooks to the image. This might be a git repo instead
 ADD playbooks/ /etc/ansible/
 ADD inventory /etc/ansible/hosts
 WORKDIR /etc/ansible

 # Run ansible using the site.yml playbook 
 RUN ansible-playbook /etc/ansible/site.yml -c local
Run Code Online (Sandbox Code Playgroud)

ansible库存文件看起来像

[local]
localhost
Run Code Online (Sandbox Code Playgroud)

然后你可以docker build .(.你的剧本和Dockerfile所在的目录的根目录),然后docker run在结果图像上.

Ansible首席技术官迈克尔德哈恩(Michael DeHaan)就此主题撰写了一篇内容丰富的博客文章.

  • 谢谢.所以基本上你从docker容器中部署"本地",而不是容器是"主机".我没有想到这一点.我明天晚上会试一试,看看它是如何适合我的用例的.也感谢您的提醒,这可能不是最好的方法!将考虑到这一点. (2认同)

Ewa*_*Ewa 5

除了配置 localhost(安装了 Ansible 的机器)之外,您还可以告诉 Ansible:

  1. 创建一个新的docker容器,
  2. 提供该容器,
  3. 销毁那个容器。

为此,您需要这样一个hosts.yaml文件:

all:
  hosts:
    mycontainer:
      ansible_connection: docker
    localhost:
      ansible_connection: local
Run Code Online (Sandbox Code Playgroud)

这样一个playbook.yaml文件:

---
- name: Create a container to be provisioned later
  hosts: localhost
  tasks:
    - name: create docker container
      docker_container:
        name: mycontainer
        image: python:2.7.16-slim-stretch
        command: ["sleep", "1d"]

- name: Provision the container created above
  hosts: mycontainer
  roles:
    - simple
Run Code Online (Sandbox Code Playgroud)

和另一个剧本文件:destroy.yaml用于销毁容器:

---
- name: Destroy a container
  hosts: localhost
  tasks:
    - name: destroy docker container
      docker_container:
        name: mycontainer
        state: absent
Run Code Online (Sandbox Code Playgroud)

还创建一个简单的角色:roles/simple/taksks/main.yaml

---
- name: Create a file
  copy:
    content: "hi!!"
    dest: /tmp/hello
    force: yes
    mode: 0555
Run Code Online (Sandbox Code Playgroud)

现在要创建一个容器并配置它,请运行:

ansible-playbook -i ./hosts.yaml ./playbook.yml
Run Code Online (Sandbox Code Playgroud)

验证容器是否已配置(文件已创建):

docker exec mycontainer cat /tmp/hello
Run Code Online (Sandbox Code Playgroud)

要销毁容器,请运行:

ansible-playbook -i ./hosts.yaml ./destroy.yml
Run Code Online (Sandbox Code Playgroud)

当然也有缺点:

  • 容器必须安装了 python
  • 某些 Ansible 模块可能无法工作,因为必须安装额外的 python 软件包。例如,如果你想部署 docker 容器(在 docker 容器中),你必须安装 docker python SDK ( pip3 install docker)

我受到这篇博文的启发:https://medium.com/@andreilhicas/provision-docker-containers-with-ansible-30cc5ee6d950