aisible roles lnmp
时间: 2023-10-09 17:08:54 浏览: 45
Sure, I can help you with that.
To create an Ansible role for LNMP (Linux, Nginx, MySQL, and PHP), you can follow these steps:
1. In your Ansible project directory, create a new directory named `roles` with the command `mkdir roles`.
2. Change into the `roles` directory with the command `cd roles`.
3. Create a new role directory with the name `lnmp` using the command `ansible-galaxy init lnmp`.
4. Modify the `lnmp/tasks/main.yml` file to include the tasks for installing and configuring the LNMP stack. Here is an example:
```
- name: Install Nginx
apt:
name: nginx
state: present
- name: Install MySQL
apt:
name: mysql-server
state: present
- name: Install PHP
apt:
name: php-fpm
state: present
- name: Start Nginx service
service:
name: nginx
state: started
```
5. Modify the `lnmp/vars/main.yml` file to include any variables needed for the role. Here is an example:
```
mysql_root_password: "password"
```
6. Modify the `lnmp/templates/nginx.conf.j2` file to include the Nginx configuration. Here is an example:
```
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
7. Modify the `lnmp/handlers/main.yml` file to include any handlers needed for the role. Here is an example:
```
- name: Restart PHP-FPM service
service:
name: php7.4-fpm
state: restarted
```
8. Save all the files and use the `lnmp` role in your Ansible playbook by including the following:
```
- name: Install LNMP stack
hosts: webserver
become: true
roles:
- lnmp
```
This will install and configure Nginx, MySQL, and PHP on the `webserver` host.