Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####---------------------------------- Tasks --------------------####
- #### Lineinfile module ####
- ### Before change in client server ####
- [root@ip-172-31-38-196 ssh]# grep ^PasswordAuthentication /etc/ssh/sshd_config
- PasswordAuthentication no
- #### Playbook ####
- ---
- - name: "Checking lineinfile module"
- hosts: amazon
- become: true
- tasks:
- - name: "Editing the ssh config to enable PasswordAuthentication"
- lineinfile:
- path: /etc/ssh/sshd_config
- regexp: "PasswordAuthentication no"
- line: "PasswordAuthentication yes"
- validate: "sshd -t -f %s"
- register: sshd_status
- - name: "Service sshd restart"
- when: sshd_status.changed == true
- service:
- name: sshd
- state: restarted
- #### Output ####
- [ec2-user@ip-172-31-10-148 nginx-project]$ ansible-playbook -i inventory lineinfile.yml
- PLAY [Checking lineinfile module] **************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [lineinfile]
- TASK [Editing the ssh config to enable PasswordAuthentication] *********************************************************
- changed: [lineinfile]
- TASK [Service sshd restart] ********************************************************************************************
- changed: [lineinfile]
- PLAY RECAP *************************************************************************************************************
- lineinfile : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- #### Verification ####
- [root@ip-172-31-38-196 ssh]# grep ^PasswordAuthentication /etc/ssh/sshd_config
- PasswordAuthentication yes
- [root@ip-172-31-38-196 ssh]# systemctl status sshd
- ● sshd.service - OpenSSH server daemon
- Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: enabled)
- Active: active (running) since Wed 2023-07-05 14:58:22 UTC; 1min 42s ago
- Docs: man:sshd(8)
- man:sshd_config(5)
- Main PID: 5260 (sshd)
- Tasks: 1 (limit: 1114)
- Memory: 1.2M
- CPU: 8ms
- CGroup: /system.slice/sshd.service
- └─5260 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
- ##### Nginx #####
- [ec2-user@ip-172-31-10-148 nginx-project]$ tree
- .
- ├── ansible.cfg
- ├── aws.pem
- ├── inventory
- ├── lineinfile.yml
- ├── main.yml
- └── roles
- └── nginx
- ├── defaults
- │ └── main.yml
- ├── files
- ├── handlers
- │ └── main.yml
- ├── meta
- │ └── main.yml
- ├── README.md
- ├── tasks
- │ └── main.yml
- ├── templates
- │ └── nginx.conf.tmpl
- ├── tests
- │ ├── inventory
- │ └── test.yml
- └── vars
- └── main.yml
- #### Role task ###
- ---
- # tasks file for nginx
- - name: "Installing nginx on remote server"
- yum:
- name: "{{ packages }}"
- state: present
- - name: "Checking the status of the file"
- stat:
- path: "/etc/nginx/conf.d/{{ domain_name }}.conf"
- register: nginx_conf
- - name: "Creating an nginx conf for the domain {{ domain_name }}"
- when: nginx_conf.stat.exists == false
- template:
- src: nginx.conf.tmpl
- dest: "/etc/nginx/conf.d/{{ domain_name }}.conf"
- notify:
- - restart-nginx
- - restart-php
- #### nginx.conf.tmpl ####
- [ec2-user@ip-172-31-10-148 nginx-project]$ cat roles/nginx/templates/nginx.conf.tmpl
- server{
- server_name {{ domain_name }};
- listen 80;
- listen [::]:80;
- root /var/www/html/{{ domain_name }};
- index index.php;
- error_page 404 /404.html;
- add_header X-FRAME-Options "SAMEORIGIN";
- add_header X-Content-Type-Options "nosniff";
- location / {
- try_files $uri $uri/ /index.php?query_string;
- }
- location ~ \.php$ {
- include fastcgi_params;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_pass unix:/var/run/php-fpm/www.sock;
- }
- location ~ /\.ht {
- deny all;
- }
- location ~ /\.(?!well-known).* {
- deny all;
- }
- location ~* \.(?:jpg|jpeg|gif|css|png|ico|svg|html)$ {
- access_log off;
- expires max;
- }
- gzip on;
- gzip_proxied no-cache no-store private expired auth;
- }
- #### Main playbook ####
- ---
- - name: "Uploading the git PHP website using nginx role"
- hosts: amazon
- become: true
- roles:
- - nginx
- vars:
- git_url: "https://github.com/Chris-luiz-16/aws-elb-site.git"
- tasks:
- - name: "Uploading the git contents to the remote server"
- git:
- repo: "{{ git_url }}"
- dest: /tmp/website
- register: git_status
- - name: "Copying the contents to the default document root /var/www/html/{{ domain_name }}"
- when: git_status.changed == true
- copy:
- src: /tmp/website/
- dest: "/var/www/html/{{ domain_name }}/"
- owner: "{{ nginx_owner }}"
- group: "{{ nginx_owner }}"
- remote_src: true
- notify:
- - restart-php
Advertisement
Add Comment
Please, Sign In to add comment