通过ansible快速部署lamp平台

22 篇文章 0 订阅
14 篇文章 0 订阅

通过ansible快速部署lamp平台

环境说明

系统平台ip安装的项目
centos8192.168.101.120httpd
centos8192.168.101.200mysql
centos8192.168.101.210PHP

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",

查看结果
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值