LAUNCH WEB SERVER ON THE TOP OF DOCKER USING ANSIBLE PLAYBOOK
Nishant Singh
Senior Software Engineer@HCL Tech | Red Hat Certified System Administrator | AWS Certified Solution Architect-Associate | AWS Certified Developer Associate | AWS Cloud Practitioner Certified
What is Ansible?
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually. Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency. And Ansible, as noted above, is rapidly rising to the top in the world of automation tools.
Advantages of Ansible:
- Free: Ansible is an open-source tool.
- Very simple to set up and use: No special coding skills are necessary to use Ansible’s playbooks (more on playbooks later).
- Powerful: Ansible lets you model even highly complex IT workflows.
- Flexible: You can orchestrate the entire application environment no matter where it’s deployed. You can also customize it based on your needs.
- Agentless: You don’t need to install any other software or firewall ports on the client systems you want to automate. You also don’t have to set up a separate management structure.
- Efficient: Because you don’t need to install any extra software, there’s more room for application resources on your server.
What is Docker?
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.
Advantages of Using Ansible With Docker:
Ansible does a great job of automating Docker and operationalizing the process of building and deploying containers. If you’re managing a traditional IT system, for example, it can be hard to add container-tooling functionality. But Ansible removes the need to do processes manually. There are four main advantages of using Ansible with Docker:
1) Portability/Flexibility
The fact that Ansible playbooks are portable, meaning they can be used anywhere, as well as repeatable, can save you a lot of time and effort. For example, if you use a pure Dockerfile to build a container, then you can reproduce the application only in a Docker container. If you use an Ansible playbook to create a container, on the other hand, then you can reproduce the application in Docker, on the cloud, and so on.
2) Auditability
Even if you create containers, you’ll still need to monitor code and track vulnerabilities. Using Ansible with Docker, you can easily track who has deployed which containers as well as what’s in all of the containers, and know that you can rebuild any containers as necessary.
3) Management of Entire Environments
With Ansible, you already know you can manage your Docker containers. But you can also maintain the environment that all the containers are in, even in highly complex environments. Ansible can monitor containers and non-containers at the same time, which is essential because containerized applications often need to “talk” with noncontainerized applications.
4) Similar Syntax
As mentioned, Ansible used YAML files for its playbooks. Docker uses its non-YAML scripts, but they are very similar and can do almost the same things.
Problem Statement:
Write an Ansible Playbook that does the following operations in the managed nodes:
?? Configure Docker
?? Start and enable Docker services
?? Pull the httpd server image from the Docker Hub
?? Run the httpd container and expose it to the public
?? Copy the html code in /var/www/html directory and start the web server
Solution:
Before doing the task you have to download and configure the inventory of ansible. Type this command in your vm it will download the ansible for you.
pip3 install ansible
Now we have to make random name file in my case i make a file named /etc/myhosts.txt and write your other virtual machine ip (vm in which you want to configure and setup the docker) and other things like root and password etc...
Now check the ansible version
Acoording to above image ansible see its repository in /etc/ansible/ansible.conf file so configure this file.
See all the hosts by typing ansible all --list-hosts
Ping to the host to see there is ssh connectivity between both the virtual machine or not.
Now you can see that there is the connectivity between both the virtual machines.
1) Firstly you need to share the ISO file at /opt folder which is already copied in my case.
- hosts: all vars: - source_iso: "isofile/rhel-8.0-x86_64-dvd.iso" - dest_iso: "/opt/rhel-8.0-x86_64-dvd.iso" - repo_mount_path: "/mnt/dvd" tasks: - name: Copy the ISO file to remote location copy: src: "{{ source_iso }}" dest: "{{ dest_iso }}" force: no
2) In this step I have to mount this ISO permanent so after reboot it will not unmount
- name: Mount the ISO file permanently lineinfile: dest: /etc/rc.d/rc.local line: mount /opt/rhel-8.0-x86_64-dvd.iso /mnt/dvd state: present create: yes
3) Now make this mount executable by typing chmod +x /etc/rc.d/rc.local.
- name: Make the file executable command: chmod +x /etc/rc.d/rc.local
4) Now we have to configure the yum.
i) Setup AppStream repository
- name: Add repository yum_repository: name: dvd1 description: AppStream repository baseurl: "file://{{repo_mount_path}}/AppStream" enabled: yes gpgcheck: no
ii) Setup BaseOS repository
- name: Add_repository yum_repository: name: dvd2 description: BaseOS repository baseurl: "file://{{repo_mount_path}}/BaseOS" enabled: yes gpgcheck: no
5) Setup the docker repository
- name: Add_repository yum_repository: name: docker description: Docker repo baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/" gpgcheck: no
6) Now install the docker software
- name: Install Docker package: name: "docker-ce-18.06.3.ce-3.el7.x86_64" state: present
7) Start the docker service
- name: Start the Docker service service: name: "docker" state: started
8) Install the Python Docker module via pip.
- pip: name: docker
9) Pull the httpd image from docker hub.
- name: Pull the httpd Docker Image from dockerhub docker_image: name: "httpd" source: pull
11) Copy the webpage from configuration node to manage node
- name: copy webpage from CN to MN copy: src: "/root/ansiblews/webpage.html" dest: "/home"
12) Container creating using httpd image
- name: Container creation docker_container: name: webos image: httpd state: started ports: - "7000:80" volumes: - /home:/usr/local/apache2/htdocs/
Now this is my complete ansible playbook for launching the web server.
- hosts: all vars: - source_iso: "isofile/rhel-8.0-x86_64-dvd.iso" - dest_iso: "/opt/rhel-8.0-x86_64-dvd.iso" - repo_mount_path: "/mnt/dvd" tasks: - name: Copy the ISO file to remote location copy: src: "{{ source_iso }}" dest: "{{ dest_iso }}" force: no - name: Mount the ISO file permanently lineinfile: dest: /etc/rc.d/rc.local line: mount /opt/rhel-8.0-x86_64-dvd.iso /mnt/dvd state: present create: yes - name: Make the file executable command: chmod +x /etc/rc.d/rc.local - name: Add repository yum_repository: name: dvd1 description: AppStream repository baseurl: "file://{{repo_mount_path}}/AppStream" enabled: yes gpgcheck: no - name: Add_repository yum_repository: name: dvd2 description: BaseOS repository baseurl: "file://{{repo_mount_path}}/BaseOS" enabled: yes gpgcheck: no - name: Add_repository yum_repository: name: docker description: Docker repo baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/" gpgcheck: no - name: Install Docker package: name: "docker-ce-18.06.3.ce-3.el7.x86_64" state: present - name: Start the Docker service service: name: "docker" state: started - pip: name: docker - name: Pull the httpd Docker Image from dockerhub docker_image: name: "httpd" source: pull - name: copy webpage from CN to MN copy: src: "/root/ansiblews/webpage.html" dest: "/home" - name: Container creation docker_container: name: webos image: httpd state: started ports: - "7000:80" volumes: - /home:/usr/local/apache2/htdocs/
when I run this playbook by typing ansible-playbook task1.yml. It will give the output like this.
Final webpage output:
Its 2nd part Link(It is not fully automated but I tried as much as I made it automated): LAUNCH THE KUBERNETES CLUSTER BY ANSIBLE PLAYBOOK
Github url:
Thanks For Reading!!!
RHCE | ARTH learner | ISCP Certified
4 年Well Depicted ??