Creating Ansible role myapache and loadbalancer that dynamically configure Httpd WebServer on each system in Ansible Inventory inventory.

Creating Ansible role myapache and loadbalancer that dynamically configure Httpd WebServer on each system in Ansible Inventory inventory.

What is Ansible Role?

No alt text provided for this image

Roles let you automatically load related vars_files , tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users.

Task Description -

  • Create an ansible role myapache to configure Httpd WebServer.
  • Create another ansible role myloadbalancer to configure HAProxy LB.
  • We need to combine both of these roles controlling webserver versions and solving challenge for host IPs addition dynamically over each Managed Node in HAProxy.cfg file.

Note:- I'm using 3 AWS EC2 Instance as my target node, of which one is for load Balancer and other two are for Web Server.

No alt text provided for this image

(A) Role For Web Server -

  1. Generating or Creating a role for Web Server in default using the command below :-
ansible-galaxy role init myapache
No alt text provided for this image

Let's have a look on role directory  :-

No alt text provided for this image

2. Create an php file called index.php in "file" directory inside the of myapache

No alt text provided for this image

3. write task in the tasks directory using yml file extension , in my case I am using main.yml file.

No alt text provided for this image

4. Writing variables in main.yml in the vars directory

No alt text provided for this image

(B) Role for Load Balancer:-

  1. Create role for Load Balancer
ansible-galaxy role init loadbalancer


No alt text provided for this image

2. For configuring the Load Balancer, We need to configure the file (haproxy.cfg ) of haproxy. So, we have to put the template of haproxy.cfg in the template directory.

No alt text provided for this image
No alt text provided for this image

3. Adding tasks

No alt text provided for this image

4. Adding Handlers

No alt text provided for this image

Now Combining Combining both roles (myapache and loadbalancer) by creating setup.yml playbook

No alt text provided for this image

Updating Ansible Inventory

No alt text provided for this image

Now running the Playbook setup.yml using command given below :-

ansible-playbook setup.yml


No alt text provided for this image
No alt text provided for this image

Let's Check whether Web Service is running or not both instances.

No alt text provided for this image
No alt text provided for this image

As you can see it changing IP at every refresh which means our setup is working great.

Task Completed........!!!!!!!!

Thanks For Reading....................

No alt text provided for this image


Nimish Agarwal

Magento 2 Developer | Shopify Developer | Web Developer | Online Security Advisor | Cyber security Researcher | Tech Writter | Former GPCSSI Intern.

4 年

Great

要查看或添加评论,请登录

Mohit Singh Tomar的更多文章

社区洞察

其他会员也浏览了