Creating an Ansible playbook that will retrieve new Container IP and update the inventory. So that further Configuration of Web server could be done
Mohit Singh Tomar
DevOps Engineer | Python | Flask | Ansible | Docker | Kubernetes | Jenkins | Openshift | Shell Scripting | IOT |
In this Article, I'll show you how to create an Ansible playbook that will retrieve new Container IP and update the inventory. So that further Configuration of Web server could be done inside that Container.
In this Article we will expand some dynamism in task-10 of Arth whose link is provided below -->
In this article, I am going to.
- Dockerize SSH - Install SSH and Enable SSH Service in Docker
- Configure WebServer in Docker with Ansible
- As we already know , Ansible works on SSH protocol , and we need to enable it while working with Ansible.
So as far as we know that , in the beginning docker container don't have SSH allow , for that we need to install some software for which I had made custom docker image with some modification or configuration in centos that includes all software pre-installed and configured.
Pre-Requisite :
- Ansible Installed
Let's deep dive into the practical :-
1- First of all we'll make custom docker image from different system called (A) on which docker is already installed.
(A) Launching Docker Container
Use following command to an interactive terminal with the container name and maps the port using concept of Patting.
docker run -it -p 8080:80 --name <Container_Name> <docker_image_name>:<version>
where,
-t :- assigns a terminal inside the container.
-i :- create a interactive connection with container by grabbing STDIN.
-p :- maps the host’s 8080 port with 80 port of container
<Container_Name> :- defines the name of container
<docker_image_name>:<version> :- defines the docker which is used to launch container
docker run -it -p 8080:80 --name web centos:latest
(B) Configuring SSH in Docker Container :-
We need to install SSH , because Ansible works on it , so that Ansible will configure it via SSH. Use following command to install ssh properly :-
yum install openssh-server openssh-clients passwd -y
Now generate ssh host key for further configuration of ssh.
The following command will do this: For each of the key types (rsa, dsa, ecdsa and ed25519) for which host keys do not exist, generate the host keys with the default key file path, an empty passphrase, default bits for the key type, and default comment.
ssh-keygen -A
While working with SSH we need user and password , but by-default docker container don't have any password , so for that we need to install passwd and set the password for any user ( if we created) otherwise for root.
passwd root
Use command describe above to set password of root user of docker container.
Note:- As we know systemctl command doesn't work inside the Docker Container , so for that we need to find which file will start this services and become permanent.
For SSH we have to run /usr/sbin/sshd with these options: do not detach (-D), log to stderr (-e), passthrough other arguments. After that we need to make SSH service permanent , for that we need to go inside the the /root/.bashrc file because the file will run when we restart the container so our service also start again , and whatever inside this file will run at the time of restarting.
Now run get out of docker container to host system and run followinf command to check IP of running docker container which we have configured.
docker inspect web | grep IP
Here we can see that the IP of Docker container is "172.17.0.2" . And now we know the password for root , as we have already set .
Now check the SSH connectivity to docker container from host system.
Here we can wee that , SSH is working great !!!! and we finally landed inside the container.
Now stop the docker container called web.
(C) Making Custom Image and Pushing to Docker Hub :-
Use following command to login to docker hub from host system :-
docker login -u DOCKERHUB_USERNAME -p DOCKERHUB_PASSWORD
Now commit web container by using following command :-
docker commit web centosssh:latest
Now tag image by using following command :-
docker tag image_name:latest docker_username/repository:tagname
After tagging push the image to your docker hub by following command :-
docker push docker_username/repository:tagname
Docker Custom image is build and pshed to Docker Hub....
2- Now we'll install docker, launch container and fetch its IP dynamically and update the inventory without going inside it , and also do web server configuration in it using Ansible on different computer (B).
(A) Create HTML File:-
Create html file using command given below in the cureent working directory called "Arth-Task-14.2" :-
vim index.html.j2
Add following lines in "index.html.j2" file
"<h1>Hey I'm Mohit Singh Tomar. You are in Docker</h1>"
(B) Create Ansble Playbook -
Create Ansible Plyabook (lets say dock.yml) which consist of following line in it :-
- hosts: localhost tasks: - name: "Downloading and Installing Python-3" package: name: python3 state: present - name : "Downloading and Installing Downloading docker-py " pip: name: docker state: present - name: "Downloading and Installing Docker" package: name: docker state: present - name: "Starting Docker Service" service: name: "docker" state: started enabled: yes - name: "Pulling Custom Docker Image" docker_image: name: mohitsinghtomar/centosssh:latest source: pull - name: "Launching Docker Custom Centos Container" docker_container: name: "WebOS" image: "mohitsinghtomar/centosssh:latest" exposed_ports: "80" ports: "8080:80" state: started interactive: yes register: result - name: "Does container exist?" debug: var: result.container.NetworkSettings.IPAddress - name: "Updating Inventory of Ansible with Docker Container IP Address" blockinfile: path: /etc/ansible/hosts block: | [docker] {{ result.container.NetworkSettings.IPAddress }} ansible_ssh_user=root ansible_ssh_pass=redhat - hosts: docker tasks: - name: "Installing httpd or Apache Server" package: name: "httpd" state: present - name: "Copying Webpages" template: src: "/Arth-Task-14.2/index.html.j2" dest: "/var/www/html/index.html" register: x - name: "Statring HTTPD" command: "/usr/sbin/httpd"
(C) Run above playbook :-
Here in the image you can see there is no host in our ansible before running playbook.
Now, I'll going to run " dock.yml " using following command, It will install docker, launch container along with fetcing IP and dynamically adding to ansible inventory and configure webserver.
ansible-playbook dock.yml
Here we can see that our playbook run successfully .
Now I can show you updated host file .
Now lets check whether the web server of docker is running or not :-
It's working fine.
That's all, The Task is done.
Thanks for reading. . . . . .