raw 、command、shell的区别:
修改默认清单文件指定路径
[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
192.168.159.167 ansible_user=root ansible_password=1
192.168.159.129 ansible_user=root ansible_password=1
192.168.159.134 ansible_user=root ansible_password=1
[root@ansible ansible]# ansible all --list-hosts
hosts (3):
192.168.159.167
192.168.159.129
192.168.159.134
[root@ansible ansible]#
ping模块用于检查指定节点机器是否连通,用法简单,不涉及参数,主机如果在线,则回复pong。
[root@ansible ~]# ansible all -m ping
192.168.159.129 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.159.167 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.159.134 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@ansible ~]#
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
注意:command模块有一个缺陷就是不能使用管道符和重定向功能。
//查看受控主机的/tmp目录内容
[root@ansible ~]# ansible 192.168.159.167 -a 'ls /tmp'
192.168.159.167 | CHANGED | rc=0 >>
ansible_command_payload_8o7fb6r0
systemd-private-67c9ad4e1a0d497ea429223928195df3-chronyd.service-kpcvbf
//在受控主机的/tmp目录下新建一个文件zwl
[root@ansible ~]# ansible 192.168.159.167 -a 'touch /tmp/zwl'
[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.159.167 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.159.167 -a 'ls /tmp'
192.168.159.167 | CHANGED | rc=0 >>
ansible_command_payload_l033sco1
systemd-private-67c9ad4e1a0d497ea429223928195df3-chronyd.service-kpcvbf
zwl
//command模块不支持管道符,不支持重定向
[root@ansible ~]# ansible 192.168.159.167 -a "echo 'hello world' > /tmp/zwl"
192.168.159.167 | CHANGED | rc=0 >>
hello world > /tmp/zwl
[root@ansible ~]# ansible 192.168.159.167 -a 'cat /tmp/zwl'
192.168.159.167 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.159.167 -a 'ps -ef|grep vsftpd'
192.168.159.167 | 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
[root@ansible ~]#
raw模块用于在远程主机上执行命令,其支持管道符与重定向。
//支持重定向
[root@ansible ~]# ansible 192.168.159.167 -m raw -a "echo 'hello world' > /tmp/zwl"
192.168.159.167 | CHANGED | rc=0 >>
Shared connection to 192.168.159.167 closed.
[root@ansible ~]# ansible 192.168.159.167 -a 'cat /tmp/zwl'
192.168.159.167 | CHANGED | rc=0 >>
hello world
[root@ansible ~]#
//支持管道符
[root@ansible ~]# ansible 192.168.159.167 -m raw -a 'ps -ef|grep vsftpd'
192.168.159.167 | CHANGED | rc=0 >>
root 5226 4960 0 15:46 pts/0 00:00:00 bash -c ps -ef|grep vsftpd
root 5246 5226 0 15:46 pts/0 00:00:00 grep vsftpd
Shared connection to 192.168.159.167 closed.
[root@ansible ~]#
shell模块用于在受控主机上执行受控机上的脚本,也可直接在受控主机上执行命令,shell模块支持管道重定向。
//查看受控主机上的脚本
[root@167 ~]# ls
anaconda-ks.cfg test.sh
[root@167 ~]# cat test.sh
echo "hello world!"
//使用shell模块执行受控机上的脚本
[root@ansible ~]# ansible 192.168.159.167 -m shell -a '/bin/bash /root/test.sh &> /tmp/zwl'
192.168.159.167 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.159.167 -a 'cat /tmp/zwl'
192.168.159.167 | CHANGED | rc=0 >>
hello world!
[root@ansible ~]#
script模块用于在受控机上执行主控机上的脚本
//在主控机上创建一个脚本
[root@ansible ~]# mkdir /etc/ansible/scripts
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible scripts]# vim abc.sh
[root@ansible scripts]# ll
total 4
-rw-r--r-- 1 root root 25 Oct 23 15:55 abc.sh
//执行
[root@ansible ~]# ansible 192.168.159.167 -m script -a '/etc/ansible/scripts/abc.sh &> /tmp/zwl'
192.168.159.167 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.159.167 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.159.167 closed."
],
"stdout": "",
"stdout_lines": []
}
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'cat /tmp/zwl'
192.168.159.167 | CHANGED | rc=0 >>
hello world!!!!!!
[root@ansible ~]#
template模块用于生成一个模板,并可将其传输至远程主机上。
[root@ansible ~]# ansible 192.168.159.167 -m template -a 'src=~/outfile dest=/tmp/ mode=0664'
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "59c03533730007c1c8afb34275acf48e22a71cb9",
"dest": "/tmp/outfile",
"gid": 0,
"group": "root",
"md5sum": "fe5b000e6be075da64d3468b9888809c",
"mode": "0664",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 10349,
"src": "/root/.ansible/tmp/ansible-tmp-1666512938.3616195-2892-179252729639221/source",
"state": "file",
"uid": 0
}
[root@ansible ~]#
[root@167 tmp]# ls
outfile systemd-private-67c9ad4e1a0d497ea429223928195df3-chronyd.service-kpcvbf zwl
[root@167 tmp]#
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
state常用的值:
//在受控机上查看vsftpd软件是否安装
[root@167 ~]# rpm -qa | grep vsftpd
//在ansible主机上使用yum模块在受控机上安装vsftpd
[root@ansible ~]# ansible 192.168.159.167 -m yum -a 'name=vsftpd state=present'
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: vsftpd-3.0.3-34.el8.x86_64"
]
}
[root@ansible ~]#
//查看受控机上是否安装了vsftpd
[root@167 ~]# rpm -qa | grep vsftpd
vsftpd-3.0.3-34.el8.x86_64
copy模块用于复制文件至远程受控机。
[root@ansible ~]# ansible 192.168.159.167 -m copy -a 'src=/etc/ansible/scripts/abc.sh dest=/tmp/'
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "c5162b508c6b9549138fdd64b851f7d2e49528b3",
"dest": "/tmp/abc.sh",
"gid": 0,
"group": "root",
"md5sum": "da69fea35598c29bd935992f17da8cf7",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 25,
"src": "/root/.ansible/tmp/ansible-tmp-1666513989.8076553-3073-279159375371943/source",
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'ls /tmp/'
192.168.159.167 | CHANGED | rc=0 >>
abc.sh
ansible_command_payload_bop6pl_p
ansible_dnf_payload_tdrjfe_d
outfile
systemd-private-67c9ad4e1a0d497ea429223928195df3-chronyd.service-kpcvbf
tmpdir.BKPpmj
zwl
[root@ansible ~]#
group模块用于在受控机上添加或删除组。
//在受控机上添加一个系统组,其gid为306,组名为mysql
[root@ansible ~]# ansible 192.168.159.167 -m group -a 'name=mysql gid=306 state=present'
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": false
}
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'grep mysql /etc/group'
192.168.159.167 | CHANGED | rc=0 >>
mysql:x:306:
//删除受控机上的mysql组
[root@ansible ~]# ansible 192.168.159.167 -m group -a 'name=mysql state=absent'
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'grep mysql /etc/group'
192.168.159.167 | FAILED | rc=1 >>
non-zero return code
[root@ansible ~]#
user模块用于管理受控机的用户帐号。
//在受控机上添加一个系统用户,用户名为mysql,uid为306.设置其shell为/sbin/nologin。无家目录
[root@ansible ~]# ansible 192.168.159.167 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.159.167 | 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 ~]# ansible 192.168.159.167 -m shell -a 'grep mysql /etc/passwd' 192.168.159.167 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'ls /home'
192.168.159.167 | CHANGED | rc=0 >>
admin
//修改mysql用户的uid为366
[root@ansible ~]# ansible 192.168.159.167 -m user -a 'name=mysql uid=336'
192.168.159.167 | 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 ~]# ansible 192.168.159.167 -m shell -a 'grep mysql /etc/passwd'
192.168.159.167 | CHANGED | rc=0 >>
mysql:x:336:306::/home/mysql:/sbin/nologin
[root@ansible ~]#
//删除受控机上的mysql用户
[root@ansible ~]# ansible 192.168.159.167 -m user -a 'name=mysql state=absent'
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "mysql",
"remove": false,
"state": "absent"
}
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'grep mysql /etc/passwd'
192.168.159.167 | FAILED | rc=1 >>
non-zero return code
service模块用于管理受控机上的服务。
//查看受控机上的vsftpd服务是否启动
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'systemctl is-active vsftpd'
192.168.159.167 | FAILED | rc=3 >>
inactivenon-zero return code
//启动受控机上的vsftpd服务
[root@ansible ~]# ansible 192.168.159.167 -m service -a 'name=vsftpd state=started'
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "vsftpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
......
//查看受控机上的vsftpd服务是否启动
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'systemctl is-active vsftpd'
192.168.159.167 | CHANGED | rc=0 >>
active
//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'systemctl is-enabled vsftpd'
192.168.159.167 | FAILED | rc=1 >>
disablednon-zero return code
//设置受控机上的vsftpd服务开机自动启动
[root@ansible ~]# ansible 192.168.159.167 -m service -a 'name=vsftpd enabled=yes'
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "vsftpd",
"status": {
"ActiveEnterTimestamp": "Sun 2022-10-23 16:43:42 CST",
......
//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'systemctl is-enabled vsftpd'
192.168.159.167 | CHANGED | rc=0 >>
enabled
//停止受控机上的vsftpd服务
[root@ansible ~]# ansible 192.168.159.167 -m service -a 'name=vsftpd state=stopped'
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "vsftpd",
"state": "stopped",
"status": {
"ActiveEnterTimestamp": "Sun 2022-10-23 16:43:42 CST",
......
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'systemctl is-active vsftpd'
192.168.159.167 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'ss -antl'
192.168.159.167 | 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 ~]# ansible 192.168.159.167 -m file -a "path=/tmp/test state=touch owner=root group=root"
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/tmp/test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 0,
"state": "file",
"uid": 0
}
//创建目录
[root@ansible ~]# ansible 192.168.159.167 -m file -a "path=/tmp/zzzzz state=directory mode=0755"
192.168.159.167 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/zzzzz",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
[root@ansible ~]#
//查看
[root@ansible ~]# ansible 192.168.159.167 -m shell -a 'ls -l /tmp'
192.168.159.167 | CHANGED | rc=0 >>
total 20
-rw-r--r--. 1 root root 25 Oct 23 16:33 abc.sh
drwx------. 2 root root 41 Oct 23 16:54 ansible_command_payload_5h7kbkxz
drwx------. 2 root root 37 Oct 23 16:18 ansible_dnf_payload_tdrjfe_d
-rw-rw-r--. 1 root root 10349 Oct 23 16:15 outfile
drwx------. 3 root root 17 Jul 21 08:49 systemd-private-67c9ad4e1a0d497ea429223928195df3-chronyd.service-kpcvbf
-rw-r--r--. 1 root root 0 Oct 23 16:52 test
drwx------. 2 root root 6 Oct 23 16:18 tmpdir.BKPpmj
-rw-r--r--. 1 root root 18 Oct 23 15:57 zwl
drwxr-xr-x. 2 root root 6 Oct 23 16:53 zzzzz
[root@ansible ~]#
环境说明
主机名 | IP地址 | 应用 | 系统 |
---|---|---|---|
ansible | 192.168.159.168 | ansible主控机 | centos8 |
nginx | 192.168.159.167 | nginx受控机 | centos8 |
mysql | 192.168.159.129 | mysql受控机 | centos8 |
php | 192.168.159.134 | 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]# ls
ansible.cfg hosts inventory roles scripts
[root@ansible ansible]# vim inventory
[lnmp]
nginx ansible_user=root ansible_password=1
mysql ansible_user=root ansible_password=1
php ansible_user=root ansible_password=1
[root@ansible ~]# vim /etc/hosts
192.168.159.167 nginx
192.168.159.129 mysql
192.168.159.134 php
//列出lnmp主机组
[root@ansible ~]# ansible lnmp --list-hosts
hosts (3):
nginx
mysql
php
[root@ansible ~]#
//设置密钥连接
[root@ansible ~]# ssh nginx
The authenticity of host 'nginx (192.168.159.167)' can't be established.
ECDSA key fingerprint is SHA256:Fd2APCVVg1vSL/bQVbJmpfGrlvw8N3T8SyCv8mbMyq0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'nginx' (ECDSA) to the list of known hosts.
Last login: Sun Oct 23 16:54:05 2022 from 192.168.159.168
[root@nginx ~]# exit
logout
Connection to nginx closed.
[root@ansible ~]# ssh mysql
The authenticity of host 'mysql (192.168.159.129)' can't be established.
ECDSA key fingerprint is SHA256:3dBv4SD+L+Cc2iQwm+356ftPyR8YpWNwaYmTg/r3T7M.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'mysql' (ECDSA) to the list of known hosts.
Last login: Sun Oct 23 15:34:48 2022 from 192.168.159.168
[root@mysql ~]# exit
logout
Connection to mysql closed.
[root@ansible ~]# ssh php
The authenticity of host 'php (192.168.159.134)' can't be established.
ECDSA key fingerprint is SHA256:w1jC4OTx6KkZbLbuZccFQUen4XI6ScQ+JLHjUalJHgk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'php' (ECDSA) to the list of known hosts.
Last login: Sun Oct 23 15:34:48 2022 from 192.168.159.168
[root@php ~]# exit
logout
Connection to php closed.
[root@ansible ~]#
//测试受控机连通性
[root@ansible ~]# ansible lnmp -m ping
php | 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"
}
nginx | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@ansible ~]#
//关闭防火墙和selinux
[root@ansible ~]# ansible nginx -m service -a 'name=firewalld state=stopped enabled=no'
nginx | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": false,
"name": "firewalld",
"state": "stopped",
"status": {
"ActiveEnterTimestamp": "Thu 2022-07-21 08:49:35 CST",
[root@ansible ~]# ansible nginx -a 'setenforce 0'
nginx | CHANGED | rc=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'
nginx | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 991,
"home": "/home/nginx",
"name": "nginx",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 994
}
//安装依赖包
[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
#!/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
make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@ansible scripts]# 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 ~]#
//启动服务
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible scripts]# vim 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 12
-rw-r--r-- 1 root root 25 Oct 23 15:55 abc.sh
-rw-r--r-- 1 root root 364 Oct 23 19:11 configure.sh
-rw-r--r-- 1 root root 309 Oct 23 19:18 nginx_service.sh
[root@ansible ~]# ansible nginx -m script -a '/etc/ansible/scripts/nginx_service.sh'
nginx | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to nginx closed.\r\n",
"stderr_lines": [
"Shared connection to nginx closed."
],
"stdout": "Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.\r\n",
"stdout_lines": [
"Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service."
]
}
[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 [::]:*
[root@ansible ~]#
//关闭防火墙和selinux
[root@ansible ~]# ansible mysql -m service -a 'name=firewalld state=stopped enabled=no'
[root@ansible ~]# ansible mysql -a 'setenforce 0'
mysql | CHANGED | rc=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'
mysql | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 992,
"home": "/home/mysql",
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 995
}
//安装依赖包
[root@ansible ~]# ansible mysql -m yum -a 'name=ncurses-devel,openssl-devel,openssl,wget,vim,cmake,mariadb-devel,ncurses-compat-libs 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'
mysql | CHANGED | rc=0 >>
//修改属主属组
[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"
mysql | CHANGED | rc=0 >>
[root@ansible ~]# ansible mysql -a 'which mysql'
mysql | CHANGED | rc=0 >>
/usr/local/mysql/bin/mysql
//建立数据存放目录
[root@ansible ~]# ansible mysql -a 'mkdir -p /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-23T11:56:48.283323Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-23T11:56:48.443010Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-23T11:56:48.467325Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-23T11:56:48.523231Z 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: c62f1652-52c9-11ed-a376-000c29181680.
2022-10-23T11:56:48.523972Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-23T11:56:48.713380Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-23T11:56:48.713409Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-23T11:56:48.713714Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-23T11:56:48.755940Z 1 [Note] A temporary password is generated for root@localhost: cNcXMOkyf1/v
[root@ansible ~]# ansible mysql -m shell -a "echo 'cNcXMOkyf1/v' > pass"
//生成配置文件启动服务
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible scripts]# vim 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 16
-rw-r--r-- 1 root root 25 Oct 23 15:55 abc.sh
-rw-r--r-- 1 root root 364 Oct 23 19:11 configure.sh
-rw-r--r-- 1 root root 758 Oct 23 19:59 mysql_service.sh
-rw-r--r-- 1 root root 363 Oct 23 19:19 nginx_service.sh
[root@ansible ~]# ansible mysql -m script -a '/etc/ansible/scripts/mysql_service.sh'
mysql | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to mysql closed.\r\n",
"stderr_lines": [
"Shared connection to mysql closed."
],
"stdout": "Synchronizing state of mysqld.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.\r\nExecuting: /usr/lib/systemd/systemd-sysv-install enable mysqld\r\nCreated symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.\r\n",
"stdout_lines": [
"Synchronizing state of mysqld.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.",
"Executing: /usr/lib/systemd/systemd-sysv-install enable mysqld",
"Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service."
]
}
[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 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@ansible ~]#
//关闭防火墙和selinux
[root@ansible ~]# ansible php -m service -a 'name=firewalld state=stopped enabled=no'
[root@ansible ~]# ansible php -a 'setenforce 0'
php | CHANGED | rc=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 php.sh
#!/bin/bash
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 openldap-devel 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-devel-6.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/oniguruma-devel-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 --enable-calendar --with-curl --enable-exif --enable-ftp --enable-gd --with-jpeg --with-zlib-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-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posix && \
make && make install
[root@ansible scripts]# ansible php -m script -a '/etc/ansible/scripts/php.sh'
//配置环境变量
[root@ansible ~]# ansible php -m shell -a "echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh"
//配置启动
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible scripts]# vim php_service.sh
#!/bin/bash
cd php-8.1.11
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/php-fpm.d/www.conf
cat > /usr/lib/systemd/system/php.service <<EOF
[Unit]
Description=php server daemon
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now php
[root@ansible scripts]# ansible php -m script -a '/etc/ansible/scripts/php_service.sh'
php | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to php closed.\r\n",
"stderr_lines": [
"Shared connection to php closed."
],
"stdout": "",
"stdout_lines": []
}
[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 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
nginx受控机配置
//修改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.159.134: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 ~]#
//检查语法
[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 ~]#
//重启服务
[root@ansible ~]# ansible nginx -m service -a 'name=nginx state=restarted'
nginx | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "nginx",
"state": "started",
"status": {
"ActiveEnterTimestamp": "Sun 2022-10-23 19:19:56 CST",
[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 [::]:*
[root@ansible ~]#
//创建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 >>
[root@ansible ~]#
php受控机配置
//监听php,运行nginx访问
[root@ansible ~]# ansible php -m shell -a "echo 'listen = 192.168.159.134:9000' >> /usr/local/php8/etc/php-fpm.d/www.conf"
php | CHANGED | rc=0 >>
[root@ansible ~]# ansible php -m shell -a "echo ';listen.allowed_clients = 192.168.159.167' >> /usr/local/php8/etc/php-fpm.d/www.conf"
php | CHANGED | rc=0 >>
[root@ansible ~]#
//创建index.php文件
[root@ansible ~]# ansible php -a 'mkdir /var/www'
[root@ansible ~]# ansible php -m shell -a "echo ' /var/www/index.php"
[root@ansible ~]# ansible php -m shell -a "echo ' phpinfo();' >> /var/www/index.php"
[root@ansible ~]# ansible php -m shell -a "echo '?>' >> /var/www/index.php"
[root@ansible ~]#
//重启服务
[root@ansible ~]# ansible php -m service -a 'name=php state=restarted'
php | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "php",
"state": "started",
"status": {
"ActiveEnterTimestamp": "Sun 2022-10-23 20:35:49 CST",
[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.159.134:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@ansible ~]#