简体   繁体   中英

How to test Ansible playbook using Docker

I'm new to ansible (and docker). I would like to test my ansible playbook before using it on any staging/production servers.

Since I don't have access to an empty remote server, I thought the easiest way to test would be to use Docker container and then just run my playbook with the Docker container as the host.

I have a basic DockerFile that creates a standard ubuntu container. How would I configure the ansible hosts in order to run it against the docker container? Also, I suspect I would need to "run" the docker container to allow ansible to connect to it.

There's a working example regarding this: https://github.com/William-Yeh/docker-ansible

First, choose the base image you'd like to begin with from the following list:

  • 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

Second, put the following Dockerfile along with your playbook directory:

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

Third, docker build .

For more advanced usage, the role in Ansible Galaxy williamyeh/nginx also demonstrates how to do a simple integration test for a variety of Linux distributions on Travis CI 's Ubuntu 12.04 worker instances.

Disclosure: I am the author of the docker-ansible and wiliamyeh/nginx projects.

Running the playbook in a docker container may not actually be the best approach unless your stage and production servers are also Docker containers. The Docker ubuntu image is stripped down and will have some differences from a full installation. A better option might be to run the playbook in an Ubuntu VM that matches your staging and production installations.

That said, in order to run the ansible playbook within the container you should write a Dockerfile that runs your playbook. Here's a sample 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

The ansible inventory file would look like

[local]
localhost

Then you can just docker build . (where . is the root of the directory where your playbooks and Dockerfile live), then docker run on the resulting image.

Michael DeHaan, the CTO of Ansible, has an informative blog post on this topic .

I've created a role for this vary scenario: https://github.com/chrismeyersfsu/provision_docker . Easily start Docker containers and use them in your role or playbook, as inventory, to test.

Includes:

  • Curated Dockerfile for Ubuntu 12.04 & 14.04 as well as CentOS 6 & 7 that put back in the distro-removed init systems
  • start ssh

Also note the examples all have a .travis.yml file to form a CI pipeline using Travis CI.

Examples:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM