一、使用ansible进行lamp的分离部署
利用ansible对受控主机进行lamp的分离部署
| 主机名 | IP地址 |
|---|---|
| ansible | 192.168.10.200 |
| apache | 192.168.10.201 |
| mysql | 192.168.10.202 |
| php | 192.168.20.203 |
先在ansible主机上添加受管主机清单
[root@ansible ~]# mkdir /lamp
[root@ansible ~]# cp -a /etc/ansible/ansible.cfg /lamp/
[root@ansible ~]# cd /lamp/
[root@ansible lamp]# ls
ansible.cfg
[root@ansible lamp]# vim inventory
[apache]
192.168.10.201
[mysql]
192.168.10.202
[php]
192.168.10.203
[root@ansible lamp]#
创建免密登陆,每台受控主机都需要传一遍
[root@ansible lamp]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:WUVrG3vL3qYlK92Af3/eFRFSXj5Uq6+VOB6mwzWaeYA root@ansible
The key's randomart image is:
+---[RSA 3072]----+
| .o..++|
| . .+.+|
| . + =.|
| o . +. o|
| S . oo.. |
| E ..=+.o|
| . *B=*o|
| *=oB=B|
| .o++=B|
+----[SHA256]-----+
[root@ansible lamp]# ssh-copy-id 192.168.10.201
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.10.201 (192.168.10.201)' can't be established.
ECDSA key fingerprint is SHA256:c2GvkoHrANWAjzMHhZkjGTGk1jt4IwwdYfgZ7Ltas5k.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.10.201's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.10.201'"
and check to make sure that only the key(s) you wanted were added.
[root@ansible lamp]#
使用ping模块测试受控主机是否连通
[root@ansible lamp]# ansible all -m ping
192.168.10.202 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.10.201 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.10.203 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@ansible lamp]#
使用shell模块和lineinfile模块为所有主机关闭防火墙,selinux
[root@ansible lamp]# ansible all -m shell -a 'systemctl stop firewalld && systemctl disable firewalld '
192.168.10.202 | CHANGED | rc=0 >>
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
192.168.10.203 | CHANGED | rc=0 >>
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
192.168.10.201 | CHANGED | rc=0 >>
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@ansible lamp]# ansible all -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled"'
192.168.10.201 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": false,
"msg": ""
}
192.168.10.202 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
192.168.10.203 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@ansible lamp]#
使用yum模块为Apache主机安装Apache服务
[root@ansible lamp]# ansible apache -m yum -a "name=httpd"
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: httpd-2.4.37-30.module_el8.3.0+462+ba287492.0.1.x86_64",
"Installed: httpd-filesystem-2.4.37-30.module_el8.3.0+462+ba287492.0.1.noarch",
"Installed: apr-1.6.3-11.el8.x86_64",
"Installed: httpd-tools-2.4.37-30.module_el8.3.0+462+ba287492.0.1.x86_64",
"Installed: centos-logos-httpd-80.5-2.el8.noarch",
"Installed: mailcap-2.1.48-3.el8.noarch",
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64"
]
}
[root@ansible lamp]#
使用yum模块为mysql主机安装mysql服务
[root@ansible lamp]# ansible mysql -m yum -a 'name=mariadb*'
192.168.10.202 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: perl-Pod-Perldoc-3.28-396.el8.noarch",
"Installed: perl-Pod-Simple-1:3.35-395.el8.noarch",
"Installed: perl-Pod-Usage-4:1.69-395.el8.noarch",
"Installed: perl-Scalar-List-Utils-3:1.49-2.el8.x86_64",
"Installed: perl-Socket-4:2.027-3.el8.x86_64",
"Installed: perl-Storable-1:3.11-3.el8.x86_64",
"Installed: perl-Term-ANSIColor-4.06-396.el8.noarch",
"Installed: perl-Term-Cap-1.17-395.el8.noarch",
"Installed: unixODBC-2.3.7-1.el8.x86_64",
"Installed: perl-Text-ParseWords-3.30-395.el8.noarch",
"Installed: perl-Text-Tabs+Wrap-2013.0523-395.el8.noarch",
"Installed: perl-Time-Local-1:1.280-1.el8.noarch",
"Installed: perl-Unicode-Normalize-1.25-396.el8.x86_64",
"Installed: perl-constant-1.33-396.el8.noarch",
"Installed: perl-interpreter-4:5.26.3-420.el8.x86_64",
"Installed: perl-libs-4:5.26.3-420.el8.x86_64",
"Installed: perl-macros-4:5.26.3-420.el8.x86_64",
"Installed: perl-parent-1:0.237-1.el8.noarch",
"Installed: perl-podlators-4.11-1.el8.noarch",
"Installed: perl-threads-1:2.21-2.el8.x86_64",
"Installed: perl-threads-shared-1.58-2.el8.x86_64",
"Installed: libaio-devel-0.3.112-1.el8.x86_64",
"Installed: policycoreutils-2.9-14.el8.x86_64",
"Installed: checkpolicy-2.9-1.el8.x86_64",
"Installed: policycoreutils-python-utils-2.9-14.el8.noarch",
"Installed: perl-IO-Socket-IP-0.39-5.el8.noarch",
"Installed: perl-IO-Socket-SSL-2.066-4.module_el8.4.0+517+be1595ff.noarch",
"Installed: psmisc-23.1-5.el8.x86_64",
"Installed: python3-audit-3.0-0.17.20191104git1c2f876.el8.x86_64",
"Installed: libverto-devel-0.3.0-5.el8.x86_64",
"Installed: libcom_err-devel-1.45.6-1.el8.x86_64",
"Installed: perl-Memoize-1.03-420.el8.noarch",
"Installed: lksctp-tools-1.0.18-3.el8.x86_64",
"Installed: python3-libsemanage-2.9-6.el8.x86_64",
"Installed: perl-Mozilla-CA-20160104-7.module_el8.3.0+416+dee7bcef.noarch",
"Installed: copy-jdk-configs-4.0-2.el8.noarch",
"Installed: perl-Net-SSLeay-1.88-1.module_el8.4.0+517+be1595ff.x86_64",
"Installed: lsof-4.93.2-1.el8.x86_64",
"Installed: python3-policycoreutils-2.9-14.el8.noarch",
"Installed: python3-setools-4.3.0-2.el8.x86_64",
"Installed: zlib-devel-1.2.11-17.el8.x86_64",
"Installed: galera-25.3.32-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: libkadm5-1.18.2-12.el8.x86_64",
"Installed: perl-Test-Simple-1:1.302135-1.el8.noarch",
"Installed: perl-Time-HiRes-4:1.9758-2.el8.x86_64",
"Installed: perl-URI-1.73-3.el8.noarch",
"Installed: rsync-3.1.3-12.el8.x86_64",
"Installed: nmap-ncat-2:7.70-5.el8.x86_64",
"Installed: perl-libnet-3.11-3.el8.noarch",
"Installed: openssl-1:1.1.1k-1.el8.x86_64",
"Installed: openssl-devel-1:1.1.1k-1.el8.x86_64",
"Installed: openssl-libs-1:1.1.1k-1.el8.x86_64",
"Installed: boost-program-options-1.66.0-10.el8.x86_64",
"Installed: nspr-4.31.0-1.el8_4.x86_64",
"Installed: lua-5.3.4-11.el8.x86_64",
"Installed: nss-3.67.0-6.el8_4.x86_64",
"Installed: perl-DBD-MySQL-4.046-3.module_el8.3.0+419+c2dec72b.x86_64",
"Installed: perl-DBI-1.641-3.module_el8.3.0+413+9be2aeb5.x86_64",
"Installed: libselinux-devel-2.9-5.el8.x86_64",
"Installed: libsemanage-2.9-6.el8.x86_64",
"Installed: libsepol-2.9-2.el8.x86_64",
"Installed: libsepol-devel-2.9-2.el8.x86_64",
"Installed: mariadb-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: nss-softokn-3.67.0-6.el8_4.x86_64",
"Installed: mariadb-backup-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: mariadb-common-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: mariadb-connector-c-3.1.11-2.el8_3.x86_64",
"Installed: mariadb-connector-c-config-3.1.11-2.el8_3.noarch",
"Installed: mariadb-connector-c-devel-3.1.11-2.el8_3.x86_64",
"Installed: mariadb-connector-odbc-3.1.12-1.el8.x86_64",
"Installed: mariadb-devel-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: nss-softokn-freebl-3.67.0-6.el8_4.x86_64",
"Installed: perl-Digest-1.17-395.el8.noarch",
"Installed: mariadb-embedded-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: java-1.8.0-openjdk-headless-1:1.8.0.302.b03-0.0.ea.el8.x86_64",
"Installed: perl-Digest-MD5-2.55-396.el8.x86_64",
"Installed: mariadb-embedded-devel-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: mariadb-errmsg-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: pcre2-devel-10.32-2.el8.x86_64",
"Installed: mariadb-gssapi-server-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: nss-sysinit-3.67.0-6.el8_4.x86_64",
"Installed: mariadb-java-client-2.2.5-3.el8.noarch",
"Installed: pcre2-utf16-10.32-2.el8.x86_64",
"Installed: mariadb-oqgraph-engine-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: keyutils-libs-devel-1.5.10-6.el8.x86_64",
"Installed: pcre2-utf32-10.32-2.el8.x86_64",
"Installed: mariadb-server-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: perl-Env-1.04-395.el8.noarch",
"Installed: mariadb-server-galera-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: nss-util-3.67.0-6.el8_4.x86_64",
"Installed: mariadb-server-utils-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: mariadb-test-3:10.3.28-1.module_el8.3.0+757+d382997d.x86_64",
"Installed: tzdata-java-2021a-1.el8.noarch",
"Installed: Judy-1.0.5-18.module_el8.3.0+757+d382997d.x86_64",
"Installed: perl-Carp-1.42-396.el8.noarch",
"Installed: krb5-devel-1.18.2-12.el8.x86_64",
"Installed: perl-Data-Dumper-2.167-399.el8.x86_64",
"Installed: perl-Encode-4:2.97-3.el8.x86_64",
"Installed: perl-Errno-1.28-420.el8.x86_64",
"Installed: perl-Exporter-5.72-396.el8.noarch",
"Installed: perl-File-Path-2.15-2.el8.noarch",
"Installed: perl-File-Temp-0.230.600-1.el8.noarch",
"Installed: perl-Getopt-Long-1:2.50-4.el8.noarch",
"Installed: javapackages-filesystem-5.3.0-1.module_el8.0.0+11+5b8c10bd.noarch",
"Installed: perl-HTTP-Tiny-0.074-1.el8.noarch",
"Installed: perl-IO-1.38-420.el8.x86_64",
"Installed: krb5-libs-1.18.2-12.el8.x86_64",
"Installed: perl-MIME-Base64-3.15-396.el8.x86_64",
"Installed: perl-Math-BigInt-1:1.9998.11-7.el8.noarch",
"Installed: perl-Math-Complex-1.59-420.el8.noarch",
"Installed: perl-PathTools-3.74-1.el8.x86_64",
"Installed: perl-Pod-Escapes-1:1.07-395.el8.noarch",
"Removed: policycoreutils-2.9-9.el8.x86_64",
"Removed: libsemanage-2.9-4.el8.x86_64",
"Removed: libsepol-2.9-1.el8.x86_64",
"Removed: openssl-1:1.1.1g-11.el8.x86_64",
"Removed: krb5-libs-1.18.2-7.el8.x86_64",
"Removed: openssl-libs-1:1.1.1g-11.el8.x86_64"
]
}
[root@ansible lamp]#
使用yum模块为php主机安装php服务
[root@ansible lamp]# ansible php -m yum -a "name=php*"
192.168.10.203 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: perl-Pod-Perldoc-3.28-396.el8.noarch",
"Installed: perl-Pod-Simple-1:3.35-395.el8.noarch",
"Installed: php-pecl-zip-1.15.3-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-Pod-Usage-4:1.69-395.el8.noarch",
"Installed: centos-logos-httpd-85.8-1.el8.noarch",
"Installed: php-pgsql-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-Scalar-List-Utils-3:1.49-2.el8.x86_64",
"Installed: perl-Socket-4:2.027-3.el8.x86_64",
"Installed: perl-Storable-1:3.11-3.el8.x86_64",
"Installed: php-process-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: unixODBC-2.3.7-1.el8.x86_64",
"Installed: perl-Term-ANSIColor-4.06-396.el8.noarch",
"Installed: perl-Term-Cap-1.17-395.el8.noarch",
"Installed: php-recode-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-Text-ParseWords-3.30-395.el8.noarch",
"Installed: php-snmp-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-Text-Tabs+Wrap-2013.0523-395.el8.noarch",
"Installed: perl-Time-Local-1:1.280-1.el8.noarch",
"Installed: perl-Unicode-Normalize-1.25-396.el8.x86_64",
"Installed: php-soap-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-constant-1.33-396.el8.noarch",
"Installed: perl-interpreter-4:5.26.3-420.el8.x86_64",
"Installed: perl-libs-4:5.26.3-420.el8.x86_64",
"Installed: php-xml-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-macros-4:5.26.3-420.el8.x86_64",
"Installed: perl-parent-1:0.237-1.el8.noarch",
"Installed: perl-podlators-4.11-1.el8.noarch",
"Installed: php-xmlrpc-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-threads-1:2.21-2.el8.x86_64",
"Installed: perl-threads-shared-1.58-2.el8.x86_64",
"Installed: perl-IO-Socket-IP-0.39-5.el8.noarch",
"Installed: perl-IO-Socket-SSL-2.066-4.module_el8.4.0+517+be1595ff.noarch",
"Installed: httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64",
"Installed: libstdc++-devel-8.4.1-1.el8.x86_64",
"Installed: httpd-filesystem-2.4.37-40.module_el8.5.0+852+0aafc63b.noarch",
"Installed: httpd-tools-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64",
"Installed: hunspell-1.6.2-1.el8.x86_64",
"Installed: libtool-2.4.6-25.el8.x86_64",
"Installed: libxcrypt-4.1.1-6.el8.x86_64",
"Installed: libxcrypt-devel-4.1.1-6.el8.x86_64",
"Installed: hunspell-en-US-0.20140811.1-12.el8.noarch",
"Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
"Installed: perl-Mozilla-CA-20160104-7.module_el8.3.0+416+dee7bcef.noarch",
"Installed: lm_sensors-libs-3.4.0-23.20180522git70f7e08.el8.x86_64",
"Installed: perl-Net-SSLeay-1.88-1.module_el8.4.0+517+be1595ff.x86_64",
"Installed: cpp-8.4.1-1.el8.x86_64",
"Installed: apr-1.6.3-11.el8.x86_64",
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: m4-1.4.18-7.el8.x86_64",
"Installed: mailcap-2.1.48-3.el8.noarch",
"Installed: libmpc-1.1.0-9.1.el8.x86_64",
"Installed: glibc-langpack-zh-2.28-161.el8.x86_64",
"Installed: autoconf-2.69-29.el8.noarch",
"Installed: libXpm-3.5.12-8.el8.x86_64",
"Installed: libwebp-1.0.0-5.el8.x86_64",
"Installed: libicu-60.3-2.el8_1.x86_64",
"Installed: net-snmp-libs-1:5.8-22.el8.x86_64",
"Installed: gcc-8.4.1-1.el8.x86_64",
"Installed: automake-1.16.1-7.el8.noarch",
"Installed: gcc-c++-8.4.1-1.el8.x86_64",
"Installed: net-snmp-1:5.8-22.el8.x86_64",
"Installed: emacs-filesystem-1:26.1-5.el8.noarch",
"Installed: net-snmp-agent-libs-1:5.8-22.el8.x86_64",
"Installed: perl-Thread-Queue-3.13-1.el8.noarch",
"Installed: libzip-1.5.1-2.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: recode-3.6-47.el8.x86_64",
"Installed: perl-URI-1.73-3.el8.noarch",
"Installed: nginx-filesystem-1:1.14.1-9.module_el8.0.0+184+e34fea82.noarch",
"Installed: libpq-13.3-1.el8_4.x86_64",
"Installed: perl-libnet-3.11-3.el8.noarch",
"Installed: enchant-1:1.6.0-21.el8.x86_64",
"Installed: isl-0.16.1-6.el8.x86_64",
"Installed: php-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: php-bcmath-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: php-cli-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: glibc-2.28-161.el8.x86_64",
"Installed: php-common-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: pcre-8.42-6.el8.x86_64",
"Installed: php-dba-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: kernel-headers-4.18.0-315.el8.x86_64",
"Installed: glibc-common-2.28-161.el8.x86_64",
"Installed: php-dbg-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: pcre-cpp-8.42-6.el8.x86_64",
"Installed: php-devel-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: mariadb-connector-c-3.1.11-2.el8_3.x86_64",
"Installed: mariadb-connector-c-config-3.1.11-2.el8_3.noarch",
"Installed: glibc-devel-2.28-161.el8.x86_64",
"Installed: php-embedded-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: tokyocabinet-1.4.48-10.el8.x86_64",
"Installed: pcre-devel-8.42-6.el8.x86_64",
"Installed: php-enchant-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-Digest-1.17-395.el8.noarch",
"Installed: pcre-utf16-8.42-6.el8.x86_64",
"Installed: glibc-headers-2.28-161.el8.x86_64",
"Installed: perl-Digest-MD5-2.55-396.el8.x86_64",
"Installed: php-fpm-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: pcre-utf32-8.42-6.el8.x86_64",
"Installed: php-gd-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: php-gmp-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: php-intl-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: php-json-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: php-ldap-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: php-mbstring-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: php-mysqlnd-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-Carp-1.42-396.el8.noarch",
"Installed: gd-2.2.5-7.el8.x86_64",
"Installed: php-odbc-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-Data-Dumper-2.167-399.el8.x86_64",
"Installed: perl-Encode-4:2.97-3.el8.x86_64",
"Installed: php-opcache-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-Errno-1.28-420.el8.x86_64",
"Installed: perl-Exporter-5.72-396.el8.noarch",
"Installed: perl-File-Path-2.15-2.el8.noarch",
"Installed: php-pdo-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-File-Temp-0.230.600-1.el8.noarch",
"Installed: binutils-2.30-108.el8.x86_64",
"Installed: perl-Getopt-Long-1:2.50-4.el8.noarch",
"Installed: perl-HTTP-Tiny-0.074-1.el8.noarch",
"Installed: php-pear-1:1.10.5-9.module_el8.2.0+313+b04d0a66.noarch",
"Installed: perl-IO-1.38-420.el8.x86_64",
"Installed: php-pecl-apcu-5.1.12-2.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-MIME-Base64-3.15-396.el8.x86_64",
"Installed: php-pecl-apcu-devel-5.1.12-2.module_el8.2.0+313+b04d0a66.x86_64",
"Installed: perl-PathTools-3.74-1.el8.x86_64",
"Installed: perl-Pod-Escapes-1:1.07-395.el8.noarch",
"Removed: pcre-8.42-4.el8.x86_64",
"Removed: glibc-2.28-141.el8.x86_64",
"Removed: glibc-common-2.28-141.el8.x86_64",
"Removed: glibc-langpack-zh-2.28-141.el8.x86_64",
"Removed: libxcrypt-4.1.1-4.el8.x86_64"
]
}
[root@ansible lamp]#
使用script模块为所有受管主机添加hosts解析
##将需要添加的内容写成脚本
[root@ansible lamp]# vim hosts.sh
[root@ansible lamp]# cat hosts.sh
#!/bin/bash
echo 192.168.10.201 apache >> /etc/hosts
echo 192.168.10.202 mysql >> /etc/hosts
echo 192.168.10.203 php >> /etc/hosts
[root@ansible lamp]#
##使用script模块
[root@ansible lamp]# ansible all -m script -a 'hosts.sh'
192.168.10.201 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.10.201 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.10.201 closed."
],
"stdout": "",
"stdout_lines": []
}
192.168.10.202 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.10.202 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.10.202 closed."
],
"stdout": "",
"stdout_lines": []
}
192.168.10.203 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.10.203 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.10.203 closed."
],
"stdout": "",
"stdout_lines": []
}
[root@ansible lamp]#
使用service模块启用apache、mysql、php服务并设置开机自启
[root@ansible lamp]# ansible apache -m service -a 'name=httpd state=started enabled=yes'
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveState": "inactive",
"AllowedCPUs": "",
"AllowedMemoryNodes": "",
…………………………………………略
}
[root@ansible lamp]# ansible mysql -m service -a 'name=mariadb state=started enabled=yes'
192.168.10.202 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "mariadb",
"state": "started",
"status": {
"ActiveState": "inactive",
"AllowedCPUs": "",
"AllowedMemoryNodes": "",
"BlockIOAccounting": "no",
"BlockIOWeight": "[not set]",
……………………………………………………略
}
[root@ansible lamp]# ansible php -m service -a 'name=php-fpm state=started enabled=yes'
192.168.10.203 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "php-fpm",
"state": "started",
"status": {
"ActiveState": "inactive",
"AllowedCPUs": "",
"AllowedMemoryNodes": "",
"BlockIOAccounting": "no",
"BlockIOWeight": "[not set]",
"CPUAccounting": "no",
"CPUAffinity": "",
"CPUAffinityFromNUMA": "no",
…………………………………………………………略
}
使用shell、lineinfile、copy模块来修改apache的配置
##修改httpd的主配文件
[root@ansible lamp]# ansible apache -m shell -a 'sed -i "/DirectoryIndex/s/index.html/index.php index.html/g" /etc/httpd/conf/httpd.conf'
[WARNING]: Consider using the replace, lineinfile or template module rather than running 'sed'. If you
need to use command because replace, lineinfile or template 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.10.201 | CHANGED | rc=0 >>
[root@ansible lamp]#
[root@ansible lamp]# ansible apache -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf insertafter=" AddType application/x-gzip .gz .tgz" line="AddType application/x-httpd-php .php"'
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@ansible lamp]# ansible apache -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf insertafter=" AddType application/x-gzip .gz .tgz" line="AddType application/x-httpd-php-source .phps"'
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@ansible lamp]#
[root@ansible lamp]# ansible apache -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf insertafter="# LoadModule foo_module modules/mod_foo.so" line="LoadModule proxy_module modules/mod_proxy.so"'
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@ansible lamp]# ansible apache -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf insertafter="# LoadModule foo_module modules/mod_foo.so" line="LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so"'
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@ansible lamp]#
##把虚拟站点文件传输到apache服务器上
[root@ansible lamp]# cat /root/httpd.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/"
ServerName web.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.10.203:9000/www/html/$1
</VirtualHost>
[root@ansible lamp]#
[root@ansible lamp]# ansible apache -m copy -a 'src=/root/httpd.conf dest=/etc/httpd/conf.d/'
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "164d64c4d5a318e5f53b862be7f5f64757fa22e7",
"dest": "/etc/httpd/conf.d/httpd.conf",
"gid": 0,
"group": "root",
"md5sum": "8ba496e98726df0f1d5d6bdaf47ccaf6",
"mode": "0644",
"owner": "root",
"size": 184,
"src": "/root/.ansible/tmp/ansible-tmp-1626617198.3737397-7188-17930658183466/source",
"state": "file",
"uid": 0
}
[root@ansible lamp]#
使用lineinfile模块来修改php的配置文件
[root@ansible lamp]# ansible php -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="listen = /usr" line="listen = 0.0.0.0:9000"'
192.168.10.203 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@ansible lamp]# ansible php -m lineinfile -a 'path=/etc/php-fpm.d/www.conf regexp="listen.allowed_clients = 127.0.0.1" line="listen.allowed_clients = 192.168.10.201"'
192.168.10.203 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@ansible lamp]#
把php测试文件index.php放到php服务器上
[root@ansible lamp]# ansible php -m copy -a 'src=index.php dest=/www/html/'
192.168.10.203 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "012ee25cceff745e681fbb3697a06f3712f55554",
"dest": "/www/html/index.php",
"gid": 0,
"group": "root",
"md5sum": "9dccf462d245f55ac3e0cdb0e5401f5b",
"mode": "0644",
"owner": "root",
"size": 20,
"src": "/root/.ansible/tmp/ansible-tmp-1626617683.2730653-7352-99852615311158/source",
"state": "file",
"uid": 0
}
[root@ansible lamp]#
使用service模块重启服务
##重启Apache服务
[root@ansible lamp]# ansible apache -m service -a 'name=httpd state=restarted'
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveState": "active",
"AllowedCPUs": "",
"AllowedMemoryNodes": "",
"BlockIOAccounting": "no",
"BlockIOWeight": "[not set]",
"CPUAccounting": "no",
"CPUAffinity": "",
"CPUAffinityFromNUMA": "no",
"CPUQuotaPerSecUSec": "infinity",
"CPUQuotaPeriodUSec": "infinity",
"CPUSchedulingPolicy": "0",
"CPUSchedulingPriority": "0",
……………………………………略
}
[root@ansible lamp]# ansible php -m service -a 'name=php-fpm state=restarted'
192.168.10.203 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "php-fpm",
"state": "started",
"status": {
"ActiveState": "active",
"AllowedCPUs": "",
"AllowedMemoryNodes": "",
"BlockIOAccounting": "no",
"BlockIOWeight": "[not set]",
"CPUAccounting": "no",
"CPUAffinity": "",
……………………………………………………略
}
访问IP进行验证

发现apache和php已经连通
至此,lamp分离部署就完成了
本文档详细记录了如何使用Ansible自动化工具在多台主机上进行LAMP(Linux、Apache、MySQL、PHP)环境的分离部署。首先创建Ansible主机清单,配置SSH免密登录,然后通过yum模块分别安装Apache、MySQL和PHP服务,并配置相关服务。接着,通过shell和lineinfile模块修改配置文件,确保Apache和PHP能够正确通信。最后,添加hosts解析,启动和重启服务,验证部署成功。整个过程体现了Ansible在IT基础设施自动化部署中的强大能力。
562

被折叠的 条评论
为什么被折叠?



