Ansible分離部署lamp架構
實驗環境
| 主機 | IP |
|---|---|
| ansible | 192.168.200.135 |
| apache | 192.168.200.136 |
| nginx | 192.168.200.129 |
| PHP | 192.168.200.138 |
安裝apache
在ansible主機上執行命令安裝apache服務
[root@localhost ansible]# ansible 192.168.200.136 -m yum -a 'name=httpd state=present'
192.168.200.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64",
"Installed: mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64",
"Installed: apr-1.6.3-9.el8.x86_64",
"Installed: httpd-filesystem-2.4.37-21.module+el8.2.0+5008+cca404a3.noarch",
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: redhat-logos-httpd-81.1-1.el8.noarch",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
"Installed: httpd-tools-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64"
]
}
[root@localhost ansible]#
設定apache服務開機自啟
[root@localhost ansible]# ansible 192.168.200.134 -m service -a 'name=httpd state=started enabled=yes'
192.168.200.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "network.target nss-lookup.target systemd-tmpfiles-setup.service -.mount sysinit.target httpd-init.service tmp.mount system.slice systemd-journald.socket remote-fs.target basic.target",
"AllowIsolate": "no",
"AllowedCPUs": "",
"AllowedMemoryNodes": "",
"AmbientCapabilities": "",
"AssertResult": "no",
"AssertTimestampMonotonic": "0",
"Before": "shutdown.target",
"BlockIOAccounting": "no",
"BlockIOWeight": "[not set]",
"CPUAccounting": "no",
"CPUAffinity": "",
"CPUQuotaPerSecUSec": "infinity",
"CPUSchedulingPolicy": "0",
"CPUSchedulingPriority": "0",
"CPUSchedulingResetOnFork": "no",
"CPUShares": "[not set]",
"CPUUsageNSec": "[not set]",
"CPUWeight": "[not set]",
"CacheDirectoryMode": "0755",
"CanIsolate": "no",
配置防火墻規則放行http服務
[root@localhost ansible]# ansible 192.168.200.134 -m firewalld -a 'service=http zone=public permanent=yes state=enabled'
192.168.200.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent operation, Changed service http to enabled"
}
[root@localhost ansible]#
[root@localhost ansible]# ansible 192.168.200.136 -m firewalld -a 'service=https zone=public permanent=yes state=enabled'
192.168.200.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent operation, Changed service https to enabled"
}
[root@localhost ansible]#
重新加載防火墻配置
[root@localhost ansible]# ansible 192.168.200.136 -a 'firewall-cmd --reload'
192.168.200.136 | CHANGED | rc=0 >>
success
[root@localhost ansible]#
測驗apache頁面能否訪問

