Configuring docker containers with ansible via SSH

In this blog, I am going to use ansible to configure docker container with httpd server. Now using ssh in docker is unadvisable in general case but since ansible works on SSH protocol.

Now I created a container image called centos-ssh. I used a Key based authentication into the container image and here is the private key.

Since we have the container that has SSH server in it, now it is time to create the container and configure it with the help of ansible playbook

I have written a playbook that can launch a container and then retrive the IP from the JSON data and create an ansible inventory group with the help of add_host module in ansible.

mail.yaml

- hosts: localhost
vars_files:
- ./vars.yml
tasks:
- name: install docker.py for python client of docker
pip:
name: docker
- name: start docker
service:
name: docker
state: started
enabled: yes
- name: pulling the image with ssh pre-configured
docker_image:
name: smc181002/centos-ssh:latest
source: pull
- name: launch a container
docker_container:
name: "{{cname}}"
image: smc181002/centos-ssh:latest
labels:
type: webserver
- name: get all the runnning containers with webserver label
docker_host_info:
containers: yes
verbose_output: yes
containers_filters:
label: "type=webserver"
status: running
register: running_container
- name: add the containers to a group
add_host:
name: "{{item.NetworkSettings.Networks.bridge.IPAddress}}"
groups: webserver_containers
loop: "{{running_container.containers}}"
- name:
wait_for:
port: 22
- hosts: webserver_containers
tasks:
- name: "Install the httpd package"
package:
name: httpd
state: present
- name: copy the webserver files from localhost to the servers
copy:
src: ./pages/index.html
dest: /var/www/html/
- name: "start the httpd service"
shell: /usr/sbin/httpd

vars.yml

cname: webserver
  • In the above code, as you can see, I have first installed the docker.py package that helps us operate the docker containers from ansible playbook.
  • After that I started the docker service to configure the containers and then pulled the image that I have mentioned earlier
  • Then I created a container and added label to the container so that we can filter the containers that exist so that we can only add certain containers to our container list and then saved the output to running_container variable.
  • Now I need to add the container IP to the ansible inventory and this can be done with the help of add_host module from ansible
  • Since it may take some time to launch the ssh daemon in the container, I used wait_for to wait until the application on port 22 (ssh server) starts.
  • Now the container creation is done and it is time to configure the container with httpd server. The configuration is almost similar to my configurations that I have done in my previous blogs but since there is no systemctl in containers, we need to directly run the application and hence I used shell command to start the httpd program.

Below is the output of playbook execution

And below is the curl response to the IP of the container

Thank you and hope you have enjoyed the article and found it useful. 😃

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store