Configuring LoadBalancer using Ansible with dynamic inventory

Configuring LoadBalancer using Ansible with dynamic inventory

Pre-requisites for the task:

  1. RHEL8 Installed in the systems and yum configured properly.
  2. Ansible installed in the configuration node.
  3. An active AWS account with IAM credentials.
  4. Network connectivity between the systems.

----------TASK----------

The key role for any ansible script is played by its configuration file which specifies the paths of the hosts, roles, permissions, and private-keys. The config file would be as below wherein the path of the roles and the manager hosts are specified.

No alt text provided for this image

Moving further, the roles are to be created which are capable of manage multiple scripts of ansible like the tasks, vars, files, templates, etc. Here 3 roles are created which are the following:

1.ec2:- This role is capable of dynamically creating an instance in the AWS cloud. This requires the AWS access key and the AWS secret access key so that the ansible can contact the API of the AWS.

No alt text provided for this image

Then we have to run the following command:

ansible-playbook lb.yml --ask-vault-pass
No alt text provided for this image

2.webserver: With the dynamic servers created, we need to configure them to work as web servers with the httpd package installed and hence started its services. The ansible script for the same is given below which are tasks of the role folder.

No alt text provided for this image

3. loadbalancer :- This role is capable of configuring the load balancer to install the haproxy software, configuring the config file of haproxy and finally starting its services. The config file should be updated to allocate the so created cloud instances or the webservers. This is done by using the template module of the ansible. The python code for this is mentioned below.

No alt text provided for this image

With the config file updated, the service has to be restarted to get the update in the services.

No alt text provided for this image

The tasks module of the ansible with the load balancer role looks as below:

No alt text provided for this image

With the updated config file transferred to the loadbalancer system , it looks as follows:

No alt text provided for this image

Here the only file that is to be run is the main ansible file that specifies what roles are to be performed on what hosts which is below:

- hosts: environment_ansible
  remote_user: ec2-user
  become: yes
  roles:
    - role: webserver


- hosts: loadbalancer 
  roles:
    - role: loadbalancer

Finally, the command we need to run is:

ansible-playbook set.yml
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image


To view or add a comment, sign in

Others also viewed

Explore content categories