Configuring LoadBalancer using Ansible with dynamic inventory
Pre-requisites for the task:
- RHEL8 Installed in the systems and yum configured properly.
- Ansible installed in the configuration node.
- An active AWS account with IAM credentials.
- 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.
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.
Then we have to run the following command:
ansible-playbook lb.yml --ask-vault-pass
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.
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.
With the config file updated, the service has to be restarted to get the update in the services.
The tasks module of the ansible with the load balancer role looks as below:
With the updated config file transferred to the loadbalancer system , it looks as follows:
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