文章目录

  • 准备
  • 部署 apache
    • playbook
    • Xftp 上传文件
    • 脚本
    • 变量
    • 单元文件
    • 执行 playbook
    • 验证
  • 部署 mysql
    • playbook
    • Xftp 上传文件
    • 脚本
    • 配置文件
    • 单元文件
    • 执行 playbook
  • 部署 php
    • playbook
    • Xftp 上传文件
    • 脚本
    • 变量
    • php 测试页
    • 执行 playbook
    • 验证

准备

[root@server ~]# cd /etc/ansible/[root@server ansible]# lsansible.cfghostsroles[root@server ansible]# vim hosts //在末尾添加以下内容node1[root@server ansible]# cd roles/[root@server roles]# ansible-galaxy init apache- Role apache was created successfully[root@server roles]# ansible-galaxy init mysql- Role mysql was created successfully[root@server roles]# ansible-galaxy init php- Role php was created successfully[root@server roles]# lsapachemysqlphp

部署 apache

[root@server roles]# cd apache/[root@server apache]# lsdefaultshandlersREADME.mdtemplatesvarsfiles metataskstests

playbook

[root@server apache]# vim tasks/main.yml ---# tasks file for apache- name: stop firewalldservice:name: firewalldstate: stoppedenabled: no- name: stop selinuxlineinfile:path: /etc/selinux/configregexp: '^SELINUX='line: SELINUX=disabled- name: stop selinux1shell:cmd: setenforce 0- name: set yumscript: yum.sh- name: install pkgsyum:name: "{{ pkgs }}"state: present- name: unzip1unarchive:src: apr-1.7.0.tar.gzdest: /opt/- name: unzip2unarchive:src: apr-util-1.6.1.tar.gzdest: /opt/- name: unzip3unarchive: src: httpd-2.4.54.tar.gzdest: /opt/ - name: create apacheuser:name: apachesystem: yesshell: /sbin/nologincreate_home: nostate: present- name: apache.shscript: apache.sh- name: httpd.shscript: httpd.sh- name: cp configtemplate:src: httpd.service.j2dest: /usr/lib/systemd/system/httpd.service- name: apply configshell:cmd: systemctl daemon-reload- name: restart httpdservice:name: httpdstate: startedenabled: yes[root@server apache]# cd /etc/ansible/[root@server ansible]# vim apache.yml---- name: use apache rolehosts: node1roles:- apache

Xftp 上传文件

[root@server ~]# cd /etc/ansible/roles/apache/files/[root@server files]# lsapr-util-1.6.1.tar.gzapr-1.7.0.tar.gzhttpd-2.4.54.tar.gz

脚本

[root@server ~]# cd /etc/ansible/roles/apache/files/[root@server files]# vim yum.sh#!/bin/bash rm -f /etc/yum.repos.d/*/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repoyum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm/usr/bin/sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*/usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*[root@server files]# vim apache.sh#!/bin/bashcd /opt/apr-1.7.0sed -i '/$RM "$cfgfile"/d' configure./configure --prefix=/usr/local/aprmakemake installcd /opt/apr-util-1.6.1./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/aprmakemake install cd /opt/httpd-2.4.54./configure --prefix=/usr/local/apache \--sysconfdir=/etc/httpd24 \--enable-so \--enable-ssl \--enable-cgi \--enable-rewrite \--with-zlib \--with-pcre \--with-apr=/usr/local/apr \--with-apr-util=/usr/local/apr-util/ \--enable-modules=most \--enable-mpms-shared=all \--with-mpm=preforkmakemake install[root@server files]# vim httpd.shexport PATH=/usr/local/apache/bin/:$PATH

变量

[root@server ~]# cd /etc/ansible/roles/apache/vars/[root@server vars]# vim main.yml ---# vars file for apachepkgs: - bzip2 - make - wget - openssl-devel - pcre-devel - expat-devel - libtool - gcc - gcc-c++ - libxml2-devel 

单元文件

[root@server ~]# cd /etc/ansible/roles/apache/templates/[root@server templates]# vim httpd.service.j2[Unit]Description=httpd server daemonAfter=network.target [Service]Type=forkingExecStart=/usr/local/apache/bin/apachectl startExecStop=/usr/local/apache/bin/apachectl stopExecReload=/bin/kill -HUP $MAINPID[Install]WantedBy=multi-user.target

执行 playbook

