ansible常用模块有:
ansible常用模块raw
、command
、shell
的区别:
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@ansible project]# ansible all -m ping
192.168.174.177 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能。
//查看受控主机的/tmp目录内容
[root@ansible project]# ansible 192.168.174.177 -a 'ls /tmp'
192.168.174.177 | CHANGED | rc=0 >>
ansible_command_payload_5itl_888
systemd-private-9dc9302a955a4cdf801d5d766e86cfa5-chronyd.service-eNSaQg
vmware-root_873-4013854327
vmware-root_881-4013198953
vmware-root_886-2697008560
vmware-root_887-4013330030
vmware-root_896-2730693439
//在受控主机的/tmp目录下新建一个文件test
[root@ansible project]# ansible 192.168.174.177 -a 'touch /tmp/test'
192.168.174.177 | CHANGED | rc=0 >>
[root@ansible project]# ansible 192.168.174.177 -a 'ls /tmp'
192.168.174.177 | CHANGED | rc=0 >>
ansible_command_payload_yc5jv44p
systemd-private-9dc9302a955a4cdf801d5d766e86cfa5-chronyd.service-eNSaQg
test
vmware-root_873-4013854327
vmware-root_881-4013198953
vmware-root_886-2697008560
vmware-root_887-4013330030
vmware-root_896-2730693439
//command模块不支持管道符,不支持重定向
[root@ansible project]# ansible 192.168.174.177 -a "echo 'hello world' > /tmp/test"
192.168.174.177 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@ansible project]# ansible 192.168.174.177 -a 'cat /tmp/test'
192.168.174.177 | CHANGED | rc=0 >>
[root@ansible project]# ansible 192.168.174.177 -a 'ps -ef|grep vsftpd'
192.168.174.177 | FAILED | rc=1 >>
error: unsupported SysV option
Usage:
ps [options]
Try 'ps --help '
or 'ps --help '
for additional help text.
For more details see ps(1).non-zero return code
raw模块用于在远程主机上执行命令,其支持管道符与重定向
//支持重定向
[root@ansible project]# ansible 192.168.174.177 -m raw -a "echo 'hello world' > /tmp/test"
192.168.174.177 | CHANGED | rc=0 >>
Shared connection to 192.168.174.177 closed.
[root@ansible project]# ansible 192.168.174.177 -a 'cat /tmp/test'
192.168.174.177 | CHANGED | rc=0 >>
hello world
//支持管道符
root@ansible project]# ansible 192.168.174.177 -m raw -a 'cat /tmp/test|grep -Eo hello'
192.168.174.177 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.174.177 closed.
root@ansible project]# ansible 192.168.174.177 -m raw -a 'ps -ef|grep vsftpd'
192.168.174.177 | CHANGED | rc=0 >>
root 2712 2569 0 03:29 pts/1 00:00:00 bash -c ps -ef|grep vsftpd
root 2732 2712 0 03:29 pts/1 00:00:00 grep vsftpd
Shared connection to 192.168.174.177 closed.
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
//查看受控机上的脚本
root@node3 ~]# ll /scripts/
total 4
-rw-r--r-- 1 root root 30 Oct 20 22:21 test.sh
//使用shell模块在受控机上执行受控机上的脚本
[root@ansible ~]# ansible 192.168.174.177 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
192.168.174.177 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.174.177 -m shell -a 'cat /tmp/test'
192.168.174.177 | CHANGED | rc=0 >>
/scripts/test.sh: line 1: !/bin/bash: No such file or directory
hello world
script模块用于在受控机上执行主控机上的脚本
[root@ansible project]# cat scripts/test1.sh
#!/bin/bash
echo "hello world"
[root@ansible project]# ansible 192.168.174.177 -m script -a '/opt/project/scripts/test1.sh &> /tmp/a'
192.168.174.177 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.174.177 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.174.177 closed."
],
"stdout": "",
"stdout_lines": []
}
//查看受控机上的/tmp/a文件内容
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'cat /tmp/a'
192.168.174.177 | CHANGED | rc=0 >>
hello world
template模块用于生成一个模板,并可将其传输至远程主机上。
//下载一个163的yum源文件并开启此源
[root@ansible ~]# cd /etc/yum.repos.d/
[root@ansible yum.repos.d]# curl -o CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1572 100 1572 0 0 10076 0 --:--:-- --:--:-- --:--:-- 10076
[root@ansible yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@ansible yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
//将设置好的163源传到受控主机
[root@ansible project]# ansible 192.168.174.177 -m template -a 'src=/etc/yum.repos.d/CentOS7-Base-163.repo dest=/etc/yum.repos.d/163.repo'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "60b8868e0599489038710c45025fc11cbccf35f2",
"dest": "/etc/yum.repos.d/163.repo",
"gid": 0,
"group": "root",
"md5sum": "5a3e688854d9ceccf327b953dab55b21",
"mode": "0644",
"owner": "root",
"size": 1462,
"src": "/root/.ansible/tmp/ansible-tmp-1666511654.0177784-2621-71980718987537/source",
"state": "file",
"uid": 0
}
//查看受控机上是否有163源
[root@node3 ~]# ls /etc/yum.repos.d/
163.repo CentOS-Base.repo
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
state常用的值:
若想使用yum来管理软件,请确保受控机上的yum源无异常。
//在受控机上查询看httpd软件是否安装
[root@node3 ~]# rpm -qa|grep httpd
[root@node3 ~]#
//在ansible主机上使用yum模块在受控机上安装httpd
[root@ansible project]# ansible 192.168.174.177 -m yum -a 'name=httpd state=present'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: mailcap-2.1.48-3.el8.noarch",
"Installed: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
"Installed: apr-1.6.3-12.el8.x86_64",
"Installed: httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch",
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
"Installed: httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
"Installed: centos-logos-httpd-85.8-2.el8.noarch",
"Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64"
]
}
//查看受控机上是否安装了httpd
[root@node3 ~]# rpm -qa|grep httpd
centos-logos-httpd-85.8-2.el8.noarch
httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch
httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
copy模块用于复制文件至远程受控机。
[root@ansible ~]# ls /etc/ansible/scripts/
a.sh
[root@ansible ~]# ansible 172.16.103.129 -m copy -a 'src=/etc/ansible/scripts/a.sh dest=/scripts/'
172.16.103.129 | SUCCESS => {
"changed": true,
"checksum": "83f66f804c195247885b013912cf9dc649f36391",
"dest": "/scripts/a.sh",
"gid": 0,
"group": "root",
"md5sum": "a63e880a932bba1160f329836cbfd730",
"mode": "0644",
"owner": "root",
"size": 61,
"src": "/root/.ansible/tmp/ansible-tmp-1536406467.26-35192956264311/source",
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'ls /scripts/'
172.16.103.129 | SUCCESS | rc=0 >>
a.sh
test.sh
group模块用于在受控机上添加或删除组。
//在受控机上添加一个系统组,其gid为306,组名为mysql
[root@ansible project]# ansible 192.168.174.177 -m group -a 'name=mysql gid=306 state=present'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": false
}
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'grep mysql /etc/group'
192.168.174.177 | CHANGED | rc=0 >>
mysql:x:306:
//删除受控机上的mysql组
[root@ansible project]# ansible 192.168.174.177 -m group -a 'name=mysql state=absent'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'grep mysql /etc/group'
192.168.174.177 | FAILED | rc=1 >>
non-zero return code
user模块用于管理受控机的用户帐号。
//在受控机上添加一个系统用户,用户名为mysql,uid为306,设置其shell为/sbin/nologin,无家目录
[root@ansible project]# ansible 192.168.174.177 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 306,
"home": "/home/mysql",
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 306
}
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'grep mysql /etc/passwd'
192.168.174.177 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'ls /home'
192.168.174.177 | CHANGED | rc=0 >>
runtime
//修改mysql用户的uid为366
[root@ansible project]# ansible 192.168.174.177 -m user -a 'name=mysql uid=336'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"append": false,
"changed": true,
"comment": "",
"group": 306,
"home": "/home/mysql",
"move_home": false,
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"uid": 336
}
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'grep mysql /etc/passwd'
192.168.174.177 | CHANGED | rc=0 >>
mysql:x:336:306::/home/mysql:/sbin/nologin
//删除受控机上的mysql用户
[root@ansible project]# ansible 192.168.174.177 -m user -a 'name=mysql state=absent'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "mysql",
"remove": false,
"state": "absent"
}
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'grep mysql /etc/passwd'
192.168.174.177 | FAILED | rc=1 >>
non-zero return code
service模块用于管理受控机上的服务。
//查看受控机上的httpd服务是否启动
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'systemctl is-active httpd'
192.168.174.177 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible project]# ansible 192.168.174.177 -m service -a 'name=httpd state=started'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
....此处省略N行
}
//查看受控机上的httpd服务是否启动
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'systemctl is-active httpd'
192.168.174.177 | CHANGED | rc=0 >>
active
//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'systemctl is-enabled httpd'
192.168.174.177 | FAILED | rc=1 >>
disablednon-zero return code
//设置受控机上的httpd服务开机自动启动
[root@ansible project]# ansible 192.168.174.177 -m service -a 'name=httpd enabled=yes'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"status": {
"ActiveEnterTimestamp": "Sun 2022-10-23 04:35:19 EDT",
"ActiveEnterTimestampMonotonic": "590489245",
....此处省略N行
}
//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'systemctl is-enabled httpd'
192.168.174.177 | CHANGED | rc=0 >>
enabled
//停止受控机上的httpd服务
[root@ansible project]# ansible 192.168.174.177 -m service -a 'name=httpd state=stopped'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "stopped",
"status": {
....此处省略N行
}
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'systemctl is-active httpd'
192.168.174.177 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'ss -anlt'
192.168.174.177 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
专门用来设定文件属性。
force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软
链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group:定义文件/目录的属组
mode:定义文件/目录的权限
owner:定义文件/目录的属主
path:必选项,定义文件/目录的路径
recurse:递归的设置文件的属性,只对目录有效
src:要被链接的源文件的路径,只应用于state=link的情况
dest:被链接到的路径,只应用于state=link的情况
state:
=directory:如果目录不存在,创建目录
=file:即使文件不存在,也不会被创建
=link:创建软链接
=hard:创建硬链接
=touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
=absent:删除目录、文件或者取消链接文件
//创建文件
[root@ansible project]# ansible 192.168.174.177 -m file -a 'path=/tmp/test state=touch owner=root group=root'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/tmp/test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
//创建目录
[root@ansible project]# ansible 192.168.174.177 -m file -a 'path=/tmp/www state=touch state=directory mode=0775'
192.168.174.177 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0775",
"owner": "root",
"path": "/tmp/www",
"size": 6,
"state": "directory",
"uid": 0
}
//查看
[root@ansible project]# ansible 192.168.174.177 -m shell -a 'ls -l /tmp'
192.168.174.177 | CHANGED | rc=0 >>
total 0
drwx------ 2 root root 41 Oct 23 08:22 ansible_command_payload_f54o0rn_
drwx------ 3 root root 17 Oct 23 04:25 systemd-private-dbef845a0d7943caab6a91b846a53215-chronyd.service-4foT0e
-rw-r--r-- 1 root root 0 Oct 23 08:20 test
drwx------ 2 root root 6 Oct 23 04:25 vmware-root_883-4021653483
drwxrwxr-x 2 root root 6 Oct 23 08:22 www
环境说明
主机名 | IP地址 | 应用 | 系统 |
---|---|---|---|
ansible | 192.168.174.168 | ansible主控机 | centos8 |
nginx | 192.168.174.170 | nginx受控机 | centos8 |
mysql | 192.168.174.175 | mysql受控机 | centos8 |
php | 192.168.174.177 | php受控机 | centos8 |
修改默认清单文件位置,构建清单
[root@ansible ~]# vim /etc/ansible/ansible.cfg
inventory = /etc/ansible/inventory
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# touch inventory
[root@ansible ansible]# vim inventory
[lnmp]
nginx ansible_user=root ansible_password=cobbler123
mysql ansible_user=root ansible_password=cobbler123
php ansible_user=root ansible_password=cobbler123
[root@ansible ansible]# vim /etc/hosts
192.168.174.170 nginx
192.168.174.175 mysql
192.168.174.177 php
//列出lnmp主机组
[root@ansible ~]# ansible lnmp --list-hosts
hosts (3):
nginx
mysql
php
//测试受控机连通性
[root@ansible ~]# ansible lnmp -m ping
nginx | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
mysql | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
php | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
//关闭防火墙和selinux
[root@ansible ~]# ansible nginx -m service -a 'name=firewalld state=stopped
enabled=no'
[root@ansible ~]# ansible nginx -a 'setenforce 0'
[root@ansible ~]# ansible nginx -a "sed -ri 's/^(SELINUX=).*/\1disabled/g'
/etc/selinux/config"
//创建用户
[root@ansible ~]# ansible nginx -m user -a 'name=nginx system=yes create_home=no shell=/sbin/nologin state=present'
//安装依赖包
[root@ansible ~]# ansible nginx -m yum -a 'name=pcre-devel,openssl,openssl-devel,gd-devel,gcc,gcc-c++,make,wget,vim state=present'
//下载软件包并解压
[root@ansible ~]# ansible nginx -a 'wget http://nginx.org/download/nginx-1.20.2.tar.gz'
[root@ansible ~]# ansible nginx -a 'tar -xf nginx-1.20.2.tar.gz'
//进入目录编译安装
[root@ansible ~]# mkdir -p /etc/ansible/scripts/
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible scripts]# vim configure.sh
[root@ansible scripts]# cat configure.sh
#!/bin/bash
cd nginx-1.20.2
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log && \
make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@ansible scripts]# ll
total 4
-rw-r--r--. 1 root root 457 Oct 23 22:03 configure.sh
[root@ansible ~]# ansible nginx -m script -a '/etc/ansible/scripts/configure.sh'
//安装完成
[root@ansible ~]# ansible nginx -a 'ls /usr/local/nginx'
nginx | CHANGED | rc=0 >>
conf
html
logs
sbin
//配置环境变量
[root@ansible ~]# ansible nginx -m shell -a 'echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh'
nginx | CHANGED | rc=0 >>
[root@ansible ~]# ansible nginx -a 'which nginx'
nginx | CHANGED | rc=0 >>
/usr/local/nginx/sbin/nginx
//启动服务
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible scripts]# vim nginx_service.sh
[root@ansible scripts]# cat nginx_service.sh
#!/bin/bash
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx
[root@ansible scripts]# ll
total 8
-rw-r--r--. 1 root root 457 Oct 23 22:03 configure.sh
-rw-r--r--. 1 root root 364 Oct 23 22:11 nginx_service.sh
[root@ansible ~]# ansible nginx -m script -a '/etc/ansible/scripts/nginx_service.sh'
[root@ansible ~]# ansible nginx -a 'ss -antl'
nginx | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//关闭防火墙和selinux
[root@ansible ~]# ansible mysql -m service -a 'name=firewalld state=stopped
enabled=no‘
[root@ansible ~]# ansible mysql -a 'setenforce 0'
[root@ansible ~]# ansible mysql -a "sed -ri 's/^(SELINUX=).*/\1disabled/g'
/etc/selinux/config"
//创建用户
[root@ansible ~]# ansible mysql -m user -a 'name=mysql system=yes create_home=no shell=/sbin/nologin state=present'
//安装依赖包
[root@ansible ~]# ansible mysql -m yum -a 'name=ncurses-devel,openssl-devel,openssl,cmake,mariadb-devel,ncurses-compat-libs,wget state=present'
//下载软件包,解压并重名名
[root@ansible ~]# ansible mysql -a 'wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz'
[root@ansible ~]# ansible mysql -a 'tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/'
[root@ansible ~]# ansible mysql -a 'mv /usr/local/mysql-5.7.38-linux-glibc2.12-x86_64 /usr/local/mysql'
//修改属主属组
[root@ansible ~]# ansible mysql -a 'chown -R mysql.mysql /usr/local/mysql'
//配置include、man及环境变量
[root@ansible ~]# ansible mysql -a 'ln -s /usr/local/mysql/include /usr/include/mysql'
[root@ansible ~]# ansible mysql -m shell -a 'echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf'
[root@ansible ~]# ansible mysql -a "sed -i '22a MANDATORY_MANPATH /usr/local/mysql/man' /etc/man_db.conf"
[root@ansible ~]# ansible mysql -m shell -a "echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh"
[root@ansible ~]# ansible mysql -a 'which mysql'
mysql | CHANGED | rc=0 >>
/usr/local/mysql/bin/mysql
//建立数据存放目录
[root@ansible ~]# ansible mysql -a 'mkdir /opt/data'
[root@ansible ~]# ansible mysql -a 'chown -R mysql.mysql /opt/data'
//初始化数据库
[root@ansible ~]# ansible mysql -a 'mysqld --initialize --user mysql --datadir /opt/data'
mysql | CHANGED | rc=0 >>
2022-10-23T14:37:04.250324Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-23T14:37:14.685245Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-23T14:37:17.531360Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-23T14:37:17.661687Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 31990363-52e0-11ed-9d5d-000c296ac4f8.
2022-10-23T14:37:17.662267Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-23T14:37:17.849159Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-23T14:37:17.849171Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-23T14:37:17.849515Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-23T14:37:17.904203Z 1 [Note] A temporary password is generated for root@localhost: 7ZPKeIyptl#_
[root@ansible ~]# ansible mysql -m shell -a "echo '7ZPKeIyptl#_' > pass"
//生成配置文件启动服务
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible scripts]# vim /etc/ansible/scripts/mysql_service.sh
[root@ansible scripts]# cat /etc/ansible/scripts/mysql_service.sh
#!/bin/bash
cat >> /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysqld server daemon
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now mysqld
[root@ansible scripts]# ll
total 12
-rw-r--r--. 1 root root 457 Oct 23 22:03 configure.sh
-rw-r--r--. 1 root root 759 Oct 23 22:40 mysql_service.sh
-rw-r--r--. 1 root root 364 Oct 23 22:11 nginx_service.sh
[root@ansible ~]# ansible mysql -m script -a '/etc/ansible/scripts/mysql_service.sh'
[root@ansible ~]# ansible mysql -a 'ss -antl'
mysql | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
//关闭防火墙和selinux
[root@ansible ~]# ansible php -m service -a 'name=firewalld state=stopped
enabled=no'
[root@ansible ~]# ansible php -a 'setenforce 0'
[root@ansible ~]# ansible php -a "sed -ri 's/^(SELINUX=).*/\1disabled/g'
/etc/selinux/config"
//配置脚本启动php
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible scripts]# vim /etc/ansible/scripts/php.sh
[root@ansible scripts]# cat /etc/ansible/scripts/php.sh
#!/bin/bash
#配置yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo
https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo && \
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
#安装依赖包
yum -y install epel-release && \
yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ wget make ncurses-devel openssl cmake libxm12 libxm12-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldapdevel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel php-mysqlnd libsqlite3x-devel libzip-devel https://dl.rockylinux.org/pub/rocky/9/CRB/x86_64/os/Packages/o/oniguruma-devel6.9.6-1.el9.5.x86_64.rpm http://mirror.stream.centos.org/9-stream/CRB/x86_64/os/Packages/libzip-devel-1.7.3-7.el9.x86_64.rpm http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/onigurumadevel-6.8.2-2.el8.x86_64.rpm--allowerasing --skip-broken --nobest
#下载软件包解压编译安装
wget https://www.php.net/distributions/php-8.1.11.tar.gz && \
tar xf php-8.1.11.tar.gz && \
cd php-8.1.11./configure --prefix=/usr/local/php8 --with-config-file-path=/etc --enable-fpm --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enablecalendar --with-curl --enable-exif --enable-ftp --enable-gd --with-jpeg --withzlib-dir --with-freetype --with-gettext --enable-json --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --with-zip --enable-mysqlndcompression-support --with-pear --enable-pcntl --enable-posix && \
make && make install
#配置环境变量
echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
#配置启动
cp php.ini-production /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/phpfpm.d/www.conf
cat > /usr/lib/systemd/system/php.service </etc/ansible/scripts/php.sh' //编译时间稍长,等待即可
[root@ansible scripts]# ansible php -a 'ss -antl'
php | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
nginx受控机配置
[root@ansible ~]# ansible nginx -a "sed -i '45 s/index index.html index.htm;/index index.php index.html index.htm;/g' /usr/local/nginx/conf/nginx.conf"
[root@ansible ~]# ansible nginx -a "sed -i '65,71 s/#/ /' /usr/local/nginx/conf/nginx.conf"
[root@ansible ~]# ansible nginx -a "sed -i '67 s/fastcgi_pass 127.0.0.1:9000;/fastcgi_pass 192.168.174.177:9000;/g' /usr/local/nginx/conf/nginx.conf"
[root@ansible ~]# ansible nginx -a "sed -i '69 s/\/scripts/\/var\/www/' /usr/local/nginx/conf/nginx.conf"
//检查语法
[root@ansible ~]# ansible nginx -a 'nginx -t'
nginx | CHANGED | rc=0 >>
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
//重启服务
[root@ansible ~]# ansible nginx -m service -a 'name=nginx state=restarted'
//创建index.php文件
[root@ansible ~]# ansible nginx -m shell -a "echo ' /usr/local/nginx/html/index.php"
nginx | CHANGED | rc=0 >>
[root@ansible ~]# ansible nginx -m shell -a "echo ' phpinfo();' >> /usr/local/nginx/html/index.php"
nginx | CHANGED | rc=0 >>
[root@ansible ~]# ansible nginx -m shell -a "echo '?>' >> /usr/local/nginx/html/index.php"
nginx | CHANGED | rc=0 >>
php受控机配置
//监听php,运行nginx访问
[root@ansible ~]# ansible php -m shell -a "echo 'listen = 192.168.174.177:9000' >> /usr/local/php/etc/php-fpm.d/www.conf"
[root@ansible ~]# ansible php -m shell -a "echo ';listen.allowed_clients = 192.168.174.168' >> /usr/local/php/etc/php-fpm.d/www.conf"
//创建index.php文件
[root@ansible ~]# ansible php -a 'mkdir /var/www'
[root@ansible ~]# ansible php -m shell -a "echo ' /var/www/index.php"
php | CHANGED | rc=0 >>
[root@ansible ~]# ansible php -m shell -a "echo ' phpinfo();' >>
/var/www/index.php"
php | CHANGED | rc=0 >>
[root@ansible ~]# ansible php -m shell -a "echo '?>' >> /var/www/index.php"
php | CHANGED | rc=0 >>
//重启服务
[root@ansible ~]# ansible php -m service -a 'name=php state=restarted'
[root@ansible ~]# ansible php -a 'ss -antl'
php | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 192.168.183.137:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
ble php -m shell -a “echo ‘;listen.allowed_clients = 192.168.174.168’ >> /usr/local/php/etc/php-fpm.d/www.conf”
//创建index.php文件
[root@ansible ~]# ansible php -a ‘mkdir /var/www’
[root@ansible ~]# ansible php -m shell -a “echo ‘ /var/www/index.php”
php | CHANGED | rc=0 >>
[root@ansible ~]# ansible php -m shell -a “echo ’ phpinfo();’ >>
/var/www/index.php”
php | CHANGED | rc=0 >>
[root@ansible ~]# ansible php -m shell -a “echo ‘?>’ >> /var/www/index.php”
php | CHANGED | rc=0 >>
//重启服务
[root@ansible ~]# ansible php -m service -a ‘name=php state=restarted’
[root@ansible ~]# ansible php -a ‘ss -antl’
php | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 192.168.183.137:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]
### 6、访问php测试页面
![](https://img-blog.csdnimg.cn/img_convert/73f98ab418eb6f7e1a91b427ee46dd42.png)