'write tasks to install nginx and postgresql using ansible-playbook

.score.sh is given as

#!/bin/bash

pass=0;
fail=0;
if [ $? -eq 0 ];then
    worker=`ps -eaf|grep nginx|grep worker`
    master=`ps -eaf|grep nginx|grep master`
    serverup=`curl -Is http://localhost:9090/|grep -i "200 OK"`
    serverurl=`curl -Is http://localhost:9090/|grep -io "google.com"`
    if [[ ! -z ${worker} ]];then
        ((pass++))
        echo "nginx is running as worker";
    else 
        ((fail++))
        echo "nginx is not running as worker";
    fi;

    if [[ ! -z ${master} ]];then
        ((pass++))
        echo "nginx is running as master";
    else 
        ((fail++))
        echo "nginx is not running as master";
    fi;

    if [[ ! -z ${serverup} ]];then
        ((pass++))
        echo "Nginx server is up";
    else 
        ((fail++))
        echo "Nginx server is not up";
    fi;

    if [[ ! -z ${serverurl} ]];then
        ((pass++))
        echo "Nginx server is redirecting to google.com";
    else 
        ((fail++))
        echo "Nginx server is not redirecting to google.com ";
    fi;
fi;
echo $pass $fail
score=$(( $pass * 25 ))
echo "FS_SCORE:$score%"

i was only able to install nginx and postgresql but not satisy the conditions given in .score.sh Can someone help me how do I install nginx as both master worker node and master and direct it to google?



Solution 1:[1]


#installing nginx and postgresql

  • name: Updating apt command: sudo apt-get update
  • name: Install list of packages apt: pkg: ['nginx', 'postgresql'] state: latest
  • name: Start Ngnix service service: name: nginx state: started
  • name: Start PostgreSQL service service: name: postgresql state: started

if nginx not started use 'sudo service nginx restart'

Solution 2:[2]

This worked for me and the fresco course did get passed for me.

---
#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

Solution 3:[3]

---
#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

I have tried the above one getting below error message

ERROR! 'apt' is not a valid attribute for a Play

The error appears to be in '/projects/challenge/fresco_nginx/tasks/main.yml': line 3, column 3, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

#installing nginx and postgresql
- name: Install nginx
  ^ here

Solution 4:[4]

All the answers give above works on installing nginx, the problem is nginx is running port 80 and the score script check 9090. If you curl using port 80 you will get response. So you need to find some way to change the nginx conf file to use port 9090.

Solution 5:[5]

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

Solution 6:[6]

I found below code useful and passed the frescoplay. and above mentioned code also passess the handson in frescoplay.

- hosts: all
  tasks:
    - name: ensure nginx is at the latest version
      apt: name=nginx state=latest
    - name: start nginx
      service:
          name: nginx
          state: started

Solution 7:[7]

server { listen 9090;

    root /var/www/your_domain/html;
    index index.html; 

    server_name google.com;
   

    location / {
            try_files $uri $uri/ =404;
            proxy_pass https://www.google.com;
    }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Blah
Solution 3 bguiz
Solution 4 Aritra Roy
Solution 5 Sruti Chirravuru
Solution 6 Cryptonian _98
Solution 7 karthikeyan thirunavukkarasu