#ansible #docker image to safely run my playbooks in few steps

Hi there, after some work on my ansible playbooks. I made a disaster on Mac and I got some issues with my Python libraries. Actually, my ansible stop to work. Then, I decided to apply what I’be been saying over my last posts. Use containers to encapsulate my apps and libraries. What a nice journey.

First, Install docker. Then, you’ll need to prepare your env creating a work space and independent ssh keys (for safety, I don’t want to have my personal keys in an image). I did the following:

mkdir ~/docker
cd ~/docker
mkdir -p ./var/log
mkdir -p ./var/tmp
mkdir ~/docker/.ssh
ssh-keygen -t rsa -b 4096 -C "dev@nuage.io" -f ~/docker/.ssh/id_rsa

Now, let’s build the image that will prepare our env to run our playbook and any Python module from nuage. This case, I am using the playbook I did to build your own SDN PoC on-premises. Then. don’t forget to copy the qcow2 images to ~/docker/tmp/ if you want to use it.

mkdir ~/docker/vcs-in-a-box
cd ~/docker/vcs-in-a-box
cat << 'EOF' >> Dockerfile
FROM centos:latest

# Install all packages
RUN yum -y install epel-release && yum clean all
RUN yum -y install python-pip && yum clean all
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 --upgrade pip
RUN pip install vspk
RUN pip install pexpect

# Generate user dev and his files
RUN useradd dev
WORKDIR /home/dev
ENV HOME /home/dev
ADD vimrc /home/dev/.vimrc
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

# install ansible playbook
RUN git clone https://github.com/p1nrojas/vcs-in-a-box /home/dev/vcs-in-a-box
RUN echo "log_path = /var/log/ansible/ansible-vsc-in-a-box.log" >> /home/dev/vcs-in-a-box/ansible.cfg

RUN chown -R dev: /home/dev
USER dev
EOF
cat << 'EOF' >> bash_profile
export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\$ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls='ls -GFh'
EOF
cat << 'EOF' >> vimrc
execute pathogen#infect()
syntax on
setlocal noautoindent
setlocal nocindent
setlocal nosmartindent
setlocal indentexpr=
EOF
docker build -t vcs-in-a-box .

Then, know you can create your instance and start using your ansible playbook. This case my instance is called “vcs01”.

# create the container
docker run -d -i -t --name vcs01 -v ~/docker/.ssh:/home/dev/.ssh -v ~/docker/var/log:/var/log/ansible -v ~/docker/var/tmp:/tmp vcs-in-a-box  /bin/bash
# access the container
docker exec -i -t vcs01 /bin/bash

Other option

If you want to keep your ansible files after destroying your container, then you can do the following
Create environment:

mkdir ~/docker
cd ~/docker
mkdir -p ./var/log
mkdir -p ./var/tmp
mkdir ~/docker/.ssh
ssh-keygen -t rsa -b 4096 -C "dev@nuage.io" -f ~/docker/.ssh/id_rsa
mkdir -p ~/docker/code
cd ~/docker/code
git clone https://github.com/p1nrojas/vcs-in-a-box ~/docker/code/vcs-in-a-box
echo "log_path = /var/log/ansible/ansible-vsc-in-a-box.log" >> ~/docker/code/vcs-in-a-box/ansible.cfg

And build the container like this

mkdir ~/docker/vcs-in-a-box
cd ~/docker/vcs-in-a-box
cat << 'EOF' >> Dockerfile
FROM centos:latest

# Install all packages
RUN yum -y install epel-release && yum clean all
RUN yum -y install python-pip && yum clean all
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 --upgrade pip
RUN pip install vspk
RUN pip install pexpect

# Generate user dev and his files
RUN useradd dev
WORKDIR /home/dev
ENV HOME /home/dev
ADD vimrc /home/dev/.vimrc
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 chown -R dev: /home/dev
USER dev
EOF
cat << 'EOF' >> bash_profile
export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\$ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls='ls -GFh'
EOF
cat << 'EOF' >> vimrc
execute pathogen#infect()
syntax on
setlocal noautoindent
setlocal nocindent
setlocal nosmartindent
setlocal indentexpr=
EOF
docker build -t vcs-in-a-box .

And then create the container that way:

docker run -d -i -t --name vcs01 -v ~/docker/.ssh:/home/dev/.ssh -v ~/docker/var/log:/var/log/ansible -v ~/docker/code/vcs-in-a-box:/home/dev/vcs-in-a-box -v ~/docker/var/tmp:/tmp vcs-in-a-boxc  /bin/bash

And access your container that way:

docker exec -i -t vcs01 /bin/bash

See ya!

One thought on “#ansible #docker image to safely run my playbooks in few steps

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