我是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-onbuildwilliamyeh/ansible:debian7-onbuildwilliamyeh/ansible:ubuntu14.04-onbuildwilliamyeh/ansible:ubuntu12.04-onbuildwilliamyeh/ansible:centos7-onbuildwilliamyeh/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-ansible和wiliamyeh/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)就此主题撰写了一篇内容丰富的博客文章.
除了配置 localhost(安装了 Ansible 的机器)之外,您还可以告诉 Ansible:
为此,您需要这样一个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)
当然也有缺点:
pip3 install docker)我受到这篇博文的启发:https://medium.com/@andreilhicas/provision-docker-containers-with-ansible-30cc5ee6d950
| 归档时间: |
|
| 查看次数: |
10542 次 |
| 最近记录: |