Installing Nginx using Ansible Playbook on ubuntu

Working as a I.T professional, We need to also required to install a single or multiple packages in our multiple server, Here we are about to create Ansible Playbook to install Nginx web server.

We believe, You have already installed Ansible if not you need to click on given link to install Ansible on ubuntu server.

Step 1: Configure Ansible 

By default ansible host configuration file is /etc/ansible/hosts Here you need to add the Apache web server details like this, Use the following step for the same, In my case i have 1 apache server.

  • Group Name  – WebServer
  • IP Address     – Your IP Address
  • Username      – example
  • Password       – XXXXXXXXXX

You need to add your server in ansible host file, Syntax is given below, You need to open the /etc/ansible/hosts file with nano or vim text editor.

sudo vim /etc/ansible/hosts

and add the web server entry like this.

localhost ansible_connection=ssh ansible_ssh_user=ubuntu ansible_ssh_pass=xxxxxxx

Change the credentials as per your environment, Save and exit from the vim text editor and verify the host machine connectivity with ansible by using the given command.

ansible all -m ping

Here our host configuration is completed and now we can go for next step to write play book.

Step 2: Creating Nginx Ansible Playbook

You need to create a nginx-playbook.yml and you change it according to your requirement.

sudo vim nginx-playbook.yml

Paste the given code.

- hosts: webserver
  become: yes

    - name: update
      apt: update_cache=yes   
    - name: Install Nginx
      apt: name=nginx state=latest

        - restart nginx

    - name: restart nginx
      service: name=nginx state=reloaded

Now we are ready to use the Nginx’s PlayBook, To execute the PlayBook you need to use the given command.

ansible-playbook nginx-playbook.yml

Verify the Nginx service on your targeted server by using the given command.

sudo systemctl status nginx.service

You should get nginx service up and running state.


We have successfully installed PHP using Ansible playbook on ubuntu 22.04 LTS, Kindly report in case you are facing defeculties with follwing details.

  • OS
  • Ansible version
  • Package name
  • Logs – Error / Warning / failed

Get in touch with our new devops tools based website-

Installing Nginx using Ansible Playbook on ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top