Ansible: Working with Ansible Galaxy to use pre-written role to configure Nginx Webserver

Share At:

In continuation to my previous article titled “Ansible: Writing ansible-playbook to update “Motd” to display banner with Facts”, here we are going to see how to use Ansible Galaxy to download pre-written Nginx rolewrite a playbook and install Nginxon client servers.

Pre-Requisites:

  1. In this demonstration, we will be using centos-07. Please go through the Ansible documentation for supported OS platforms.
  2. We will be using 3 machines for our lab: 1 ansible workstation and 2 ansible clients with the below IP details:

192.168.33.50 ansibleworkstation.unixlab.com
192.168.33.51 webserver.unixlab.com
192.168.33.52 applicationserver.unixlab.com

Note: Ansible workstation would be our ansible server.

3. For all the servers — memory should be at least 2 GB and there should be at least 2 core CPUs.

4. Ansible workstation and client servers should be able to communicate with each other over ssh (port 22).

5. We have already installed the ansible package on the ansible workstation and have created an inventory file — /etc/ansible/hosts.

The inventory file “/etc/ansible/hosts” should have entry like below:

Working with Ansible Galaxy to Configure Nginx:

Here we will learn to use pre-written roles available in Ansible Galaxy and write a playbook to configure Nginx on client servers.

Please perform the below steps:

  1. Visit “https://galaxy.ansible.com/” and click on Search. Now type nginx in the search bar. The first thing you will see is “nginx_core”.

2. Now Click on nginx_core and click on the copy icon next to Installation.

3. Now login to Ansible Workstation and paste the installation command we just copied from Ansible Galaxy. The roles will be installed in “/home/vagrant/.ansible/collections/ansible_collections/nginxinc/nginx_core/roles” directory. It should be something like below.

4. Now copy the nginx role to the following path “/home/vagrant/repo/ansible-automation/roles” :

5. Now we are going to update our playbook called “webserver.yml” which we have created in our previous tutorial for Motd. We will be adding nginx role to our playbook.

For this, go to “/home/vagrant/repo/ansible-automation” directory and Update the webservers.yml file to look something like below:

6. Use “ansible-playbook webservers.yml — check” to perform the dry runThis will not perform the actual installation of “nginx” on client servers.

7. Now run “ansible-playbook webservers.yml” command to run the playbook and perform actual installation:

8. Now login to our client-server called “webserver” and validate if Nginx services running. If successful, you will see something similar to this:

9. Running curl command along with webserver’s ip will show you nginx homepage now:

9. Open your web browser and type the ip of our client machine “webserver”. you should see the Nginx Homepage:

That’s It. We are done.

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
mersin ekonomi
5 months ago

thank you

Back To Top

Contact Us