Webserver Configuration Inside Docker Container By Auto-Updating Ansible Inventory File

Webserver Configuration Inside Docker Container By Auto-Updating Ansible Inventory File

·

5 min read

Task - Create An Ansible Playbook that will Retrieve New Container IP and Update the Inventory. So further Configuration of Web server could be done inside that Container.

So here we have a task:-

  • First, we have to launch a Docker container.
  • Then we have to retrieve container IP and save it to the inventory.
  • And then using Ansible we have to configure the Docker container.

But there's a slight problem if we want to configure docker container Ansible uses ssh and ssh is by default not enabled in the container, we have two choices we can use a non-official image where the ssh is already enabled / we can create our own image where we have already configured ssh or we can create a single Ansible-Playbook which will do everything for us in one single go.

Here I have already created a Docker Image in which ssh is enabled you can search for any other image in the hub.docker.com

So here are my files:-

contents.jpg

home.html:-

home.jpg

My dockerIp.yml:-

- hosts: localhost
  tasks:
          - name: Stopping SElinux Services
            selinux:
                    policy: targeted
                    state: permissive

          - name: Starting Docker Services
            service: 
                    name: docker
                    state: started
                    enabled: yes

          - name: Starting Docker Container
            docker_container:
                    name: sshos
                    image: myos/ssh
                    state: started
                    interactive: yes
                    command: /usr/sbin/sshd -D

          - name: Retrieving Docker IP
            docker_container_info:
                    name: sshos
            register: result

          - debug:
                    var: result.container.NetworkSettings.IPAddress

          - name: Updating Inventory File By Docker Container IP
            blockinfile:
                    dest: "/root/Extra/ip.txt"
                    block: |
                            [docker]
                            {{ result['container']['NetworkSettings']['IPAddress'] }} ansible_user=root ansible_password=docker123 ansible_connection=ssh

          - name: Refreshing Inventory
            meta: refresh_inventory

- hosts: docker
  tasks:
          - name: Installing Packages
            package:
                   name: httpd
                   state: present

          - name: Starting webserver
            command: /usr/sbin/httpd

          - name: Copying
            copy:
                   src: 'home.html'
                   dest: '/var/www/html/index.html'

This playbook will run in two phases:-

Phase 1:-

Ansible will launch the container from my custom docker image which I've created with the CentOS and it will start the docker services in our computer.

Even with the ssh configured in the image when we launch a new container from the image at that time the ssh service is stopped so before configuring anything in the docker we have to start ssh service that why I have used command: /usr/sbin/sshd -D in the code -D will run this service alongside the bash .

And in the end the code will retrieve the Docker IP and add it to the Inventory with the group name Docker and ansible will refresh the Inventory.

Phase 2:-

In this phase, Ansible will retrieve the Docker IP and start configuring it. First, it will install httpd package and start its services and then copy html files from our computer to the container.

So when we run the program:-

running 1.jpg

running 2.jpg

iptext.jpg

Now we check by searching the Docker IP in Browser:-

browser.jpg

We can see this web-page this means the task is successful, I would like to thanks Mr Vimal Daga sir for providing such information.