chrisluiz16

nginx+php+git+lineinfile+stat

Jul 5th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 5.36 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. ####---------------------------------- Tasks  --------------------####
  6.  
  7.  
  8. #### Lineinfile module ####
  9.  
  10.  
  11.  
  12. ### Before change in client server ####
  13.  
  14.  
  15. [root@ip-172-31-38-196 ssh]# grep ^PasswordAuthentication /etc/ssh/sshd_config
  16. PasswordAuthentication no
  17.  
  18.  
  19.  
  20.  
  21. #### Playbook ####
  22.  
  23.  
  24. ---
  25.  
  26. - name: "Checking lineinfile module"
  27.   hosts: amazon
  28.   become: true
  29.   tasks:
  30.     - name: "Editing the ssh config to enable PasswordAuthentication"
  31.       lineinfile:
  32.         path: /etc/ssh/sshd_config
  33.         regexp: "PasswordAuthentication no"
  34.         line: "PasswordAuthentication yes"
  35.         validate: "sshd -t -f %s"
  36.       register: sshd_status
  37.     - name: "Service sshd restart"
  38.       when: sshd_status.changed == true
  39.       service:
  40.         name: sshd
  41.         state: restarted
  42.        
  43.  
  44.  
  45. #### Output ####
  46.  
  47. [ec2-user@ip-172-31-10-148 nginx-project]$ ansible-playbook -i inventory lineinfile.yml
  48.  
  49. PLAY [Checking lineinfile module] **************************************************************************************
  50.  
  51. TASK [Gathering Facts] *************************************************************************************************
  52. ok: [lineinfile]
  53.  
  54. TASK [Editing the ssh config to enable PasswordAuthentication] *********************************************************
  55. changed: [lineinfile]
  56.  
  57. TASK [Service sshd restart] ********************************************************************************************
  58. changed: [lineinfile]
  59.  
  60. PLAY RECAP *************************************************************************************************************
  61. lineinfile                 : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  62.  
  63.  
  64. #### Verification ####
  65.  
  66. [root@ip-172-31-38-196 ssh]# grep ^PasswordAuthentication /etc/ssh/sshd_config
  67. PasswordAuthentication yes
  68.  
  69. [root@ip-172-31-38-196 ssh]# systemctl status sshd
  70. ● sshd.service - OpenSSH server daemon
  71.      Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: enabled)
  72.      Active: active (running) since Wed 2023-07-05 14:58:22 UTC; 1min 42s ago
  73.        Docs: man:sshd(8)
  74.              man:sshd_config(5)
  75.    Main PID: 5260 (sshd)
  76.       Tasks: 1 (limit: 1114)
  77.      Memory: 1.2M
  78.         CPU: 8ms
  79.      CGroup: /system.slice/sshd.service
  80.              └─5260 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
  81.              
  82.            
  83.            
  84.  
  85.  
  86. ##### Nginx #####
  87.  
  88.  
  89. [ec2-user@ip-172-31-10-148 nginx-project]$ tree
  90. .
  91. ├── ansible.cfg
  92. ├── aws.pem
  93. ├── inventory
  94. ├── lineinfile.yml
  95. ├── main.yml
  96. └── roles
  97.    └── nginx
  98.        ├── defaults
  99.        │   └── main.yml
  100.        ├── files
  101.        ├── handlers
  102.        │   └── main.yml
  103.        ├── meta
  104.        │   └── main.yml
  105.        ├── README.md
  106.        ├── tasks
  107.        │   └── main.yml
  108.        ├── templates
  109.        │   └── nginx.conf.tmpl
  110.        ├── tests
  111.        │   ├── inventory
  112.        │   └── test.yml
  113.        └── vars
  114.            └── main.yml
  115.            
  116.            
  117.  
  118.  
  119. #### Role task ###
  120.  
  121. ---
  122. # tasks file for nginx
  123.  
  124. - name: "Installing nginx on remote server"
  125.  yum:
  126.    name: "{{ packages }}"
  127.    state: present
  128. - name: "Checking the status of the file"
  129.  stat:
  130.    path: "/etc/nginx/conf.d/{{ domain_name }}.conf"
  131.  register: nginx_conf
  132.  
  133. - name: "Creating an nginx conf for the domain {{ domain_name }}"
  134.  when: nginx_conf.stat.exists == false
  135.  template:
  136.    src: nginx.conf.tmpl
  137.    dest: "/etc/nginx/conf.d/{{ domain_name }}.conf"
  138.  notify:
  139.    - restart-nginx
  140.    - restart-php
  141.    
  142.  
  143. #### nginx.conf.tmpl ####
  144.  
  145. [ec2-user@ip-172-31-10-148 nginx-project]$ cat roles/nginx/templates/nginx.conf.tmpl
  146. server{
  147.    server_name {{ domain_name }};
  148.    listen 80;
  149.    listen [::]:80;
  150.  
  151.    root /var/www/html/{{ domain_name }};
  152.    index index.php;
  153.    error_page  404  /404.html;
  154.  
  155.    add_header X-FRAME-Options "SAMEORIGIN";
  156.    add_header X-Content-Type-Options "nosniff";
  157.  
  158.  
  159.    location / {
  160.        try_files $uri $uri/ /index.php?query_string;
  161.    }
  162.  
  163.  
  164.    location ~ \.php$ {
  165.        include fastcgi_params;
  166.        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  167.        fastcgi_pass unix:/var/run/php-fpm/www.sock;
  168.    }
  169.  
  170.    location ~ /\.ht {
  171.        deny all;
  172.    }
  173.  
  174.    location ~ /\.(?!well-known).* {
  175.        deny all;
  176.    }
  177.  
  178.    location ~* \.(?:jpg|jpeg|gif|css|png|ico|svg|html)$ {
  179.        access_log off;
  180.        expires max;
  181.    }
  182.  
  183.  
  184.    gzip on;
  185.    gzip_proxied no-cache no-store private expired auth;
  186. }
  187.  
  188.  
  189.  
  190. #### Main playbook ####
  191.  
  192.  
  193. ---
  194.  
  195. - name: "Uploading the git PHP website using nginx role"
  196.  hosts: amazon
  197.  become: true
  198.  roles:
  199.    - nginx
  200.  vars:
  201.    git_url: "https://github.com/Chris-luiz-16/aws-elb-site.git"
  202.  tasks:
  203.    - name: "Uploading the git contents to the remote server"
  204.      git:
  205.        repo: "{{ git_url }}"
  206.        dest: /tmp/website
  207.      register: git_status
  208.    - name: "Copying the contents to the default document root /var/www/html/{{ domain_name }}"
  209.       when: git_status.changed == true
  210.      copy:
  211.        src: /tmp/website/
  212.        dest: "/var/www/html/{{ domain_name }}/"
  213.        owner: "{{ nginx_owner }}"
  214.        group: "{{ nginx_owner }}"
  215.        remote_src: true
  216.      notify:
  217.        - restart-php
  218.  
  219.  
Advertisement
Add Comment
Please, Sign In to add comment