安裝mysql服務
在ansible主機上安裝mysql服務
[root@localhost ansible]# ansible 192.168.200.129 -m yum -a 'name=mariadb state=present'
192.168.200.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: mariadb-connector-c-3.0.7-1.el8.x86_64",
"Installed: mariadb-connector-c-config-3.0.7-1.el8.noarch",
"Installed: mariadb-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
"Installed: mariadb-common-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64"
]
}
[root@localhost ansible]#
//安裝mariadb-server
[root@localhost ansible]# ansible 192.168.200.129 -m yum -a 'name=mariadb-server state=present'
192.168.200.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: mariadb-backup-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
"Installed: mariadb-errmsg-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
"Installed: mariadb-gssapi-server-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
"Installed: mariadb-server-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
"Installed: mariadb-server-utils-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64",
"Installed: perl-DBD-MySQL-4.046-3.module+el8.1.0+2938+301254e2.x86_64"
]
}
啟動maysql并設定開機自啟
[root@localhost ansible]# ansible 192.168.200.129 -m service -a 'name=mariadb state=started enabled=yes'
192.168.200.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "mariadb",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "basic.target tmp.mount sysinit.target system.slice -.mount systemd-tmpfiles-setup.service systemd-journald.socket network.target",
"AllowIsolate": "no",
"AllowedCPUs": "",
"AllowedMemoryNodes": "",
"AmbientCapabilities": "",
"AssertResult": "no",
"AssertTimestampMonotonic": "0",
安裝php服務
在ansible上執行命令安裝php服務
[root@localhost ansible]# ansible 192.168.200.138 -m yum -a 'name=php state=present'
192.168.200.138 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: nginx-filesystem-1:1.14.1-9.module+el8.0.0+4108+af250afe.noarch",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: php-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
"Installed: php-cli-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
"Installed: httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64",
"Installed: php-common-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
"Installed: mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64",
"Installed: apr-1.6.3-9.el8.x86_64",
"Installed: httpd-filesystem-2.4.37-21.module+el8.2.0+5008+cca404a3.noarch",
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: redhat-logos-httpd-81.1-1.el8.noarch",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
"Installed: php-fpm-7.2.24-1.module+el8.2.0+4601+7c76a223.x86_64",
"Installed: httpd-tools-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64"
]
}
[root@localhost ansible]#
安裝php相關組件
[root@localhost ansible]# ansible 192.168.200.138 -m yum -a 'name=php-* state=present'
192.168.200.138 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: lm_sensors-libs-3.4.0-21.20180522git70f7e08.el8.x86_64",
"Installed: glibc-devel-2.28-101.el8.x86_64",
"Installed: glibc-headers-2.28-101.el8.x86_64",
"Installed: libtool-2.4.6-25.el8.x86_64",
"Installed: autoconf-2.69-27.el8.noarch",
"Installed: libzip-1.5.1-2.module+el8.1.0+3202+af5476b9.x86_64",
"Installed: libpq-12.1-3.el8.x86_64",
"Installed: m4-1.4.18-7.el8.x86_64",
[root@localhost ansible]# ansible 192.168.200.138 -m yum -a 'name=curl state=present'
192.168.200.138 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": "Nothing to do",
"rc": 0,
"results": []
}
[root@localhost ansible]# ansible 192.168.200.138 -m yum -a 'name=curl-devel state=present'
192.168.200.138 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: libcurl-devel-7.61.1-12.el8.x86_64"
]
}
[root@localhost ansible]#
配置apache和php服務
httpd服務配置
[root@localhost ansible]# ansible 192.168.200.136 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf line="<VirtualHost 192.168.200.136:80>\nDocumentRoot "/var/www/html/www1"\nServerName www.node2.com\nProxyRequests off\nProxyPassMatch ^/(.*\.php)$ fcgi://192.168.200.138:9000/var/www/html/www1/$1\n<Directory "/var/www/html/www1">\nOptions None\nAllowOverride None\nOrder allow,deny\nAllow from all\n</Directory>\n</VirtualHost>"'
192.168.200.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@localhost ansible]#
[root@localhost ansible]# ansible 192.168.200.136 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^AddType " insertafter="^AddType application/x-" line="AddType application/x-httpd-php .php"'
192.168.200.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@localhost ansible]#
'path=/etc/httpd/conf/httpd.conf regexp="^AddType " insertafter="^AddType application/x-" line="AddType application/x-httpd-php .php"'
192.168.200.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@localhost ansible]#
[root@localhost ansible]# ansible 192.168.200.136 -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^DirectoryIndex" line="DirectoryIndex index.html index.php"'
192.168.200.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
php服務配置
[root@localhost ansible]# ansible 192.168.200.138 -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen =" line="listen = 192.168.200.138:9000"'
192.168.200.138 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@localhost ansible]#
[root@localhost ansible]# ansible 192.168.200.138 -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="^listen.allowed_clients =" line="listen.allowed_clients = 192.168.200.136"'
192.168.200.138 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@localhost ansible]#
重啟php和apache服務
[root@localhost ansible]# ansible 192.168.200.138 -m service -a 'name=php-fpm state=restarted'
192.168.200.138 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "php-fpm",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
[root@localhost ansible]# ansible apache -m service -a 'name=httpd state=restarted'
192.168.200.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestamp": "Mon 2021-07-19 20:39:27 CST",
"ActiveEnterTimestampMonotonic": "14767651884",
"ActiveExitTimestamp": "Mon 2021-07-19 20:39:26 CST",
"ActiveExitTimestampMonotonic": "14766489544",
"ActiveState": "active",
"After": "network.target nss-lookup.target systemd-tmpfiles-setup.service basic.target systemd-journald.socket system.slice -.mount tmp.mount remote-fs.target sysinit.target httpd-init.service",
"AllowIsolate": "no",
"AllowedCPUs": "",
訪問頁面測驗

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/289232.html
標籤:其他
上一篇:ansible分離部署lamp
