十六、03-使用Ansible Playbook自动化搭建LNMP环境

使用Ansible Playbook自动化搭建LNMP环境

1、环境配置

主机名 配置 运行服务 备注
node01 2核4G nginx
node02 1核2G php、mysql

2、准备工作

  1. 安装ansible工具
# yum install -y ansible
  1. 配置主机名,做免密
# vim /etc/hosts
192.168.85.128  node01
192.168.85.129  node02

# ssh-keygen

# ssh-copy-id root@node01
# ssh-copy-id root@node02

  1. 将使用到的文件放到指定目录(我已经打包上传到百度云,需要的自取)
    链接:https://pan.baidu.com/s/1kXUi_ZIGtwtMJoDuS0bliA
    提取码:y0ik
root@DESKTOP-Q4O3FV5:/mnt/d/ansible/lnmp# ll
total 7056
drwxrwxrwx 1 root root    4096 Jul 17 16:35 ./
drwxrwxrwx 1 root root    4096 Jul 17 16:43 ../
-rwxrwxrwx 1 root root    3824 Jul 17 15:43 config.inc.php*
-rwxrwxrwx 1 root root     310 Jul 17 14:40 ilinux.conf*
-rwxrwxrwx 1 root root      22 Jul 17 14:41 index.html*
-rwxrwxrwx 1 root root      24 Jul 17 14:37 index.php*
-rwxrwxrwx 1 root root      94 Jul 17 16:13 node01-pma.sh*
-rwxrwxrwx 1 root root      82 Jul 17 16:35 node02-pma.sh*
-rwxrwxrwx 1 root root 7203314 Jul 17 15:42 phpMyAdmin-4.0.10.20-all-languages.tar.gz*
-rwxrwxrwx 1 root root     790 Jul 17 14:38 server.cnf*
-rwxrwxrwx 1 root root   10016 Jul 17 14:39 www.conf*
  1. 编写playbook
# vim lnmp.yaml
#一、安装软件包
- hosts: node01
  remote_user: root
  tasks:
  - name: install epel
    yum: name=epel-release state=latest
  - name: install nginx
    yum: name=nginx state=latest
- hosts: node02
  remote_user: root
  tasks:
  - name: install epel
    yum: name=epel-release state=latest
  - name: install php mysql
    yum: name={{ item }} state=latest
    with_items:
    - php-fpm
    - php-mysql
    - php-mbstring
    - php-mcrypt
    - mariadb-server
#配置服务,并启动服务
- hosts: node01
  remote_user: root
  tasks:
  - name: mkdir nginx root dir
    shell: mkdir -pv /data/nginx/html
  - name: copy nginx config
    copy: src=/mnt/d/ansible/lnmp/phpMyAdmin-4.0.10.20-all-languages.tar.gz dest=/data/nginx/html/
  - name: tar zxvf phpMyAdmin-4.0.10.20-all-languages.tar.gz
    shell: tar zxvf /data/nginx/html/phpMyAdmin-4.0.10.20-all-languages.tar.gz -C /data/nginx/html/
  - name: copy  pma.sh
    copy: src=/mnt/d/ansible/lnmp/node01-pma.sh  dest=/data/nginx/html/
  - name: bash pma.sh
    shell: bash /data/nginx/html/node01-pma.sh
  - name: copy config inc php
    copy: src=/mnt/d/ansible/lnmp/config.inc.php dest=/data/nginx/html/pma/
  - name: copy nginx config ilinux.conf
    copy: src=/mnt/d/ansible/lnmp/ilinux.conf dest=/etc/nginx/conf.d
  - name: start nginx service
    service: name=nginx state=restarted
