data-only containers for ansible automation

Hi, I did a playbook to create my PoCs much faster (from days to one hour). And got some issues with my Mac. My python got broken :/

Then I’ve decided to manage my playbook thru containers (Check my project here). However, managing data from those, brings a portability issue. Then, to make it simple. I started to use data-only containers.

I’ve got the the idea from posts:

My Dockerfile

I defined this docker file:

FROM centos:7.3.1611

# Install all packages
RUN yum -y install epel-release && yum clean all
RUN curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
RUN python get-pip.py
RUN yum -y install ansible && yum clean all
RUN yum -y install vim && yum clean all
RUN yum -y install curl && yum clean all
RUN yum -y install git && yum clean all

# install sdk from nuage
RUN pip install vspk
RUN pip install pexpect
RUN pip install packet-python
RUN pip install django

# Generate user dev and his files
RUN useradd dev
RUN mkdir -p /home/dev/.ssh
RUN mkdir -p /home/dev/images
RUN mkdir -p /home/dev/packet-nuagevns
RUN mkdir -p /var/log/ansible
RUN chown -R dev:dev /home/dev
RUN chown -R dev:dev /var/log/ansible
VOLUME /home/dev/packet-nuagevns
VOLUME /home/dev/.ssh
VOLUME /var/log/ansible
WORKDIR /home/dev
ENV HOME /home/dev
ADD vimrc /home/dev/.vimrc
ADD setup.sh /home/dev/setup.sh
ADD .packet_token /home/dev/.packet_token
ADD .packet_project_id /home/dev/.packet_project_id
ADD .nuage_license_key /home/dev/.nuage_license_key
RUN chmod 755 /home/dev/setup.sh
ADD bash_profile /home/dev/.bash_profile
RUN mkdir -p /home/dev/.vim/autoload /home/dev/.vim/bundle
RUN curl -LSso /home/dev/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
RUN git clone git://github.com/chase/vim-ansible-yaml.git  /home/dev/.vim/bundle/vim-ansible-yaml
RUN git clone https://github.com/lepture/vim-jinja.git /home/dev/.vim/bundle/vim-jinja
RUN git clone git://github.com/ansible/ansible.git --recursive /home/dev/ansible

RUN chown -R dev: /home/dev
USER dev
CMD /home/dev/setup.sh

You can see that I’ve defined three volumes:

VOLUME /home/dev/packet-nuagevns
VOLUME /home/dev/.ssh
VOLUME /var/log/ansible

Those volumes are persistent among containers. I built my image thru

docker build -t p1nrojas/packet-nuagevns . >> /tmp/install.log

Now, creates you container as the following. Data-only container don’t have to be running:

docker run -d --name vns-data-only p1nrojas/packet-nuagevns true

I’ve created a setup.sh file to manage the first action on it, and install my app files.

#!/bin/bash

if [ ! -f /var/log/ansible/ansible-packet-nuagevns.log ]; then
    echo "There is not data. Proceed setup"
    git clone https://github.com/p1nrojas/packet-nuagevns ~/packet-nuagevns
    touch /var/log/ansible/ansible-packet-nuagevns.log
    ssh-keygen -t rsa -b 4096 -C "dev@nuage.io" -f ~/.ssh/id_rsa -q -N ""
    cd /home/dev/packet-nuagevns
    ansible-playbook build.yml
    /bin/bash
else
    echo "Caution: user data detected. skipping setup. Next time override CMD"
    /bin/bash
fi

And then, you just need to run the follow to install your files.

docker run -d -i -t --volumes-from vns-data-only --name vns-packet p1nrojas/packet-nuagevns

You can run as many container you want on that data-only container.

See ya!

One thought on “data-only containers for ansible automation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s