Creating Ansible role myapache and loadbalancer that dynamically configure Httpd WebServer on each system in Ansible Inventory inventory.
Mohit Singh Tomar
DevOps Engineer | Python | Flask | Ansible | Docker | Kubernetes | Jenkins | Openshift | Shell Scripting | IOT |
What is Ansible Role?
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.
(A) Role For Web Server -
- Generating or Creating a role for Web Server in default using the command below :-
ansible-galaxy role init myapache
Let's have a look on role directory :-
2. Create an php file called index.php in "file" directory inside the of myapache
3. write task in the tasks directory using yml file extension , in my case I am using main.yml file.
4. Writing variables in main.yml in the vars directory
(B) Role for Load Balancer:-
- Create role for Load Balancer
ansible-galaxy role init loadbalancer
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.
3. Adding tasks
4. Adding Handlers
Now Combining Combining both roles (myapache and loadbalancer) by creating setup.yml playbook
Updating Ansible Inventory
Now running the Playbook setup.yml using command given below :-
ansible-playbook setup.yml
Let's Check whether Web Service is running or not both instances.
As you can see it changing IP at every refresh which means our setup is working great.
Magento 2 Developer | Shopify Developer | Web Developer | Online Security Advisor | Cyber security Researcher | Tech Writter | Former GPCSSI Intern.
4 年Great