ansible部署lamp

1.部署apache

配置yum网络源

[root@ansible apache]# vim files/yum.sh
[root@ansible apache]# cat files/yum.sh 
#!/bin/bash
rm -rf /etc/yum.repo.d/*
/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo &>/dev/null
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm  &>/dev/null
/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@ansible apache]# cat vars/main.yml 
---
# vars file for apache
pkgs:
  - bzip2
  - vim
  - make
  - wget
  - openssl-devel
  - pcre-devel
  - expat-devel
  - libtool 
  - gcc
  - gcc-c++
  - libxml2-devel

解压编译压缩包

[root@ansible apache]# cd files/
[root@ansible files]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz  yum.sh

[root@ansible apache]# cat files/apache.sh 
#!/bin/bash
cd /opt/apr-1.7.0
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr
make
make install

cd /opt/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make 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=prefork
make
make install

配置环境变量

[root@ansible apache]# cat files/httpd.sh 
export PATH=/usr/local/apache/bin/:$PATH

编写http服务的单元文件

[root@ansible apache]# cat templates/httpd.service.j2
[Unit]
Description=httpd server daemon
After=network.target 
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target

配置启动文件

[root@ansible ansible]# cat apache.yml 
---
- name: use apache role
  hosts: node1
  roles:
    - apache

配置角色

[root@ansible apache]# cat tasks/main.yml 
---
# tasks file for apache
- name: stop firewalld
  service:
    name: firewalld
    state: stopped
    enabled: no

- name: stop selinux
  lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=disabled

- name: stop selinux1
  shell:
    cmd: setenforce 0

- name: set yum
  script: yum.sh

- name: install pkgs
  yum:
    name: "{{ pkgs }}"
    state: present

- name: cp file
  copy: 
    src: apr-1.7.0.tar.gz
    dest: /opt/

- name: cp file1
  copy:
    src: apr-util-1.6.1.tar.gz
    dest: /opt/

- name: cp file2
  copy:
    src: httpd-2.4.54.tar.gz
    dest: /opt/

- name: unzip
  unarchive:
    src: apr-1.7.0.tar.gz
    dest: /opt/

- name: unzip1
  unarchive:
    src: apr-util-1.6.1.tar.gz
    dest: /opt/

- name: unzip2
  unarchive:
    src: httpd-2.4.54.tar.gz
    dest: /opt/

- name: create apache
  user: 
    name: apache
    system: yes
    shell: /sbin/nologin
    create_home: no
    state: present

- name: apache.sh
  script: apache.sh

- name: httpd.sh
  script: httpd.sh

- name: cp config
  template: 
    src: httpd.service.j2
    dest: /usr/lib/systemd/system/httpd.service

- name: apply config
  shell:
    cmd: systemctl daemon-reload

- name: restart httpd
  service:
    name: httpd
    state: started
    enabled: yes

[root@ansible ansible]# ansible-playbook apache.yml 

PLAY [use apache role] ************************************************************************************************************

TASK [apache : stop firewalld] ****************************************************************************************************
ok: [node1]

TASK [apache : stop selinux] ******************************************************************************************************
ok: [node1]

TASK [apache : stop selinux1] *****************************************************************************************************
changed: [node1]

TASK [apache : set yum] ***********************************************************************************************************
changed: [node1]

TASK [apache : install pkgs] ******************************************************************************************************
ok: [node1]

TASK [apache : cp file] ***********************************************************************************************************
ok: [node1]

TASK [apache : cp file1] **********************************************************************************************************
ok: [node1]

TASK [apache : cp file2] **********************************************************************************************************
ok: [node1]

TASK [apache : unzip] *************************************************************************************************************
changed: [node1]

TASK [apache : unzip1] ************************************************************************************************************
ok: [node1]

TASK [apache : unzip2] ************************************************************************************************************
changed: [node1]

TASK [create apache] **************************************************************************************************************
ok: [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=17   changed=9    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

在这里插入图片描述

2. 部署mysql

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


初始化数据库

[root@ansible files]# vim mysql-chushi.sh
[root@ansible files]# cat mysql-chushi.sh
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/ 
ln -sv /usr/local/mysql/include/ /usr/local/include/mysql 
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf 
ldconfig 

编辑mysql配置文件

[root@ansible mysql]# cat templates/my.cnf.j2
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

[root@ansible mysql]# cat templates/mysqld.service.j2
[Unit]
Description=mysql server daemon
After=network.targe

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target

//做一个链接到环境变量
[root@ansible mysql]# vim files/mysql.sh
[root@ansible mysql]# cat files/mysql.sh
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile.d/mysql.sh

[root@ansible mysql]# cat tasks/main.yml
---
# tasks file for mysql
- name: create user mysql
  user:
    name: mysql
    system: yes
    shell: /sbin/nologin
    create_home: no
    state: present

- name: install pkgs
  yum: 
    name: "libncurses*"
    state: present

- name: unzip
  unarchive:
    src: mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
    dest: /usr/local

- name: create link
  file:
    src: /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64
    dest: /usr/local/mysql
    owner: mysql
    group: mysql
    state: link

- name: create data 
  file:
    path: /opt/data
    owner: mysql
    group: mysql
    state: directory

- name: chushi.sh
  script: mysql-chushi.sh

- name: cp config
  template:
    src: my.cnf.j2
    dest: /etc/my.cnf

- name: replace file1
  replace: 
    path: /usr/local/mysql/support-files/mysql.server
    regexp: "s#^(basedir=).*"
    replace: "basedir=/usr/local/mysql"

- name: replace file2
  replace:
    path: /usr/local/mysql/support-files/mysql.server
    regexp: "s#^(datadir=).*"
    replace: "datadir=/opt/data"

- name: cp mysqld.service
  template:
    src: mysqld.service.j2
    dest:  /usr/lib/systemd/system/mysqld.service

- name: apply config
  shell:
    cmd: systemctl daemon-reload

- name: restart mysqld
  service:
    name: mysqld
    state: started
    enabled: yes

- name: passwd
  shell:
    cmd: /usr/local/mysql/bin/mysql -uroot -e "set password=password('1')"

- name: env
  script: mysql.sh

[root@ansible ansible]# ansible-playbook mysql.yml 

PLAY [use mysql role] *************************************************************************************************************

TASK [create user mysql] **********************************************************************************************************
ok: [node1]

TASK [mysql : install pkgs] *******************************************************************************************************
ok: [node1]

TASK [mysql : unzip] **************************************************************************************************************
changed: [node1]

TASK [mysql : create link] ********************************************************************************************************
changed: [node1]

TASK [mysql : create data] ********************************************************************************************************
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 [mysql : passwd] *************************************************************************************************************
changed: [node1]

TASK [mysql : env] ****************************************************************************************************************
changed: [node1]

PLAY RECAP ************************************************************************************************************************
node1                      : ok=14   changed=10   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

3. 部署php

安装依赖包

[root@ansible php]# cat vars/main.yml
---
# vars file for php
phppkgs:
  - libjpeg 
  - libjpeg-devel 
  - libpng 
  - libpng-devel 
  - freetype 
  - freetype-devel 
  - libxml2 
  - libxml2-devel 
  - zlib 
  - zlib-devel 
  - curl 
  - curl-devel

上传压缩包

[root@ansible php]# cd files/
[root@ansible files]# ls
[root@ansible files]# rz -E
rz waiting to receive.
[root@ansible files]# ls
php-7.1.10.tar.gz

配置编译安装

[root@ansible files]# cat php.sh 
cd /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-zip

make
make install

cp php.ini-development /usr/local/php/lib/php.ini
sed -i 's/;date.timezone =/date\.timezone = \Asia\/Shanghai/' /usr/local/php/lib/php.ini
echo "AddType application/x-httpd-php .php" >> /etc/httpd24/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >> /etc/httpd24/httpd.conf

验证php测试页

[root@ansible php]# vim templates/index.php.j2
[root@ansible php]# cat templates/index.php.j2
<?php
phpinfo();
?>

编辑php文件

[root@ansible php]#  cat tasks/main.yml 
---
# tasks file for php
- name: install pkgs
  yum:
    name: "{{ phppkgs }}"
    state: present

- name: unzip
  unarchive:
    src: php-7.1.10.tar.gz
    dest: /opt/

- name: php.sh
  script: php.sh

- name: config
  replace:
    path: /etc/httpd24/httpd.conf
    regexp: "index.html"
    replace: "index.php index.html"

- name: rm index.html
  shell: 
    cmd: rm -rf /usr/local/apache/htdocs/index.html

- name: edit index.php
  template:
    src: index.php.j2
    dest: /usr/local/apache/htdocs/index.php
    
- name: restart httpd
  service:
    name: httpd
    state: restarted

编辑启动文件

[root@ansible ansible]# cat php.yml 
---
- name: use php role
  hosts: node1
  roles:
    - php

//启动
[root@ansible ansible]# ansible-playbook php.yml 

PLAY [use php role] ***************************************************************************************************************

TASK [php : install pkgs] *********************************************************************************************************
changed: [node1]

TASK [php : unzip] ****************************************************************************************************************
changed: [node1]

TASK [php.sh] *********************************************************************************************************************
changed: [node1]

TASK [php : 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' in ansible.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=7    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值