Ansible使用角色部署LAMP架构

使用角色部署lamp架构

[root@ansible ansible]# cd roles/
[root@ansible roles]# ansible-galaxy init php
- Role php was created successfully
[root@ansible roles]# ansible-galaxy init apache
- Role apache was created successfully
[root@ansible roles]# ansible-galaxy init mysql
- Role mysql was created successfully

部署apache

编写任务

[root@ansible roles]# cd apache/
[root@ansible apache]# vim tasks/main.yml
---
# tasks file for apache
- name: set yum
  script: yum.sh

- name: install packages
  yum:
    name: "{{ httpd_pack }}"
    state: present

- name: unzip apr
  unarchive:
    src: apr-1.6.5.tar.bz2
    dest: /usr/src/

- name: install apr
  script: apr.sh

- name: unzip apr-util
  unarchive:
    src: apr-util-1.6.1.tar.bz2
    dest: /usr/src/

- name: install apr-util
  script: apr-util.sh

- name: unzip httpd
  unarchive:
    src: httpd-2.4.54.tar.bz2
    dest: /usr/src/

- name: install httpd
  script: httpd.sh

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

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

- name: refresh
  shell:
    cmd: systemctl daemon-reload

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

- 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

编写脚本

[root@ansible apache]# vim files/yum.sh
#!/bin/bash
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum reinstall -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*


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


[root@ansible apache]# vim files/apr-util.sh
#!/bin/bash
cd /usr/src/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install


[root@ansible apache]# vim files/httpd.sh
#!/bin/bash
cd /usr/src/httpd-2.4.54
./configure --prefix=/usr/local/apache \
        --sysconfdir=/etc/http24 \
        --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
echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
echo "MANDATORY_MANPATH                       /usr/local/apache/man" >>/etc/man_db.conf
ln -s /usr/local/apache/include/ /usr/include/httpd

配置变量

[root@ansible apache]# vim vars/main.yml
---
# vars file for apache
httpd_pack:
  - openssl-devel
  - pcre-devel
  - expat-devel
  - libtool
  - gcc
  - gcc-c++
  - vim
  - bzip2
  - wget
  - make

配置模板

[root@ansible apache]# vim templates/httpd.service.j2
[Unit]
Description=apache server daemon
After=network.target sshd-keygen.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

编写playbook执行

[root@ansible ansible]# vim httpd.yml
---
- name: install apache
  hosts: node1
  roles:
    - apache

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

PLAY [install apache] ****************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [node1]

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

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

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

TASK [apache : install apr] **********************************************************************************************************************************************************************************
changed: [node1]

TASK [apache : unzip apr-util] *******************************************************************************************************************************************************************************
ok: [node1]

TASK [apache : install apr-util] *****************************************************************************************************************************************************************************
changed: [node1]

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

TASK [apache : install httpd] ********************************************************************************************************************************************************************************
changed: [node1]

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

TASK [apache : set httpd service] ****************************************************************************************************************************************************************************
ok: [node1]

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

TASK [apache : start httpd service] **************************************************************************************************************************************************************************
ok: [node1]

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

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

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

PLAY RECAP ***************************************************************************************************************************************************************************************************
node1                      : ok=16   changed=9    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


[root@node1 ~]# ss -antl
LISTEN                  0                       128                                                    *:80                                                  *:*

部署mysql

编写任务

[root@ansible roles]# cd mysql/
[root@ansible mysql]# vim tasks/main.yml
---
# tasks file for mysql
- name: install packages
  yum:
    name: "{{ mysql_pack }}"
    state: present

- name: unzip
  unarchive:
    src: mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
    dest: /opt/

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

- name: create link
  file:
    path: /opt/mysql
    src: /opt/mysql-5.7.38-linux-glibc2.12-x86_64
    owner: mysql
    group: mysql
    state: link

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

- name: mysql initial
  script: initial.sh

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

- name: script
  script: script.sh

- name: refresh
  shell:
    cmd: systemctl daemon-reload

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

- name: set mysql passwd
  shell:
    cmd: /opt/mysql/bin/mysql -uroot -e "set password = password('123456')"

配置变量

[root@ansible mysql]# vim vars/main.yml
---
# vars file for mysql
mysql_pack:
  - ncurses-devel
  - openssl
  - cmake
  - ncurses-compat-libs

编写脚本

[root@ansible mysql]# vim files/script.sh
#!/bin/bash
cp -a /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
cat >> /etc/init.d/mysqld <<EOF
basedir=/opt/mysql
datadir=/opt/mysql_data
EOF


[root@ansible mysql]# vim files/initial.sh
#!/bin/bash
echo 'export PATH=$PATH:/opt/mysql/bin' >/etc/profile.d/mysql.sh
ln -s /opt/mysql/include  /usr/include/mysql
echo '/opt/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
echo "MANDATORY_MANPATH                       /opt/mysql/man" >> /etc/man_db.conf
source /etc/profile.d/mysql.sh
/opt/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/mysql_data/

编写模板

