目录
集中式编写lnmp剧本
执行
分布式编写lnmp剧本
一定要设置ssh免交互
nginx
mysql
php文章来源:https://www.toymoban.com/news/detail-622490.html
执行文章来源地址https://www.toymoban.com/news/detail-622490.html
集中式编写lnmp剧本
vim /etc/ansible/lnmp.yml
- name: lnmp play
hosts: dbservers
remote_user: root
tasks:
- name: perpare condifure
copy: src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/nginx.repo
- name: install nginx
yum: name=nginx state=latest
- name: start nginx
service: name=nginx state=started enabled=yes
- name: install mysql
yum: name=mysql57-community-release-el7-10.noarch.rpm state=latest
- name: modify file
replace:
path: /etc/yum.repos.d/mysql-community.repo
regexp: 'gpgcheck=1'
replace: 'gpgcheck=0'
- name: install mysql-community-server
yum: name=mysql-community-server state=latest
- name: start mysql
service: name=mysqld state=started enabled=yes
- name: add yum file
command: 'wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm -P /etc/yum.repos.d' - name: rpm epel
command: 'rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm'
- name: rpm el7
command: 'rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm'
- name: install php
command: 'yum install -y php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache'
- name: start php-fpm
service: name=php-fpm state=started enabled=yes
- name: copy configure
copy: src=/usr/local/nginx/conf/nginx.conf dest=/etc/nginx/conf.d/default.conf
- name: restart nginx
service: name=nginx state=started enabled=yes
执行
ansible-playbook lnmp.yml
分布式编写lnmp剧本
一定要设置ssh免交互
ssh-keygen -t rsa
sshpass -p’zxr123‘ ssh-copy-id 192.168.110.60
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p
touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/php/{defaults,vars,tasks,meta,handlers}/main.yml
nginx
cd /etc/ansible/roles/nginx/files
index.php nginx.repo
vim /etc/ansible/roles/nginx/files/index.php
<?php
phpinfo();
?>
vim /etc//ansible/roles/nginx/files/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
vim /etc/ansible/roles/nginx/main.yml
- include: "init.yml"
- name: copy nginx repo
copy: src=nginx.repo dest=/etc/yum.repos.d/
- name: install nginx
yum: name=nginx state=latest
- name: copy index.php
copy: src=index.php dest=/var/www/html
- name: transmit nginx configuration
template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf
- name: start nginx
service: name=nginx state=started enabled=yes
vim /etc/ansible/roles/index.php
- name: stop firewalld
service: name=firewalld state=stopped enabled=no
- name: stop selinux
command: 'setenforce 0'
vim /etc/ansible/roles/nginx/template/default.conf.j2
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /var/www/html;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 192.168.110.60:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
mysql
vim /etc/ansible/roles/mysql/tasks/init.yml
- name: stop firewalld
service: name=firewalld state=stopped enabled=no
- name: stop selinux
command: 'setenforce 0'
vim /etc/ansible/roles/mysql/main.yml
- include: "init.yml"
- name: remove mariadb
shell: 'yum remove mariadb* -y'
- name: wget
shell: 'wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm -P /etc/yum.repos.d'
- name: install mysql57-community-release-el7-10.noarch.rpm
yum: name=epel-release
- name: sed
replace: path=/etc/yum.repos.d/mysql-community.repo regexp="gpgcheck=1" replace="gpgcheck=0"
- name: install mysql-community-server
yum: name=mysql-community-server
- name: start mysql
service: name=mysqld.service state=started
- name: passd
shell: passd=$(grep "password" /var/log/mysqld.log | awk 'NR==1 {print $NF}')
- name: mysql 1
shell: mysql -uroot -p'passd' --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'ZXRabc@123';"
ignore_errors: true
- name: mysql 2
shell: mysql -uroot -pZXRabc@123 -e "grant all privileges on *.* to root@'%' identified by 'ZXRabc@123' with grant option;"
ignore_errors: true
php
vim /etc/ansible/roles/php/tasks/init.yml
- name: stop firewalld
service: name=firewalld state=stopped enabled=no
- name: stop selinux
command: 'setenforce 0'
vim /etc/ansible/rolesphp/tasks/main.yml
- include: "init.yml"
- name: install yum repo
shell: "rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm"
ignore_errors: true
- name: install php
command: 'yum install -y php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache'
- name: add user
user:
name: php
shell: /sbin/nologin
system: yes
- name: copy php.ini
copy: src=php.ini dest=/etc/php.ini
- name: copy www.conf
copy: src=www.conf dest=/etc/php-fpm.d/www.conf
- name: copy index.php
copy: src=index.php dest=/var/www/html
- name: start php-fpm
service: name=php-fpm state=started
执行
vim /etc/ansible/lnmp.yml
- name: nginx play
hosts: webservers
remote_user: root
roles:
- nginx
- name: mysql play
hosts: dbservers
remote_user: root
roles:
- mysql
- name: php play
hosts: phpservers
remote_user: root
roles:
- php
ansible-playbook lnmp.yml
到了这里,关于ansible-playbook roles编写lnmp剧本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!