[root@server ansible]# ansible-playbook apache.yml PLAY [use apache role] *********************************************************TASK [Gathering Facts] *********************************************************ok: [node1]TASK [apache : stop firewalld] *************************************************changed: [node1]TASK [apache : stop selinux] ***************************************************changed: [node1]TASK [apache : stop selinux1] **************************************************changed: [node1]TASK [apache : set yum] ********************************************************changed: [node1]TASK [apache : install pkgs] ***************************************************changed: [node1]TASK [apache : unzip1] *********************************************************changed: [node1]TASK [apache : unzip2] *********************************************************changed: [node1]TASK [apache : unzip3] *********************************************************changed: [node1]TASK [create apache] ***********************************************************changed: [node1]TASK [apache.sh] ***************************************************************changed: [node1]TASK [apache : httpd.sh] *******************************************************changed: [node1]TASK [apache : cp config] ******************************************************changed: [node1]TASK [apache : apply config] ***************************************************changed: [node1]TASK [apache : restart httpd] **************************************************changed: [node1]PLAY RECAP *********************************************************************node1: ok=15 changed=14 unreachable=0failed=0skipped=0rescued=0ignored=0 

验证

部署 mysql

[root@server ansible]# cd roles/mysql/[root@server mysql]# lsdefaultshandlersREADME.mdtemplatesvarsfiles metataskstests

playbook

[root@server mysql]# vim tasks/main.yml ---# tasks file for mysql- name: create user mysqluser:name: mysqlsystem: yesshell: /sbin/nologincreate_home: nostate: present- name: install pkgsyum:name: "libncurses*"state: present- name: unzipunarchive:src: mysql-5.7.37-linux-glibc2.12-x86_64.tar.gzdest: /usr/local/- name: creat linkfile:src: /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64dest: /usr/local/mysqlowner: mysqlgroup: mysqlstate: link- name: create data directoryfile:path: /opt/dataowner: mysqlgroup: mysqlstate: directory- name: mysql-chushi.shscript: mysql-chushi.sh- name: cp configtemplate:src: my.cnf.j2dest: /etc/my.cnf- name: replace file1replace:path: /usr/local/mysql/support-files/mysql.serverregexp: "#^(basedir=).*"replace: "basedir=/usr/local/mysql"- name: replace file2replace:path: /usr/local/mysql/support-files/mysql.serverregexp: "#^(datadir=).*"replace: "datadir=/opt/data"- name: cp mysqld.servicetemplate:src: mysqld.service.j2dest: /usr/lib/systemd/system/mysqld.service- name: apply configshell:cmd: systemctl daemon-reload- name: restart mysqldservice:name: mysqldstate: startedenabled: yes- name: set mysql passwdshell:cmd: /usr/local/mysql/bin/mysql -uroot -e "set password=password('redhat')"- name: set mysql envscript: mysql.sh[root@server mysql]# cd /etc/ansible/[root@server ansible]# vim mysql.yml---- name: use mysql rolehosts: node1roles:- mysql

Xftp 上传文件

[root@server files]# cd /etc/ansible/roles/mysql/files/[root@server files]# lsmysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

脚本

[root@server files]# cd /etc/ansible/roles/mysql/files/[root@server files]# vim mysql-chushi.sh#!/bin/bash/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/ln -sv /usr/local/mysql/include/ /usr/local/include/mysqlecho '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.confldconfig[root@server files]# vim mysql.sh#!/bin/bashecho 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile.d/mysql.sh

配置文件

[root@server templates]# cd /etc/ansible/roles/mysql/templates/[root@server templates]# vim my.cnf.j2[mysqld]basedir = /usr/local/mysqldatadir = /opt/datasocket = /tmp/mysql.sockport = 3306pid-file = /opt/data/mysql.piduser = mysqlskip-name-resolve

单元文件

[root@server templates]# cd /etc/ansible/roles/mysql/templates/[root@server templates]# vim mysqld.service.j2[Unit]Description=mysql server daemonAfter=network.targe[Service]Type=forkingExecStart=/usr/local/mysql/support-files/mysql.server startExecStop=/usr/local/mysql/support-files/mysql.server stopExecReload=/bin/kill -HUP \$MAINPID[Install]WantedBy=multi-user.target

执行 playbook