[root@ansible mysql]# vim templates/my.cnf.j2
[mysqld]
basedir = /opt/mysql
datadir = /opt/mysql_data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysql_data/mysql.pid
log-error=/var/log/mysqld.log
user = mysql
skip-name-resolve



[root@ansible mysql]# vim templates/mysqld.service.j2
[Unit]
Description=mysqld server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

编写playbook执行

[root@ansible ansible]# vim mysql.yml
---
- name: install mysql
  hosts: node1
  roles:
    - mysql

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

PLAY [install mysql] *****************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [node1]

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

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

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

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

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

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

TASK [mysql : cp my.cnf] *************************************************************************************************************************************************************************************
ok: [node1]

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

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

TASK [restart mysqld] ****************************************************************************************************************************************************************************************
changed: [node1]

TASK [set mysql passwd] **************************************************************************************************************************************************************************************
changed: [node1]

PLAY RECAP ***************************************************************************************************************************************************************************************************
node1                      : ok=12   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

部署PHP

编写任务

[root@ansible roles]# cd php/
[root@ansible php]# vim tasks/main.yml
---
# tasks file for php
- name: install packages
  yum:
    name: "{{ php_pack }}"
    state: present

- name: unzip
  unarchive:
    src: php-7.4.30.tar.xz
    dest: /usr/src

- name: install php
  script: php.sh

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

- name: refresh
  shell:
    cmd: systemctl daemon-reload

- name: set php service
  service:
    name: php-fpm
    state: started
    enabled: yes

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

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

- name: cp index.php
  template:
    src: index.php.j2
    dest: /usr/local/apache/htdocs/index.php
    owner: apache
    group: apache

- name: restart httpd
  service:
    name: httpd
    state: restarted

配置变量

[root@ansible php]# vim vars/main.yml
---
# vars file for php
php_pack:
  - openssl
  - openssl-devel
  - bzip2-devel
  - libcurl
  - libcurl-devel
  - libicu-devel
  - libjpeg
  - libjpeg-devel
  - libpng
  - libpng-devel
  - openldap-devel
  - freetype
  - freetype-devel
  - gmp
  - gmp-devel
  - libmcrypt
  - libmcrypt-devel
  - readline
  - readline-devel
  - libxslt
  - libxslt-devel
  - mhash
  - mhash-devel
  - php-mysqlnd
  - sqlite-devel
  - libzip-devel
  - libxml2-devel
  - pcre-devel
  - http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

编写脚本

[root@ansible php]# vim files/php.sh
#!/bin/bash
cd /usr/src/php-7.4.30
./configure --prefix=/usr/local/php7 \
        --with-config-file-path=/etc \
        --enable-fpm \
        --enable-inline-optimization \
        --disable-debug \
        --disable-rpath \
        --enable-shared \
        --enable-soap \
        --with-openssl \
        --enable-bcmath \
        --with-iconv \
        --with-bz2 \
        --enable-calendar \
        --with-curl \
        --enable-exif \
        --enable-ftp \
        --enable-gd \
        --with-jpeg \
        --with-zlib-dir \
        --with-freetype \
        --with-gettext \
        --enable-json \
        --enable-mbstring \
        --enable-pdo \
        --with-mysqli=mysqlnd \
        --with-pdo-mysql=mysqlnd \
        --with-readline \
        --enable-shmop \
        --enable-simplexml \
        --enable-sockets \
        --with-zip \
        --enable-mysqlnd-compression-support \
        --with-pear \
        --enable-pcntl \
        --with-apxs2=/usr/local/apache/bin/apxs \
        --enable-posix

make
make install

echo "export PATH=$PATH:/usr/local/php7/bin" > /etc/profile.d/php.sh
source /etc/profile.d/php.sh
ln -s /usr/local/php7/include/ /usr/include/php
echo "/usr/local/php7/lib" > /etc/ld.so.conf.d/php.conf
ldconfig

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

echo "AddType application/x-httpd-php .php" >>/etc/http24/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >>/etc/http24/httpd.conf

sed -i '/proxy_module/s/#//g' /etc/http24/httpd.conf
sed -i '/proxy_fcgi_module/s/#//g' /etc/http24/httpd.conf

编写模板

[root@ansible php]# vim templates/php-fpm.service.j2
[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target



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

编写playbook执行

[root@ansible ansible]# vim php.yml
---
- name: install php
  hosts: node1
  roles:
    - php

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

PLAY [install php] *******************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [node1]

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

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

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

TASK [php : cp template] *************************************************************************************************************************************************************************************
ok: [node1]

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

TASK [set php service] ***************************************************************************************************************************************************************************************
changed: [node1]

TASK [php : modify apache config] ****************************************************************************************************************************************************************************
changed: [node1]

TASK [php : rm file] *****************************************************************************************************************************************************************************************
[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 [cp index.php] ******************************************************************************************************************************************************************************************
changed: [node1]

TASK [php : restart httpd] ***********************************************************************************************************************************************************************************
changed: [node1]

PLAY RECAP ***************************************************************************************************************************************************************************************************
node1                      : ok=11   changed=9    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、付费专栏及课程。

余额充值