Setting Haproxy On Aws Using Power Of Ansible

Gupta Aditya
4 min readDec 24, 2020

--

So hey guys hope you all are doing in today's article we are going to see how to set up a haproxy server over was using ansible.

If you have not check my previous article on setting up haproxy then do check it out because that I have explained everything in detail in this article we are just going to see how setup it on Aws in that we have launched on the local server. If you have not read yet click below to read it.

Firstly to do something on AWS instance we need one security and transfer it to your system wherever your ansible control node running to send key you can use WinSCP tool once key is transferred note down its path,aws instance IP and also note down by default user of aws instance so that ansible can do ssh because we are not allowed to ssh directly to root we first go to some default user they use sudo us- command to go to the root user.

That's it we only need these three things for connecting to aws instance from ansible so that we can deploy our playbook that we used in previous articles stated above we are going to use exactly the same playbook and technique as we did in Setting Haproxy Using Power Of Ansible☺ only change is there we use local IP and here we are using aws instance that's it.

Let's move toward practical now

Step1 Open the inventory file and write all the three information that we note above in the following way as shown in the picture below here we have make a host group for ease not mandatory for you.

Once this is done save it and close it now our system can ping to this IP when you use the command:-

ansible IP -m ping

because ping, ls types of command not require superpower to run but if you run playbook or command to install some package then it will show error this is because we are giving command from ec2 user but this command need superpower so either we should in root account or we should use sudo but how ansible decide all these things you might be thinking?

so here privilege escalation comes into the picture we can define this in the playbook or in configuration here are we are defining in configuration(when you define in cfg file then all the playbook in which privilege escalation is not declared they use privilege escalation declared in configuration file).

As you can see in below image we have added privilege escalation in the cfg file in a similar way you to add this in your cfg file

Inside privilage_esclation become = True means they have to use the method defined below

become_method=sudo

this means they have to use the sudo command for changing user

become_user=root

this means username to which you want to change user

sometime there are os which required a password whenever we use sudo but aws gen don't so we comment that line.

Once this is done save it and close it we are all done now to run deploy haproxy playbook only we have to change the host in the playbook that's it and we are done.

haproxy running on aws
haproxy running on aws

As you can above haproxy is running on aws instance setup by ansible.

All code for playbook is in Github repo attached below

Guys, here we come to the end of this blog I hope you all like it and found it informative. If have any query feel free to reach me :)

Github link:-https://github.com/guptaadi123/Ansible.git

Guys follow me for such amazing blogs and if have any review then please let me know I will keep those points in my mind next time while writing blogs. If want to read more such blog to know more about me here is my website link https://sites.google.com/view/adityvgupta/home.Guys Please do not hesitate to keep 👏👏👏👏👏 for it (An Open Secret: You can clap up to 50 times for a post, and the best part is, it wouldn’t cost you anything), also feel free to share it across. This really means a lot to me.

--

--

No responses yet