通过ansible快速部署lamp平台
环境说明
系统平台 | ip | 安装的项目 |
---|---|---|
centos8 | 192.168.101.120 | httpd |
centos8 | 192.168.101.200 | mysql |
centos8 | 192.168.101.210 | PHP |
lamp平台软件安装次序
httpd------>mysql------>php
部署前检查
[root@localhost ansible]# cat inventory
[apache]
192.168.101.120 ansible_user=root ansible_password=a
[mysql]
192.168.101.200 ansible_user=root ansible_password=a
[php]
192.168.101.210 ansible_user=root ansible_password=a
[root@localhost ansible]# ansible all -m ping
192.168.101.120 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.101.210 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.101.200 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
远程关闭防火墙
[root@localhost ansible]# ansible 192.168.101.120 -m service -a "name=firewalld.service state=stopped enabled=no "
192.168.101.120 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": false,
"name": "firewalld.service",
"state": "stopped",
[root@localhost ansible]# ansible 192.168.101.200 -m service -a "name=firewalld.service state=stopped enabled=no "
192.168.101.200 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": false,
"name": "firewalld.service",
"state": "stopped",
[root@localhost ansible]# ansible 192.168.101.210 -m service -a "name=firewalld.service state=stopped enabled=no "
192.168.101.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": false,
"name": "firewalld.service",
"state": "stopped",
部署过程
通过ansible控制远程安装httpd服务
[root@localhost ansible]# ansible 192.168.101.120 -m service -a "name=httpd state=started enabled=yes "
192.168.101.120 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
检查网站是否生成
通过ansible控制远程安装mariadb服务
[root@localhost ansible]# ansible 192.168.101.200 -m service -a "name=mariadb state=started enabled=yes "
192.168.101.200 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "mariadb",
"state": "started",
通过ansible控制远程安装php服务
[root@localhost ansible]# ansible 192.168.101.210 -m yum -a "name='php' state='installed'"192.168.101.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [......
[root@localhost ansible]# ansible 192.168.101.210 -m yum -a "name='php-mysqlnd.x86_64' state='installed'"
192.168.101.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [.......
对httpd进行配置
[root@localhost ansible]# ansible 192.168.101.120 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf line="<VirtualHost *:80>\nDocumentRoot "/usr/local/apache/htdocs"\nServerName www.ljt.com\nErrorLog "logs/www.example.com-error_log"\nCustomLog "logs/www.example.com-access_log" common|nProxyRequests off\nProxyPassMatch ^/(.*\.php)$ fcgi://192.168.101.210:9000/var/www/html/$1\n<Directory "/var/www/html/">\n Require all granted\n</Directory>\n</VirtualHost>"'192.168.101.120 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@localhost ansible]# ansible 192.168.101.120 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^AddType " insertafter="^AddType application/x-gzip .gz .tgz" line="AddType application/x-httpd-php .php\nAddType application/x-httpd-php-source .phps"'
192.168.101.120 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@localhost ansible]# ansible 192.168.101.120 -m service -a "name='httpd' state='restarted' "
192.168.101.120 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
对PHP进行配置
[root@localhost ansible]# ansible 192.168.101.210 -m command -a "chdir=/var/www/html touch index.php"
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. 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.
192.168.101.210 | CHANGED | rc=0 >>
[root@localhost ansible]# ansible 192.168.101.210 -m lineinfile -a "name=/var/www/html/index.php line='<?php\nphpinfo();\n?>'"
192.168.101.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@localhost ansible]# ansible 192.168.101.210 -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen =" line="listen = 192.168.100.210:9000"'
192.168.101.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@localhost ansible]# ansible 192.168.101.210 -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen.allowed_clients =" line="listen.allowed_clients = 192.168.101.120"'
192.168.101.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
重启服务
[root@localhost ansible]# ansible 192.168.101.210 -m service -a "name='php-fpm' state='restarted' "
192.168.101.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "php-fpm",
"state": "started",
[root@localhost ansible]# ansible 192.168.101.120 -m service -a "name='httpd' state='restarted' "
192.168.101.120 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
查看结果