使用Ansible Role自动化搭建LNMP环境
1、环境配置
主机名 | 配置 | 运行服务 | 备注 |
---|---|---|---|
node01 | 2核4G | nginx | |
node02 | 1核2G | php、mysql |
2、准备工作
- 安装ansible工具
# yum install -y ansible
- 配置主机名,做免密
# 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
- 将使用到的文件放到指定目录(我已经打包上传到百度云,需要的自取)
链接:https://pan.baidu.com/s/1pWt-iIcfB9diz7ieygF4Cw
提取码:at1j
root@DESKTOP-Q4O3FV5:/etc/ansible/roles# tree lnmp-nginx/
lnmp-nginx/
├── default
├── files
│ ├── config.inc.php
│ ├── ilinux.conf
│ ├── node01-pma.sh
│ └── phpMyAdmin-4.0.10.20-all-languages.tar.gz
├── handlers
├── meta
├── tasks
│ └── main.yaml
├── templates
└── vars
root@DESKTOP-Q4O3FV5:/etc/ansible/roles# tree lnmp-php/
lnmp-php/
├── default
├── files
│ ├── config.inc.php
│ ├── index.php
│ ├── node02-pma.sh
│ ├── phpMyAdmin-4.0.10.20-all-languages.tar.gz
│ └── www.conf
├── handlers
├── meta
├── tasks
│ └── main.yaml
├── templates
└── vars
root@DESKTOP-Q4O3FV5:/etc/ansible/roles# tree lnmp-mysql/
lnmp-mysql/
├── default
├── files
│ └── server.cnf
├── handlers
├── meta
├── tasks
│ └── main.yaml
├── templates
└── vars
- 编写playbook
# vim lnmp-role.yaml
- hosts: node01
remote_user: root
roles:
- lnmp-nginx
- hosts: node02
remote_user: root
roles:
- lnmp-php
- lnmp-mysql
三、使用ansible-playbook运行
root@DESKTOP-Q4O3FV5:~/playbooks# ansible-playbook lnmp-role.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
运行完毕,没有报错,进行验证
备注:如果用主机名进行访问的话,记得在windows的hosts里面配置解析