diff --git a/ansible.yml b/ansible.yml new file mode 100644 index 0000000..7386f8e --- /dev/null +++ b/ansible.yml @@ -0,0 +1,280 @@ +--- +- hosts: all + become: true + tasks: + + - name: add nedejs key + shell: | + curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg + args: + creates: /etc/apt/keyrings/nodesource.gpg + + - name: add nedejs repo + shell: | + echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_22.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list + args: + creates: /etc/apt/sources.list.d/nodesource.list + + - name: update + apt: + update_cache: yes + + - name: install web component + apt: + name: + - nginx + - php-fpm + - php-mysql + - php-gd + - php-xml + - php-curl + - php-mbstring + - php-zip + - php-intl + - mysql-server + - python3-flask + - nodejs + state: present + + - name: create db + shell: mysql -e "CREATE DATABASE IF NOT EXISTS WORDPRESS;" + + - name: generate pass + set_fact: + db_password: "{{ lookup('ansible.builtin.password', '/dev/null length=12') }}" + + - name: show pass + debug: + msg: "ВНИМАНИЕ!!! Сохраните сгенерированный пароль: {{ db_password }}" + + - name: create user + shell: mysql -e "CREATE USER IF NOT EXISTS 'WORDPRESS'@'localhost' IDENTIFIED BY '{{ db_password }}'; GRANT ALL PRIVILEGES ON WORDPRESS.* TO 'WORDPRESS'@'localhost'; FLUSH PRIVILEGES;" + + + - name: download wordpress + unarchive: + src: https://wordpress.org/latest.tar.gz + dest: /var/www/ + remote_src: yes + owner: www-data + group: www-data + creates: /var/www/wordpress + + - name: conf wordpress + shell: | + cp /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php + sed -i "s/database_name_here/WORDPRESS/" /var/www/wordpress/wp-config.php + sed -i "s/username_here/WORDPRESS/" /var/www/wordpress/wp-config.php + sed -i "s/password_here/{{ db_password }}/" /var/www/wordpress/wp-config.php + args: + creates: /var/www/wordpress/wp-config.php + + - name: create dir for flask + file: + path: /var/www/flask + state: directory + owner: www-data + group: www-data + mode: '0755' + + - name: create flask file + copy: + dest: /var/www/flask/hello.py + content: | + from flask import Flask + app = Flask(__name__) + + @app.route('/') + def hello_world(): + return 'Hello World! This site is running on Flask.' + + @app.route('/health') + def health(): + return 'OK', 200 + + if __name__ == '__main__': + app.run(host='127.0.0.1', port=5000) + owner: www-data + group: www-data + mode: '0644' + + - name: create systemd service flask-hello + copy: + dest: /etc/systemd/system/flask-hello.service + content: | + [Unit] + Description=flask hello world + After=network.target + + [Service] + WorkingDirectory=/var/www/flask + ExecStart=/usr/bin/python3 /var/www/flask/hello.py + Restart=always + RestartSec=10 + User=www-data + Group=www-data + + [Install] + WantedBy=multi-user.target + mode: '0644' + + - name: start and enable flask-hello + systemd: + name: flask-hello + state: restarted + enabled: yes + daemon_reload: yes + + - name: create dir for nodejs + file: + path: /var/www/nodejs + state: directory + owner: www-data + group: www-data + mode: '0755' + + - name: init nodejs + shell: | + cd /var/www/nodejs + npm init -y + npm install express + + - name: create nodejs file + copy: + dest: /var/www/nodejs/hello.js + content: | + const express = require('express'); + const app = express(); + const host = '127.0.0.1'; + const port = 3000; + + app.get('/', (req, res) => { + res.send('Hello World! This site is running on Nodejs.'); + }); + app.listen(port, () => { + console.log(`start server: http://localhost:${port}`); + }); + owner: www-data + group: www-data + mode: '0644' + + - name: create systemd service nodejs-hello + copy: + dest: /etc/systemd/system/nodejs-hello.service + content: | + [Unit] + Description=nodejs hello world + After=network.target + + [Service] + WorkingDirectory=/var/www/nodejs + ExecStart=/usr/bin/node /var/www/nodejs/hello.js + Restart=always + RestartSec=10 + User=www-data + Group=www-data + + [Install] + WantedBy=multi-user.target + mode: '0644' + + - name: start and enable nodejs-hello + systemd: + name: nodejs-hello + state: restarted + enabled: yes + daemon_reload: yes + + + - name: conf nginx wordpress + copy: + dest: /etc/nginx/sites-available/wordpress + content: | + server { + listen 8080; + root /var/www/wordpress; + index index.php; + + location / { + try_files $uri $uri/ /index.php?$args; + } + + location ~ \.php$ { + include snippets/fastcgi-php.conf; + fastcgi_pass unix:/var/run/php/php-fpm.sock; + } + } + + - name: conf nginx flask + copy: + dest: /etc/nginx/sites-available/flask + content: | + server { + listen 8081; + + location / { + try_files $uri @proxy_to_app; + } + + location @proxy_to_app { + proxy_pass http://127.0.0.1:5000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $server_name; + + } + } + + - name: conf nginx nodejs + copy: + dest: /etc/nginx/sites-available/nodejs + content: | + server { + listen 8082; + + location / { + try_files $uri @proxy_to_app; + } + + location @proxy_to_app { + proxy_pass http://127.0.0.1:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $server_name; + + } + } + + + - name: site wordpress enable + file: + src: /etc/nginx/sites-available/wordpress + dest: /etc/nginx/sites-enabled/wordpress + state: link + + - name: site flask enable + file: + src: /etc/nginx/sites-available/flask + dest: /etc/nginx/sites-enabled/flask + state: link + + - name: site nodejs enable + file: + src: /etc/nginx/sites-available/nodejs + dest: /etc/nginx/sites-enabled/nodejs + state: link + + - name: restart Nginx + service: + name: nginx + state: restarted \ No newline at end of file