skip to Main Content

stuck at an ansible hackkerrank lab(fresco play) that asks to install nginx and postgresql and ensure they are running.
But after finishing the code and running the exam it is checking for redirection of nginx server after restart to google.com.
Has anyone faced this issue?

Below is my code to install and ensure services are running:

name: 'To install packages'
  hosts: localhost
  connection: local
  become: yes
  become_method: sudo
  tasks:
    -   
       apt: 
         name: "{{item}}" 
         state: present
       with_items:
        - nginx
        - postgresql

   apt: name=nginx state=latest
    - name: start nginx
      service:
          name: nginx
          state: started
   apt: name=postgresql state=latest
    - name: start postgresql
      service:
          name: postgresql
          state: started

Wrote these in two separate playbooks as of now and need help in redirection of nginx to google.com

2

Answers


  1. Chosen as BEST ANSWER

    Thanks!

    Below code worked for me:

    Define your port number and the site you wish to redirect nginx server to in .j2 file in Templates folder under your roles.

    Include a task in Playbook to set the template to /etc/nginx/sites-enabled/default folder. Include a notify for the handler defined in 'Handlers' folder.

    In some cases if nginx server doesnt restart, use 'sudo service nginx restart' at the terminal before testing your code.

    Ansible-Sibelius (Try it Out- Write a Playbook)

    #installing nginx and postgresql

    - name: Install nginx
      apt: name=nginx state=latest
      tags: nginx
    
    - name: restart nginx
      service:
        name: nginx
        state: started
    
    - name: Install PostgreSQL
      apt: name=postgresql state=latest
      tags: PostgreSQL
    
    - name: Start PostgreSQL
      service:
        name: postgresql
        state: started
    
    - name: Set the configuration for the template file
      template:
        src: /<path-to-your-roles>/templates/sites-enabled.j2
        dest: /etc/nginx/sites-enabled/default
      notify: restart nginx
    

  2. You need to write your nginx configuration file (in this case specifying to redirect traffic to google) and copy to the /etc/nginx/nginx.conf file.

      - name: write nginx.conf
        template:
          src: <path_to_file>
          dest: /etc/nginx/nginx.conf
    

    After this you should restart the nginx service.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search