Automatic Webserver setup over RedHat using Ansible
What is Ansible?
Ansible is an open-source software provisioning, configuration management, and application-deployment tool. It is one of the most powerful enough to automate complex multi-tier IT application environments. To know more about Ansible you can go through this post >> What is Ansible and How it is so Powerful?
How to setup Ansible?
I have already created a post where you can learn how to set up the ansible successfully and we also troubleshoot some of the problems. Incase if there any error occurred you can comment down below or can connect with me over the social media handle as well. To know the setup part you can go through this post >> How to setup Ansible in RHEL8?
We are going to create one Ansible playbook that will set the following set up:
- Configure Docker.
- Start Docker.
- Pull httpd image from the Docker Hub.
- Launch httpd container and expose it to the public.
- Copy html code to the /var/www/html folder and start the web service.
Incase to check the ansible is successfully installed or not run
For the configuration process use
ansible all -m ping
If this is the output your ansible setup is ready and now we can proceed to our task.
This is used to define the host that we have set up in inventory file.
- hosts: dockerhost gather_facts: false
Task code here:
To config the yum for the docker we can use below code. This will configure the yum repository and we can be able to install required packages of docker.
tasks: - name: config yum for docker yum_repository: name: docker-repo baseurl: https://download.docker.com/linux/centos/7/x86_64/stable description: my docker repo gpgcheck: false
Here is the code to install the docker and start the docker. There is some confliction in docker community addition and that’s why we are using –nobest to forcefully download the docker.
- name: install docker command: "yum install docker-ce --nobest" - name: Starting docker services service: name: docker state: started enabled: yes
Here we also require a docker python library. Docekr-py is the library.
- name: installing docker-py pip: name: docker-py
To pull the httpd image from the Docker hub.
- name: installing http server docker_image: name: httpd source: pull
To copy the required html file that will be developed by the developer.
- name: Copying the html file copy: src: index.html dest: /var/www/html/
Launch the container and expose it to outer world.
- name: running docker container docker_container: name: webserver state: started exposed_ports: "80" image: httpd ports: 8080:80 volumes: /var/www/html/:/usr/local/apache2/htdocs command: httpd -D FOREGROUND
Paste the whole code in a .yaml file and this will our playbook and run
To test the webserver use http://<ip>:8080
We have finally successfully completed the above task. To go through the complete code you can go through my git hub repo
To connect with me over the social media you can go through below link