前言:
如今兄弟们对“nginx依赖的rpm”大约比较注意,各位老铁们都需要学习一些“nginx依赖的rpm”的相关文章。那么小编也在网上汇集了一些有关“nginx依赖的rpm””的相关内容,希望姐妹们能喜欢,咱们一起来了解一下吧!Ansible playbook的应用什么是playbook
playbook(剧本)是由两部分组成
play:主机或者主机组(角色:可以有一个或者多个)
task:指定工作(动作,台词:一个或者多个)
在Ansible中"剧本文件"是以yml结尾的文件。
在SaltStack中"剧本文件"是以sls结尾的文件。
但是语法,使用的都是yaml语法
playbook的组成playbook和Ad-Hoc对比
1.PlayBook功能比ad-hoc更全,是对ad-hoc的一种编排.
2.PlayBook能很好的控制先后执行顺序, 以及依赖关系.
3.PlayBook语法展现更加的直观.
4.playbook可以持久使用,ad-hoc无法持久使用.
YAML语法
缩进:中国: 上海: 浦东新区 北京: - 朝阳区 - 海淀区 - 昌平区 - 大兴区美国: 德克萨斯州: 电信一区
冒号:只要不是以冒号结尾的冒号,冒号后面都要加空格短横线-:代表一个层级,在Python中专业叫法,代表是一个列表
也可以查看官方文档
playbook编写
host : 对那些主机进行操作
remote_user : 使用什么用户执行
tasks : 具体执行什么任务
注意: 可查看官方文档, ansible-playbook +模块名
**如 copy模块: ** ansible-playbook copy
示例:
[root@m01 ~]# cat foo.yml---- hosts: all remote_user: root vars: file_name: zls.txt tasks: - name: Create New File file: name=/tmp/{{ file_name }} state=touch
检查语法
#检查mysql_zh.yml语法[root@m01 mariadb]# ansible-playbook --syntax-check mysql_zh.yml playbook: mysql_zh.yml#测试安装[root@m01 mariadb]# ansible-playbook -C mysql_zh.yml #执行playbook,安装[root@m01 mariadb]# ansible-playbook mysql_zh.yml安装httpd练习
1.安装httpd
[root@m01 httpd]# vim httpd.yml#角色- hosts: web_group#动作 tasks: - name: install httpd server yum: name: httpd state: present#检查语法[root@m01 httpd]# ansible-playbook --syntax-check httpd.yml playbook: httpd.yml#执行[root@m01 httpd]# ansible-playbook httpd.yml PLAY [web_group] ********************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************ok: [zls_web02]ok: [zls_web01]TASK [install httpd server] *********************************************************************************************************changed: [zls_web01]changed: [zls_web02]PLAY RECAP **************************************************************************************************************************zls_web01 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 zls_web02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.启动httpd
#角色- hosts: web_group#动作 tasks: - name: install httpd server yum: name: httpd state: present - name: start httpd server systemd: name: httpd state: started enabled: yes
3.关闭防火墙
- hosts: web_group#动作 tasks:#关闭防火墙 - name: Stop Firewalld systemd: name: firewalld state: stopped enabled: no#安装httpd - name: install httpd server yum: name: httpd state: present#开启httpd - name: start httpd server systemd: name: httpd state: started enabled: yes
4.给默认站点页面
#角色- hosts: web_group#动作 tasks:#关闭防火墙 - name: Stop Firewalld systemd: name: firewalld state: stopped enabled: no#安装httpd - name: install httpd server yum: name: httpd state: present#开启httpd - name: start httpd server systemd: name: httpd state: started enabled: yes#配置默认页面 - name: Config index.html copy: content: "zls_web_page" dest: /var/www/html/index.html group: root owner: root mode: 0644
5.给不同的web配置不同的页面(多个play)
#角色- hosts: web_group#动作 tasks:#关闭防火墙 - name: Stop Firewalld systemd: name: firewalld state: stopped enabled: no#安装httpd - name: install httpd server yum: name: httpd state: present#开启httpd - name: start httpd server systemd: name: httpd state: started enabled: yes- hosts: zls_web01 tasks: - name: Config index.html copy: content: "zls_web01_page" dest: /var/www/html/index.html group: root owner: root mode: 0644- hosts: zls_web02 tasks: - name: Config index.html copy: content: "zls_web02_page" dest: /var/www/html/index.html group: root owner: root mode: 0644rsyncd实战
1.环境准备
2.战前准备
#准备项目目录[root@m01 project]# mkdir rsyncd#配置文件uid = wwwgid = wwwport = 873fake super = yesuse chroot = nomax connections = 200timeout = 600ignore errorsread only = falselist = falseauth users = rsync_backupsecrets file = /etc/rsync.passwdlog file = /var/log/rsyncd.log#####################################[backup]comment = welcome to oldboyedu backup!path = /backup#准备主机清单[web_group]zls_web01 ansible_ssh_host=172.16.1.7zls_web02 ansible_ssh_host=172.16.1.8[backup_group]backup ansible_ssh_host=172.16.1.41[rsync_server:children]web_groupbackup_group
3.写剧本
1)安装rsync服务端和客户端
2)配置rsync服务端
3)创建目录授权
4)创建密码文件授权
5)创建系统用户
6)启动rsync并加入开机自启
[root@m01 rsyncd]# vim rsyncd.yml[root@m01 rsyncd]# cat rsyncd.yml - hosts: rsync_server tasks:#关闭防火墙 - name: Stop firewalld systemd: name: firewalld state: stopped enabled: no - name: SCP YUM REPO copy: src: /etc/yum.repos.d/CentOS-Base.repo dest: /etc/yum.repos.d/#创建系统用户组 - name: Create www Group group: name: www gid: 666 state: present#创建系统用户 - name: Create www User user: name: www uid: 666 group: www create_home: false shell: /sbin/nologin#安装rsync服务端和客户端 - name: Install rsyncd Server yum: name: rsync state: present- hosts: backup_group tasks:#配置rsync服务端 - name: Config rsyncd Conf copy: src: ./rsyncd.j2 dest: /etc/rsyncd.conf owner: root group: root mode: 0644#创建目录授权 - name: Create dir file: path: /backup state: directory owner: www group: www mode: 0755 recurse: yes#创建密码文件授权 - name: Create passwd file copy: content: "rsync_backup:123" dest: /etc/rsync.passwd owner: root group: root mode: 0600#启动rsync并加入开机自启 - name: Start rsyncd systemd: name: rsyncd state: started enabled: yes#配置客户端- hosts: web_group tasks: - name: Config client passwd file copy: content: "123" dest: /etc/rsync.pass owner: root group: root mode: 0600实战1:
使用Ansible-playbook部署以下内容:
web01 web02 提交作业代码 httpd phprsync 备份脚本,加入crontab,定时发送邮件nfs 挂载站点目录,实现共享存储sersync 实时同步ansible-playbook --syntax-check cron.yml
解决:
准备基本纯净环境编辑主机清单传密钥
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7···
4.编写ansible-playbook
[root@m01 project]# cat anible.yml - hosts: all tasks: #关闭防火墙 - name: stop firewalld systemd: name: firewalld state: stopped enabled: no #创建用户及组 - name: create www group group: name: www gid: 666 state: present - name: create www user user: name: www uid: 666 group: www create_home: false shell: /sbin/nologin#远程安装httpd - hosts: web_group tasks: - name: Install httpd server yum: name: httpd state: present#远程安装php - name: Install php server yum: name: php state: present#上传作业代码到站点目录并给权限 - name: copy zuoye copy: src: /root/project/httpd/zuoye/ dest: /var/www/html/ group: www owner: www mode: 0755#copy配置文件 - name: copy httpd.conf copy: src: /etc/httpd/conf/httpd.conf dest: /etc/httpd/conf/#创建目录 - name: create dir file: path: /var/www/html/uploads state: directory owner: www group: www mode: 0755 recurse: yes #启动httpd - name: start httpd server systemd: name: httpd state: restarted daemon_reload: yes enabled: yes#部署nfs服务 - hosts: nfs_server tasks:#安装nfs - name: Install nfs-utils server yum: name: nfs-utils state: present#部署nfs服务端- hosts: nfs_group tasks:#创建配置文件 - name: copy config file copy: src: /root/project/nfs/exports dest: /etc/exports#创建共享目录 - name: create dir file: path: /data state: directory owner: www group: www mode: 0755 recurse: yes #启动nfs服务 - name: start nfs-server systemd: name: nfs-server state: restarted daemon_reload: yes enabled: yes#nfs客户端挂载共享目录- hosts: web_group tasks: - name: Mount /data mount: path: /var/www/html/uploads src: 172.16.1.31:/data fstype: nfs state: mounted#部署rsync服务- hosts: rsync_server tasks:#安装rsync服务 - name: install rsyncd server yum: name: rsync state: present#部署rsync服务端- hosts: backup_group tasks:#copy配置文件 - name: config rsyncd conf copy: src: /root/project/rsyncd/rsyncd.j2 dest: /etc/rsyncd.conf owner: root group: root mode: 0644#根据配置文件创建对应的目录 - name: create dir file: path: /backup state: directory owner: www group: www mode: 0755 recurse: yes - name: create dir file: path: /data state: directory owner: www group: www mode: 0755 recurse: yes#根据配置文件创建对应的密码文件 - name: create passwd file copy: content: "rsync_backup:123" dest: /etc/rsync.passwd owner: root group: root mode: 0600#启动rsync - name: start rsyncd systemd: name: rsyncd state: started enabled: yes#备份脚本- hosts: web_group tasks: - name: create file copy: src: /root/project/cron/web.sh dest: /root/rsync.sh#加入cron定时任务 - name: Creates an cron cron: name: "每天凌晨执行脚本" minute: "0" hour: "0" job: "/bin/bash /root/rsync.sh > /dev/null"#安装邮件功能- hosts: backup_group tasks: - name: install mailx yum: name: mailx state: present #copy邮件配置文件 - name: copy mailx file copy: src: /root/project/cron/mailx.rc dest: /etc/mail.rc#校验发邮件脚本 - name: create file copy: src: /root/project/cron/backup_md5.sh dest: /root/backup_md5.sh #定时执行脚本 - name: Creates an cron cron: name: "每天凌晨执行脚本" minute: "0" hour: "0" job: "/bin/bash /root/backup_md5.sh > /dev/null"#部署实时同步服务端- hosts: nfs_group tasks: - name: install rsyncd server yum: name: rsync state: present - name: install inotify-tools server yum: name: inotify-tools state: present#创建配置文件对应的密码文件 - name: create passwd file copy: content: "123" dest: /etc/rsync.pass owner: root group: root mode: 0600#copy实时同步解压目录到站点目录 - name: copy sersync file copy: src: /usr/local/sersync/ dest: /usr/local/sersync/ mode: 0755 #启动任务 - name: shell shell: "/usr/local/sersync/sersync2 -rdo /usr/local/sersync/confxml.xml"
5.对应的配置文件
#nfs配置文件[root@m01 nfs]# cat exports /data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)#rsync配置文件[root@m01 rsyncd]# cat rsyncd.j2 uid = wwwgid = wwwport = 873fake super = yesuse chroot = nomax connections = 200timeout = 600ignore errorsread only = falselist = falseauth users = rsync_backupsecrets file = /etc/rsync.passwdlog file = /var/log/rsyncd.log#####################################[backup]comment = welcome to oldboyedu backup!path = /backup[nfs]comment = welcome to oldboyedu nfs!path = /data#定时任务对应的脚本[root@m01 cron]# pwd/root/project/cron#校验脚本[root@m01 cron]# cat backup_md5.sh #!/bin/bashPATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin'H=`hostname`I=`ifconfig eth1|awk 'NR==2{print $2}'`D=`date +%F`S=${H}_${I}_${D}BD=/backupmd5sum -c /backup/*.txt|mail -s "${D}:校验结果" 861962063@qq.comfind ${BD} -type d -mtime +180|xargs rm -fr#mailx配置文件[root@m01 cron]# cat mailx.rc ...set bsdcompatet from=861962063@qq.comset smtp=smtps://smtp.qq.com:465set smtp-auth-user=861962063@qq.comset smtp-auth-passwordwzooyakiejsrbbjcset smtp-auth=loginset ssl-verify=ignoreset nss-config-dir=/etc/pki/nssdb/[root@m01 cron]# [root@m01 cron]# cat web.sh #!/bin/bashPATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin'H=`hostname`I=`ifconfig eth1|awk 'NR==2{print $2}'`D=`date +%F`S=${H}_${I}_${D}BD=/backupexport RSYNC_PASSWORD=123mkdir -p ${BD}/${S}tar zcf /backup/${S}/conf.tar.gz /etc/passwd &>/dev/nullmd5sum /backup/${S}/conf.tar.gz > /backup/${H}.txtrsync -az /backup/ rsync_backup@172.16.1.41::backupfind ${BD} -type d -mtime +7|xargs rm -fr#实时同步配置文件[root@m01 sersync]# cat confxml.xml ... <inotify> <delete start="true"/> <createFolder start="true"/> <createFile start="true"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="true"/> <modify start="true"/> </inotify> <sersync> <localpath watch="/data"> <remote ip="172.16.1.41" name="nfs"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-az"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/> <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="true" time="100"/><!-- timeout=100 --> <ssh start="false"/>
6.执行playbook
#检查playbook语法[root@m01 project]# ansible-playbook --syntax-check anible.yml playbook: anible.yml#测试[root@m01 project]# ansible-playbook -C anible.yml #执行[root@m01 project]# ansible-playbook anible.yml浏览器测试
上传照片到web01,同时检查共享目录,实时同步的目录
上传照片到web02,同时检查共享目录,实时同步的目录
实战2:
源码安装nginx,拆分数据库,一键部署知乎搭建好的页面
准备工作
准备基本纯净环境编辑主机清单传密钥编写部署php,源码安装nginx, 上传wordpress和zh代码的playbook执行playbook,域名解析后浏览器访问,仅能访问到wordpress和zh的首页面,编辑不了。编写playbook 部署数据库Db01--10.0.0.51, 并创建wordpress和zh的库,给权限,允许 远程连接访问执行完playbook,访问wordpress和知乎,登录并发表文章在web上,把 wordpress 和 zh 的 目录打包压缩,发送给主机端。并修改playbook 对应的路径把db01,上对应的wordpress和zh库,导出发送给主机端,并修改playbook对应的路径
执行
初始化环境 web, db01一键执行完playbook ,域名访问浏览器,即可访问到写好文章的wordpress,和zh.
#需要的playbook[root@m01 nginx]# cat lnmp.yml- hosts: web_group tasks:#关闭防火墙 - name: stop firewalld systemd: name: firewalld state: stopped enabled: no#copy源 - name: copy yum repo copy: src: /etc/yum.repos.d/CentOS-Base.repo dest: /etc/yum.repos.d/ owner: root group: root mode: 0644 #关闭selinux - name: Disable SElinux selinux: state: disabled - name: install unzip yum: name: unzip state: present#创建用户及组 - name: create www group group: name: www gid: 666 state: present - name: create www user user: name: www uid: 666 group: www create_home: false shell: /sbin/nologin #解压php压缩包到对端 对应目录下 - name: tar php unarchive: src: /root/project/nginx/php.tar.gz dest: /usr/local/src copy: yes#安装php - name: install php shell: "cd /usr/local/src/php && rpm -Uvh *rpm"#安装nginx依赖 - name: install nginx require package yum: name: openssl-devel state: present#解压nginx压缩包到对端 - name: tar nginx unarchive: src: /root/project/nginx/nginx-1.16.0.tar.gz dest: /root copy: yes#源码指定模块编译安装 - name: configure nginx shell: "cd /root/nginx-1.16.0 && ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module && make && make install"#copy nginx启动文件 - name: scp nginx shell copy: src: /root/project/nginx/nginx.service dest: /usr/lib/systemd/system#copy nginx主配置文件 - name: scp nginx default conf copy: src: /root/project/nginx/nginx.conf dest: /usr/local/nginx/conf/#创建主配置文件对应的目录 - name: creat nginx conf.d file: path: /usr/local/nginx/conf/conf.d state: directory#copy 博客配置文件 - name: scp nginx conf copy: src: /root/project/nginx/wordpress.conf dest: /usr/local/nginx/conf/conf.d/#copy知乎配置文件 - name: scp zh conf copy: src: /root/project/nginx/zh.conf dest: /usr/local/nginx/conf/conf.d/#启动nginx - name: start nginx systemd: name: nginx state: restarted enabled: yes#修改对端php配置文件qidong#修改对端php配置文件启动用户 - name: config php conf shell: "sed -i '/^user/c user = www' /etc/php-fpm.d/ && sed -i '/^group/c group = www' /etc/php-fpm.d/; #启动php - name: start php systemd: name: php-fpm state: restarted enabled: yes #创建博客知乎对应目录 - name: creat code dir file: path: /code state: directory owner: www group: www mode: 0755 recurse: yes#解压写好文章的博客到对端 - name: unarchive wordpress code unarchive: src: /root/project/nginx/wordpress.tgz dest: /code owner: www group: www mode: 0755#创建知乎对应目录 - name: creat zh dir file: path: /code/zh state: directory owner: www group: www mode: 0755 recurse: yes#解压写好文章的知乎压缩包到对端 - name: unarchive zh code unarchive: src: /root/project/nginx/zh.zip dest: /code/zh owner: www group: www mode: 0755
2.对应需要的配置文件
#nginx 主配置文件[root@m01 nginx]# cat nginx.conf user www;worker_processes 1;...http { include mime.types; default_type application/octet-stream; include /usr/local/nginx/conf/conf.d/*.conf;...#nginx启动文件 [root@m01 nginx]# cat nginx.service [Unit]Description=nginx server daemonDOCUMENTATION=man:nginxAfter=network.target [Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s quitPrivateTmp=true[Install]WantedBy=multi-user.target#systemctl daemon-reload#systemctl start nginx.service#systemctl enable nginx.service#systemctl status nginx.service#wordpress配置文件[root@m01 nginx]# cat wordpress.conf server { listen 80; server_name ; location / { root /code/wordpress; index index.php index.html; } location ~\.php$ { root /code/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}#知乎对应配置文件[root@m01 nginx]# cat zh.conf server { listen 80; server_name ; location / { root /code/zh; index index.php index.html; } location ~\.php$ { root /code/zh; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}
域名解析到web0 --10.0.0.7,在浏览器访问,,并写篇文章
实战mysql迁移
1.到web02,把写好文章的wordpress,打包成名称修改为 wordpress.tgz,并远程推送到 m01--10.0.0.61 对应目录下
#需要的安装包 [root@m01 nginx]# lltotal 51736-rw-r--r-- 1 root root 788 Sep 27 09:51 lnmp.yml.gz-rw-r--r-- 1 root root 3103 Sep 27 13:15 lnmp_zh.yml-rw-r--r-- 1 root root 1032345 Sep 18 14:55 nginx-1.16.0.tar.gz-rw-r--r-- 1 1001 1001 2701 Sep 27 06:10 nginx.conf-rw-r--r-- 1 root root 418 Sep 27 05:47 nginx.service-rw-r--r-- 1 root root 20663838 Sep 18 17:34 php.tar.gz-rw-r--r-- 1 root root 297 Sep 27 08:05 wordpress.conf-rw-r--r-- 1 root root 11190716 Sep 27 09:40 wordpress.tgz-rw-r--r-- 1 root root 2478168 Sep 27 12:25 z_blog.zip-rw-r--r-- 1 root root 282 Sep 27 11:34 zh.conf-rw-r--r-- 1 root root 17572114 Sep 27 13:01 zh.zip
还原web02,修改playbook,再重新推送
[root@m01 nginx]# cat lnmp.yml - hosts: gjy_web02 tasks: - name: stop firewalld systemd: name: firewalld state: stopped enabled: no... #解压写好文章的博客到对端 - name: unarchive wordpress code unarchive: src: /root/project/nginx/wordpress.tgz dest: /code owner: www group: www mode: 0755 ...#解压写好文章的知乎压缩包到对端 - name: unarchive zh code unarchive: src: /root/project/nginx/zh.zip dest: /code/zh owner: www group: www mode: 0755
浏览器能访问到界面,但是是db01数据库的
关闭db01的数据库,或者修改web02的连接数据库的主机名,再次访问,都会出现500的错误
一键执行playbook,完成数据迁移,访问到wordpres写好文章的页面
[root@m01 mariadb]# cat mysql.yml - hosts: gjy_db02 tasks: - name: install python mysql yum: name: MySQL-python state: present#安装mariadb - name: install mariadb server yum: name: mariadb-server state: present#启动mariadb - name: start mariadb server systemd: name: mariadb state: restarted enabled: yes #创建wordpress库 - name: create database wordpress mysql_db: name: wordpress state: present#授权wordpress库 - name: create wordpress user mysql_user: name: "wordpress" password: "123" host: '172.16.1.%' priv: '*.*:ALL' state: present #copy写好文章的wordpress库到对端 - name: scp mysql wordpress copy: src: /root/project/mariadb/full.sql dest: /tmp/full.sql #导入写好文章的wordpress库 - name: import full.sql mysql_db: state: import name: wordpress target: /tmp/full.sql #copy写好文章的zh库到对端 - name: scp mysql zh copy: src: /root/project/mariadb/zh.sql dest: /tmp/zh.sql#导入写好文章的zh库 - name: import zh.sql mysql_db: state: import name: zh target: /tmp/zh.sql
执行playbook, 可以先查看db02的数据库
[root@db02 ~]# mysqlWelcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 3Server version: 5.5.64-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> select user,host from mysql.user;+-----------+------------+| user | host |+-----------+------------+| root | 127.0.0.1 || wordpress | 172.16.1.% || root | ::1 || | db01 || root | db01 || | localhost || root | localhost |+-----------+------------+7 rows in set (0.00 sec)#查看wordpress库MariaDB [(none)]> show tables from wordpress;+-----------------------+| Tables_in_wordpress |+-----------------------+| wp_commentmeta || wp_comments |.....#查看zh库MariaDB [(none)]> show tables from zh;+--------------------------------+| Tables_in_zh |+--------------------------------+| aws_active_data || aws_answer || aws_answer_comments || aws_answer_thanks || aws_answer_uninterested | ......
迁移之后,10.0.0.51db01的数据库关闭,
域名访问,都可以访问到wordpress写好文章的界面
域名访问,能访问到知乎的界面
标签: #nginx依赖的rpm