ansible----playbook安装lnmp环境
一、ansible--playbook介绍
playbook是由一个或多个”play”组成的列表。play的主要功能在于将事先归为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来将,所谓的task无法是调用ansible的一个module。将多个paly组织在一个playbook中,即可以让他们联通起来按事先编排的机制同唱一台大戏。
1、playbook基础组件:
hosts playbook中的每一个paly的目的都是为了让某个或某些以某个指定用户的身份执行任务。hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分割主机组。
user remote_user则用于指定远程主机上的执行任务的用户。
任务列表:
play的主体部分是task list. task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。
action:任务执行过程
handlers:用于当前关注的资源发生变化时采取一定指定的操作
二、自动化部署LNMP环境
Liunx安装版本 | centos 6.10 |
Nginx安装版本 | Nginx-1.17.2 |
MySQL安装版本 | MySQL-5.6.45 |
PHP安装版本 | PHP-7.2.21 |
安装包存放位置 | /opt/ |
1.Nginx安装剧本
- hosts: web remote_user: root tasks: - name: unarchive package unarchive: src=/opt/nginx-1.17.2.tar.gz dest=/opt - name: yum rely on yum: name=gcc,gcc-c++,pcre-devel,openssl-devel,zlib-devel state=installed - name: useradd user: name=nginx shell=/sbin/nologin - name: shell configure shell: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module && make && make install args: chdir: /opt/nginx-1.17.2 - name: file file: src=/usr/local/nginx/sbin/nginx dest=/usr/sbin/nginx state=link - name: shell echo nginx shell: echo "#!/bin/bash" > /etc/init.d/nginx && chmod +x /etc/init.d/nginx - name: lineinfile add nginx line1 lineinfile: dest=/etc/init.d/nginx line="#chkconfig:2345 88 88" - name: lineinfile add nginx line2 lineinfile: dest=/etc/init.d/nginx line='a="/usr/local/nginx/sbin/nginx"' - name: lineinfile add nginx line3 lineinfile: dest=/etc/init.d/nginx line='b="/usr/local/nginx/logs/nginx.pid"' - name: lineinfile add nginx line4 lineinfile: dest=/etc/init.d/nginx line='case "$1" in' - name: lineinfile add nginx line5 lineinfile: dest=/etc/init.d/nginx line='start)' - name: lineinfile add nginx line6 lineinfile: dest=/etc/init.d/nginx line='$a' - name: lineinfile add nginx line7 lineinfile: dest=/etc/init.d/nginx line='echo "starting... ok";;' - name: lineinfile add nginx line8 lineinfile: dest=/etc/init.d/nginx line='stop)' - name: lineinfile add nginx line9 lineinfile: dest=/etc/init.d/nginx line='kill -s QUIT $(cat $b)' - name: lineinfile add nginx line10 lineinfile: dest=/etc/init.d/nginx line='echo "stoping... ok";;' - name: lineinfile add nginx line11 lineinfile: dest=/etc/init.d/nginx line='reload)' - name: lineinfile add nginx line12 lineinfile: dest=/etc/init.d/nginx line='kill -s HUP $(cat $b)' - name: lineinfile add nginx line13 lineinfile: dest=/etc/init.d/nginx line='echo "reloading... ok";;' - name: lineinfile add nginx line14 lineinfile: dest=/etc/init.d/nginx line=' restart)' - name: lineinfile add nginx line15 lineinfile: dest=/etc/init.d/nginx line='$0 stop' - name: lineinfile add nginx line16 lineinfile: dest=/etc/init.d/nginx line='$0 start ;;' - name: lineinfile add nginx line17 lineinfile: dest=/etc/init.d/nginx line='esac' - name: lineinfile add nginx line18 lineinfile: dest=/etc/init.d/nginx line='exit 0' - name: shell chkconfig shell: chkconfig --add nginx - name: service service: name=nginx state=restarted
2.MySQL安装剧本
- hosts: db remote_user: root tasks: - name: unarchive package unarchive: src=/opt/mysql-5.6.45.tar.gz dest=/opt/ - name: yum rely on yum: name=gcc,gcc-c++,bison,cmake,ncurses-devel state=installed - name: useradd user: name=mysql shell=/sbin/nologin - name: shell cmake shell: cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DSYSCONFDIR=/etc && make && make install args: chdir: /opt/mysql-5.6.45 - name: file link libmysqlclient file: src=/usr/local/mysql/lib/libmysqlclient.so.18 dest=/usr/local/lib/libmysqlclient.so.18 state=link - name: file link mysql file: src=/usr/local/mysql/bin/mysql dest=/usr/bin/mysql state=link - name: file link mysql file: src=/usr/local/mysql/bin/mysqladmin dest=/usr/bin/mysqladmin state=link - name: copy my.cnf copy: src=/opt/mysql-5.6.45/support-files/my-large.cnf dest=/etc/my.cnf remote_src=yes - name: copy mysqld copy: src=/opt/mysql-5.6.45/support-files/mysql.server dest=/etc/init.d/mysqld remote_src=yes - name: lineinfile basedir lineinfile: dest=/etc/init.d/mysqld line='basedir=/usr/local/mysql' - name: lineinfile datadir lineinfile: dest=/etc/init.d/mysqld line='datadir=/usr/local/mysql/data' - name: file Modify the properties file: path=/etc/init.d/mysqld mode=0755 state=file - name: shell defaults user shell: /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql args: chdir: /opt/mysql-5.6.45/support-files - name: shell chkconfig shell: chkconfig --add mysqld - name: service service: name=mysqld state=started - name: shell mysqladmin shell: mysqladmin -uroot password 123456
3.PHP安装剧本
- hosts: web remote_user: root tasks: - name: yum yum: name=gcc,gcc-c++,gd,libjpeg-devel,libpng-devel,zlib-devel,openssl-devel,pcre-devel,libxml2-devel - name: unarchive unarchive: src=/opt/php-7.2.21.tar.gz dest=/opt/ - name: shell configure shell: ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --enable-mbstring --with-gd --with-zlib --with-jpeg-dir=/usr/lib --enable-fpm && make && make install args: chdir: /opt/php-7.2.21 - name: copy php.ini copy: src=/opt/php-7.2.21/php.ini-development dest=/usr/local/php/php.ini remote_src=yes - name: replace replace: path=/usr/local/php/php.ini regexp='short_open_tag = Off' replace='short_open_tag = On' - name: copy php-fpm copy: src=/opt/php-7.2.21/sapi/fpm/init.d.php-fpm dest=/etc/init.d/php-fpm remote_src=yes - name: file modify the properties file: path=/etc/init.d/php-fpm mode=0755 state=file - name: copy php-fpm.conf copy: src=/usr/local/php/etc/php-fpm.conf.default dest=/usr/local/php/etc/php-fpm.conf remote_src=yes - name: replace replace: path=/usr/local/php/etc/php-fpm.conf regexp=';pid = run/php-fpm.pid' replace='pid = run/php-fpm.pid' - name: replace replace: path=/usr/local/php/etc/php-fpm.conf regexp=';emergency_restart_interval = 10' replace='emergency_restart_interval = 20s' - name: replace replace: path=/usr/local/php/etc/php-fpm.conf regexp=';error_log = log/php-fpm.log' replace='error_log = log/php-fpm.log' - name: replace replace: path=/usr/local/php/etc/php-fpm.conf regexp=';emergency_restart_threshold = 0' replace='emergency_restart_threshold = 10' - name: replace replace: path=/usr/local/php/etc/php-fpm.conf regexp=';process.max = 128' replace='process.max = 128' - name: replace replace: path=/usr/local/php/etc/php-fpm.conf regexp=';rlimit_files = 1024' replace='rlimit_files = 1024' - name: replace replace: path=/usr/local/php/etc/php-fpm.conf regexp=';events.mechanism = epoll' replace='events.mechanism = epoll' - name: copy www.conf copy: src=/usr/local/php/etc/php-fpm.d/www.conf.default dest=/usr/local/php/etc/php-fpm.d/www.conf remote_src=yes - name: replace replace: path=/usr/local/nginx/conf/nginx.conf regexp=' index index.html index.htm;' replace=' index index.php index.html index.htm;' - name: shell sed shell: sed -i '65,71 s/#/ /' /usr/local/nginx/conf/nginx.conf - name: replace replace: path=/usr/local/nginx/conf/nginx.conf regexp=' include fastcgi_params;' replace=' include fastcgi.conf;' - name: shell shell: /etc/init.d/php-fpm restart - name: service service: name=nginx state=restarted - name: unarchive unarchive: src=/opt/ComsenzDiscuz-DiscuzX-master.zip dest=/opt - name: delete shell: rm -rf /usr/local/nginx/html/* - name: shell upload shell: cp -r /opt/DiscuzX/upload /usr/local/nginx/html - name: file file: path=/usr/local/nginx/html/upload mode=0777 recurse=yes
原创文章,转载请注明:
转载自蜡笔没小新博客
————————————
https://www.cnblogs.com/Huang-Niu/