- hosts: node02
  remote_user: root
  tasks:
  - name: mkdir /var/lib/php/session
    shell: mkdir -pv /var/lib/php/session/ && chown apache:apache /var/lib/php/session/
  - name: copy www.conf
    copy: src=/mnt/d/ansible/lnmp/www.conf dest=/etc/php-fpm.d/
  - name: start php-fpm
    service: name=php-fpm state=restarted
  - name: copy nginx config
    copy: src=/mnt/d/ansible/lnmp/phpMyAdmin-4.0.10.20-all-languages.tar.gz dest=/data/apps/
  - name: tar zxvf phpMyAdmin-4.0.10.20-all-languages.tar.gz
    shell: tar zxvf /data/nginx/html/phpMyAdmin-4.0.10.20-all-languages.tar.gz -C /data/apps/
  - name: copy  pma.sh
    copy: src=/mnt/d/ansible/lnmp/node02-pma.sh  dest=/data/apps
  - name: bash pma.sh
    shell: bash /data/apps/node02-pma.sh
  - name: copy config.inc.php
    copy: src=/mnt/d/ansible/lnmp/config.inc.php dest=/data/apps/pma/
  - name: copy config index.php
    copy: src=/mnt/d/ansible/lnmp/index.php dest=/data/apps/
    tags: copyindexphp
  - name: copy mariadb config
    copy: src=/mnt/d/ansible/lnmp/server.cnf dest=/etc/my.cnf.d/
  - name: start mysql
    service: name=mariadb state=restarted

三、使用ansible-playbook运行

root@DESKTOP-Q4O3FV5:~/playbooks# ansible-playbook lnmp.yaml
PLAY [node01] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [node01]
TASK [install epel] *****************************************************************************************************************
ok: [node01]
TASK [install nginx] ****************************************************************************************************************
ok: [node01]
PLAY [node02] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [node02]
TASK [install epel] *****************************************************************************************************************
ok: [node02]
TASK [install php mysql] ************************************************************************************************************
ok: [node02] => (item=[u'php-fpm', u'php-mysql', u'php-mbstring', u'php-mcrypt', u'mariadb-server'])
PLAY [node01] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [node01]
TASK [mkdir nginx root dir] *********************************************************************************************************
 [WARNING]: Consider using the file module with state=directory rather than running mkdir.  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: [node01]
TASK [copy nginx config] ************************************************************************************************************
ok: [node01]
TASK [tar zxvf phpMyAdmin-4.0.10.20-all-languages.tar.gz] ***************************************************************************
 [WARNING]: Consider using the unarchive module rather than running tar.  If you need to use command because unarchive 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: [node01]
TASK [copy  pma.sh] *****************************************************************************************************************
changed: [node01]
TASK [bash pma.sh] ******************************************************************************************************************
changed: [node01]
TASK [copy config inc php] **********************************************************************************************************
ok: [node01]
TASK [copy nginx config ilinux.conf] ************************************************************************************************
ok: [node01]
TASK [start nginx service] **********************************************************************************************************
changed: [node01]

PLAY [node02] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [node02]
TASK [mkdir /var/lib/php/session] ***************************************************************************************************
changed: [node02]
TASK [copy www.conf] ****************************************************************************************************************
ok: [node02]

TASK [start php-fpm] ****************************************************************************************************************
changed: [node02]
TASK [copy nginx config] ************************************************************************************************************
changed: [node02]
TASK [tar zxvf phpMyAdmin-4.0.10.20-all-languages.tar.gz] ***************************************************************************
changed: [node02]
TASK [copy  pma.sh] *****************************************************************************************************************
changed: [node02]
TASK [bash pma.sh] ******************************************************************************************************************
changed: [node02]
TASK [copy config.inc.php] **********************************************************************************************************
changed: [node02]
TASK [copy config index.php] ********************************************************************************************************
changed: [node02]
TASK [copy mariadb config] **********************************************************************************************************
ok: [node02]
TASK [start mysql] ******************************************************************************************************************
changed: [node02]
PLAY RECAP **************************************************************************************************************************
node01                     : ok=12   changed=5    unreachable=0    failed=0
node02                     : ok=15   changed=9    unreachable=0    failed=0

运行完毕,没有报错,进行验证


image.png

image.png

备注:如果用主机名进行访问的话,记得在windows的hosts里面配置解析

你可能感兴趣的:(十六、03-使用Ansible Playbook自动化搭建LNMP环境)