[root@server ansible]# ansible-playbook mysql.yml PLAY [use mysql role] **********************************************************TASK [Gathering Facts] *********************************************************ok: [node1]TASK [create user mysql] *******************************************************changed: [node1]TASK [mysql : install pkgs] ****************************************************changed: [node1]TASK [mysql : unzip] ***********************************************************changed: [node1]TASK [mysql : creat link] ******************************************************changed: [node1]TASK [mysql : create data directory] *******************************************changed: [node1]TASK [mysql-chushi.sh] *********************************************************changed: [node1]TASK [mysql : cp config] *******************************************************changed: [node1]TASK [mysql : replace file1] ***************************************************ok: [node1]TASK [mysql : replace file2] ***************************************************ok: [node1]TASK [cp mysqld.service] *******************************************************changed: [node1]TASK [mysql : apply config] ****************************************************changed: [node1]TASK [restart mysqld] **********************************************************changed: [node1]TASK [set mysql passwd] ********************************************************changed: [node1]TASK [set mysql env] ***********************************************************changed: [node1]PLAY RECAP *********************************************************************node1: ok=15 changed=12 unreachable=0failed=0skipped=0rescued=0ignored=0 

部署 php

[root@server ansible]# cd roles/php/[root@server php]# lsdefaultshandlersREADME.mdtemplatesvarsfiles metataskstests

playbook

[root@server php]# vim tasks/main.yml ---# tasks file for php- name: install pkgsyum:name: "{{ phppkgs }}"state: present- name: unzipunarchive:src: php-7.1.10.tar.gzdest: /opt/- name: php.shscript: php.sh- name: modify apache configreplace:path: /etc/httpd24/httpd.confregexp: "index.html"replace: "index.php index.html"- name: rm index.htmlshell:cmd: rm -rf /usr/local/apache/htdocs/index.html- name: edit index.phptemplate:src: index.php.j2dest: /usr/local/apache/htdocs/index.php- name: restart httpdservice:name: httpdstate: restartedenabled: yes[root@server php]# cd /etc/ansible/[root@server ansible]# vim php.yml---- name: use php rolehosts: node1roles:- php

Xftp 上传文件

[root@server files]# cd /etc/ansible/roles/php/files/[root@server files]# lsphp-7.1.10.tar.gz

脚本

[root@server files]# cd /etc/ansible/roles/php/files/[root@server files]# vim php.sh#!/bin/bashcd /opt/php-7.1.10./configure --prefix=/usr/local/php \--with-apxs2=/usr/local/apache/bin/apxs \--with-mysql-sock=/tmp/mysql.sock \--with-mysqli \--with-zlib \--with-curl \--with-gd \--with-jpeg-dir \--with-png-dir \--with-freetype-dir \--with-openssl \--enable-mbstring \--enable-xml \--enable-session \--enable-ftp \--enable-pdo \--enable-tokenizer \--enable-zipmakemake installcp php.ini-development /usr/local/php/lib/php.inised -i 's/;date.timezone =/date\.timezone = \Asia\/Shanghai/' /usr/local/php/lib/php.iniecho "AddType application/x-httpd-php .php" >> /etc/httpd24/httpd.confecho "AddType application/x-httpd-php-source .phps" >> /etc/httpd24/httpd.conf

变量

[root@server vars]# cd /etc/ansible/roles/php/vars/[root@server vars]# vim main.yml ---# vars file for phpphppkgs:- libxml2- libxml2-devel- openssl- openssl-devel- bzip2- bzip2-devel- curl- curl-devel- libcurl- libcurl-devel- libicu-devel- libjpeg- libjpeg-devel- libpng- libpng-devel- libzip-devel- openldap-devel- pcre-devel- freetype- freetype-devel- gmp- gmp-devel- readline- readline-devel- libxslt- libxslt-devel- sqlite-devel- php-mysqlnd- zlib- zlib-devel

php 测试页

[root@server templates]# cd /etc/ansible/roles/php/templates/[root@server templates]# vim index.php.j2执行 playbook 
[root@server ansible]# ansible-playbook php.yml PLAY [use php role] ************************************************************TASK [Gathering Facts] *********************************************************ok: [node1]TASK [php : install pkgs] ******************************************************changed: [node1]TASK [php : unzip] *************************************************************changed: [node1]TASK [php.sh] ******************************************************************changed: [node1]TASK [php : modify apache config] **********************************************changed: [node1]TASK [php : rm index.html] *****************************************************[WARNING]: Consider using the file module with state=absent rather than running'rm'.If you need to use command because file is insufficient you can add'warn: false' to this command task or set 'command_warnings=False' inansible.cfg to get rid of this message.changed: [node1]TASK [edit index.php] **********************************************************changed: [node1]TASK [php : restart httpd] *****************************************************changed: [node1]PLAY RECAP *********************************************************************node1: ok=8changed=7unreachable=0failed=0skipped=0rescued=0ignored=0 

验证