Ansible搭建lamp
要求:
4台主机,其中一台装ansible,其余三台分别部署httpd、mysql、php,实现lamp架构。请合理分配主机资源,所有主机均给500M内存即可,若资源富裕多给些亦可。
环境介绍:
主机名 | IP地址 | |
---|---|---|
ansible | 192.168.153.10 | 主控机 |
httpd | 192.168.153.11 | 受控机 |
mysql | 192.168.153.12 | 受控机 |
php | 192.168.153.13 | 受控机 |
准备工作
本次的yum源用的是阿里云的网络源
配置方法,可以查看这篇文章
设置三台受控机免密登录
详细步骤,可以查看这篇文章
[root@ansible ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.153.10 ansible
192.168.153.11 httpd
192.168.153.12 mysql
192.168.153.13 php
[root@ansible ~]# vim /etc/ansible/inventory
[webservers]
httpd
mysql
php
[root@ansible ~]# ssh-copy-id root@192.168.153.11
[root@ansible ~]# ssh-copy-id root@192.168.153.12
[root@ansible ~]# ssh-copy-id root@192.168.153.13
给三台受控机配置yum源
[root@ansible ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@ansible ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@ansible ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/CentOS-Base.repo
[root@ansible ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@ansible ~]# sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@ansible ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@ansible ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/epel*
[root@ansible ~]# ls /etc/yum.repos.d/
CentOS-Base.repo epel-playground.repo epel-testing-modular.repo redhat.repo
epel-modular.repo epel.repo epel-testing.repo
//使用copy模块拷贝过去
[root@ansible ~]# ansible all -m copy -a 'src=/etc/yum.repos.d/ dest=/etc/yum.repos.d/'
php | CHANGED => {
"changed": true,
"dest": "/etc/yum.repos.d/",
"src": "/etc/yum.repos.d/"
}
httpd | CHANGED => {
"changed": true,
"dest": "/etc/yum.repos.d/",
"src": "/etc/yum.repos.d/"
}
mysql | CHANGED => {
"changed": true,
"dest": "/etc/yum.repos.d/",
"src": "/etc/yum.repos.d/"
}
一丶安装httpd
安装工具包和依赖包
[root@ansible ~]# ansible httpd -m yum -a 'name=wget,bzip2,gcc,gcc-c++,make,pcre-devel,expat-devel,libxml2-devel,openssl-devel state=present'
httpd | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
下载源码包并解压
[root@ansible ~]# ansible httpd -m shell -a 'wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2' //httpd源码包
[root@ansible ~]# ansible httpd -m shell -a 'wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz' //apr源码包
[root@ansible ~]# ansible httpd -m shell -a 'wget https://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz' //apr-util源码包
//解压httpd
[root@ansible ~]# ansible httpd -m shell -a 'tar xf httpd-2.4.46.tar.bz2'
[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.
httpd | CHANGED | rc=0 >>
//解压apr
[root@ansible ~]# ansible httpd -m shell -a 'tar xf apr-1.7.0.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.
httpd | CHANGED | rc=0 >>
//解压apr-util
[root@ansible ~]# ansible httpd -m shell -a 'tar xf apr-util-1.6.1.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.
httpd | CHANGED | rc=0 >>
//再httpd受控主机上查看一下
[root@httpd ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.46.tar.bz2
apr-1.7.0 apr-util-1.6.1 httpd-2.4.46
安装apr apr-util httpd
[root@ansible ~]# ansible httpd -m shell -a "sed -i 's|$RM "$cfgfile"|#$RM "$cfgfile"|' /root/apr-1.7.0/configure"
[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.
httpd | CHANGED | rc=0 >>
[root@ansible ~]# mkdir /root/httpd
[root@ansible ~]# vim /root/httpd/apr.sh
[root@ansible ~]# ansible httpd -m script -a '/root/httpd/apr.sh'
httpd | FAILED! => {
"changed": true,
"msg": "non-zero return code",
"rc": 126,
"stderr": "Shared connection to httpd closed.\r\n",
"stderr_lines": [
"Shared connection to httpd closed."
],
"stdout": "/root/.ansible/tmp/ansible-tmp-1609960050.1274242-15793-93097666274780/apr.sh: ./configure: #: bad interpreter: No such file or directory\r\n",
"stdout_lines": [
"/root/.ansible/tmp/ansible-tmp-1609960050.1274242-15793-93097666274780/apr.sh: ./configure: #: bad interpreter: No such file or directory"
]
}
[root@ansible ~]# ^C
[root@ansible ~]# ansible httpd -m shell -a 'cd apr-1.7.0 && ./configure --prefix=/usr/local/apr'
httpd | FAILED | rc=126 >>
/bin/sh: ./configure: #: bad interpreter: No such file or directorynon-zero return code
[root@ansible ~]# ansible httpd -m script -a '/root/httpd/apr.sh'
httpd | FAILED! => {
"changed": true,
"msg": "non-zero return code",
"rc": 126,
"stderr": "Shared connection to httpd closed.\r\n",
"stderr_lines": [
"Shared connection to httpd closed."
],
"stdout": "/root/.ansible/tmp/ansible-tmp-1609960162.50652-15836-102036642804978/apr.sh: ./configure: #: bad interpreter: No such file or directory\r\n",
"stdout_lines": [
"/root/.ansible/tmp/ansible-tmp-1609960162.50652-15836-102036642804978/apr.sh: ./configure: #: bad interpreter: No such file or directory"
]
}
[root@ansible ~]# vim /root/httpd/apr-util.sh
[root@ansible ~]# ansible httpd -m script -a '/root/httpd/apr-util.sh'
httpd | FAILED! => {
"changed": true,
"msg": "non-zero return code",
"rc": 1,
"stderr": "Shared connection to httpd closed.\r\n",
"stderr_lines": [
"Shared connection to httpd closed."
],
"stdout": "checking build system type... x86_64-pc-linux-gnu\r\nchecking host system type... x86_64-pc-linux-gnu\r\nchecking target system type... x86_64-pc-linux-gnu\r\nchecking for a BSD-compatible install... /usr/bin/install -c\r\nchecking for working mkdir -p... yes\r\nAPR-util Version: 1.6.1\r\nchecking for chosen layout... apr-util\r\nchecking for gcc... gcc\r\nchecking whether the C compiler works... yes\r\nchecking for C compiler default output file name... a.out\r\nchecking for suffix of executables... \r\nchecking whether we are cross compiling... no\r\nchecking for suffix of object files... o\r\nchecking whether we are using the GNU C compiler... yes\r\nchecking whether gcc accepts -g... yes\r\nchecking for gcc option to accept ISO C89... none needed\r\nApplying apr-util hints file rules for x86_64-pc-linux-gnu\r\nchecking for APR... configure: error: the --with-apr parameter is incorrect. It must specify an install prefix, a build directory, or an apr-config file.\r\n",
"stdout_lines": [
"checking build system type... x86_64-pc-linux-gnu",
"checking host system type... x86_64-pc-linux-gnu",
"checking target system type... x86_64-pc-linux-gnu",
"checking for a BSD-compatible install... /usr/bin/install -c",
"checking for working mkdir -p... yes",
"APR-util Version: 1.6.1",
"checking for chosen layout... apr-util",
"checking for gcc... gcc",
"checking whether the C compiler works... yes",
"checking for C compiler default output file name... a.out",
"checking for suffix of executables... ",
"checking whether we are cross compiling... no",
"checking for suffix of object files... o",
"checking whether we are using the GNU C compiler... yes",
"checking whether gcc accepts -g... yes",
"checking for gcc option to accept ISO C89... none needed",
"Applying apr-util hints file rules for x86_64-pc-linux-gnu",
"checking for APR... configure: error: the --with-apr parameter is incorrect. It must specify an install prefix, a build directory, or an apr-config file."
]
}
[root@ansible ~]# vim /root/httpd/httpd.sh
[root@ansible ~]# ansible httpd -m script -a '/root/httpd/httpd.sh'
httpd | FAILED! => {
"changed": true,
"msg": "non-zero return code",
"rc": 1,
"stderr": "Shared connection to httpd closed.\r\n",
"stderr_lines": [
"Shared connection to httpd closed."
],
"stdout": "checking for chosen layout... Apache\r\nchecking for working mkdir -p... yes\r\nchecking for grep that handles long lines and -e... /usr/bin/grep\r\nchecking for egrep... /usr/bin/grep -E\r\nchecking build system type... x86_64-pc-linux-gnu\r\nchecking host system type... x86_64-pc-linux-gnu\r\nchecking target system type... x86_64-pc-linux-gnu\r\nconfigure: \r\nconfigure: Configuring Apache Portable Runtime library...\r\nconfigure: \r\nchecking for APR... configure: error: the --with-apr parameter is incorrect. It must specify an install prefix, a build directory, or an apr-config file.\r\n",
"stdout_lines": [
"checking for chosen layout... Apache",
"checking for working mkdir -p... yes",
"checking for grep that handles long lines and -e... /usr/bin/grep",
"checking for egrep... /usr/bin/grep -E",
"checking build system type... x86_64-pc-linux-gnu",
"checking host system type... x86_64-pc-linux-gnu",
"checking target system type... x86_64-pc-linux-gnu",
"configure: ",
"configure: Configuring Apache Portable Runtime library...",
"configure: ",
"checking for APR... configure: error: the --with-apr parameter is incorrect. It must specify an install prefix, a build directory, or an apr-config file."
]
}
[root@ansible ~]# ansible httpd -m shell -a 'echo "export PATH=/usr/local/apache/bin:$PATH" > /etc/profile.d/httpd.sh'
httpd | CHANGED | rc=0 >>
[root@ansible ~]# ansible httpd -m shell -a 'source /etc/profile.d/httpd.sh'
httpd | CHANGED | rc=0 >>
[root@ansible ~]# ansible httpd -m shell -a 'which apachectl'
httpd | FAILED | rc=1 >>
which: no apachectl in (/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)non-zero return code
[root@ansible ~]#
二丶安装mysql
安装依赖包
[root@ansible ~]# ansible mysql -m yum -a 'name=ncurses-devel,openssl-devel,openssl,cmake,mariadb-devel,ncurses-compat-libs state=present'
mysql | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: libcom_err-1.45.6-1.el8.x86_64",
"Installed: libcom_err-devel-1.45.6-1.el8.x86_64",
"Installed: emacs-filesystem-1:26.1-5.el8.noarch",
"Installed: pcre2-10.32-2.el8.x86_64",
"Installed: pcre2-devel-10.32-2.el8.x86_64",
"Installed: pcre2-utf16-10.32-2.el8.x86_64",
"Installed: pcre2-utf32-10.32-2.el8.x86_64",
下载包,解压配置环境变量
//下载包
[root@ansible ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@ansible ~]# scp mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz mysql:/root/
//解压
[root@ansible ~]# ansible mysql -m shell -a 'tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/'
mysql | CHANGED | rc=0 >>
[root@ansible ~]# ansible mysql -m shell -a 'cd /usr/local/ && ln -s mysql-5.7.31-linux-glibc2.12-x86_64 mysql'
mysql | CHANGED | rc=0 >>
//修改属主,组
[root@ansible ~]# ansible mysql -m shell -a 'chown -R mysql.mysql /usr/local/mysql*'
mysql | CHANGED | rc=0 >>
[root@ansible ~]# ansible mysql -m shell -a 'ls -l /usr/local/'
mysql | CHANGED | rc=0 >>
total 0
//配置环境变量
[root@ansible ~]# ansible mysql -m shell -a 'echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh'
mysql | CHANGED | rc=0 >>
[root@ansible ~]# ansible mysql -m shell -a 'source /etc/profile.d/mysql.sh'
mysql | CHANGED | rc=0 >>
//查看mysql命令
[root@ansible ~]# ansible mysql -m shell -a 'which mysql'
mysql | CHANGED | rc=0 >>
/usr/local/mysql/bin/mysql
[root@ansible ~]# ansible mysql -m shell -a 'mkdir /opt/data'
mysql | CHANGED | rc=0 >>
[root@ansible ~]# ansible mysql -m shell -a 'chown -R mysql.mysql /opt/data'
mysql | CHANGED | rc=0 >>