作用:可以实现批量管理主机信息
PS:command模块有缺陷,无法识别特殊符号信息:"$HOME" "<" ">" "|" ";" "&"
模块常用指令参数:
creates: 判断一个文件是否存在,如果存在就不执行相应命令
removers:判断一个文件是否存在,如果存在就执行相应命令
chdir: 执行命令前,进行目录一个切换
PS:
(1)万能模块在使用时缺陷,不具有幂等性(第一次执行结果和多次执行结果相同)
当不具有幂等性时,会对剧本编写产生问题???
(2)操作步骤可能会过于繁琐
批量执行脚本功能:
第一个历程:编写脚本
[root@ansible project]# vim test.sh
[root@ansible project]# cat test.sh
#!/bin/bash
echo "hello world"
[root@ansible project]# chmod +x test.sh
[root@ansible project]# ./test.sh
hello world
第二个历程:需要将脚本文件分发给所有主机
[root@ansible project]# ansible webservers -m copy -a "src=/opt/project/test.sh dest=/opt/project"
192.168.137.135 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"checksum": "6aa73e56214888452816de465fa1c8329ac9e119",
"dest": "/opt/project",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/opt/project",
"size": 85,
"state": "file",
"uid": 0
}
192.168.137.136 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"checksum": "6aa73e56214888452816de465fa1c8329ac9e119",
"dest": "/opt/project",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/opt/project",
"size": 85,
"state": "file",
"uid": 0
}
第三个历程:设置脚本文件权限
[root@ansible project]# ansible webservers -m file -a "path=/opt/project mode=755"
192.168.137.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/opt/project",
"size": 85,
"state": "file",
"uid": 0
}
192.168.137.135 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/opt/project",
"size": 85,
"state": "file",
"uid": 0
}
第四个历程:执行脚本文件
[root@ansible project]# ansible webservers -m shell -a "/opt/project"
192.168.137.135 | CHANGED | rc=0 >>
hello world
192.168.137.136 | CHANGED | rc=0 >>
hello world
作用:专门批量执行脚本功能
第一个历程:编写脚本
第二个历程:批量执行脚本
[root@ansible project]# ansible webservers -m script -a "/opt/project/test.sh"
192.168.137.135 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.137.135 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.137.135 closed."
],
"stdout": "hello world\r\n",
"stdout_lines": [
"hello world"
]
}
192.168.137.136 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.137.136 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.137.136 closed."
],
"stdout": "hello world\r\n",
"stdout_lines": [
"hello world"
]
}
作用:可以实现批量分发数据文件
常用参数指令:
src: 指定要分发文件数据信息
dest: 将分发文件保存到哪个目标路径
owner: 指定文件分发后的属主信息
group: 指定文件分发后的属组信息
mode: 指定文件分发后的权限信息
backup: 当文件名称重复时,会将原文件备份,再进行覆盖
content: 在被管理主机上创建文件并设置信息内容
directory_mode: 可以递归设置目录中数据权限(只是针对复制目录时)
force:(了解) 可以避免相同名称文件覆盖
force=yes 默认设置 表示会覆盖
force=no 不会对相同文件进行覆盖
remote_src: 将被管理主机上文件进行本地备份保存
validate: 验证分发后的文件合法性
123456789101112131415
实践操作:
(1)批量分发文件信息,并修改文件属主 属组 和权限信息
[root@ansible project]# ansible webservers -m copy -a "src=/opt/project/abc.txt dest=/tmp/ mode=666 owner=tom group=tom"
192.168.137.135 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "1a82a29241cefb4e3e6599a809e234e580101003",
"dest": "/tmp/abc.txt",
"gid": 1001,
"group": "tom",
"mode": "0666",
"owner": "tom",
"path": "/tmp/abc.txt",
"size": 14,
"state": "file",
"uid": 1001
}
192.168.137.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "1a82a29241cefb4e3e6599a809e234e580101003",
"dest": "/tmp/abc.txt",
"gid": 1001,
"group": "tom",
"mode": "0666",
"owner": "tom",
"path": "/tmp/abc.txt",
"size": 14,
"state": "file",
"uid": 1001
}
2)在被管理主机上创建文件信息,并设置文件的内容
[root@ansible ~]# ansible webservers -m copy -a "content=hello_dog dest=/tmp/abc.txt"
192.168.137.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "7f3d38f956a244629bac0f6c5ef9b63d677c20aa",
"dest": "/tmp/abc.txt",
"gid": 1001,
"group": "tom",
"md5sum": "b168d7d7ec5af35034436a2f0aa34ec7",
"mode": "0666",
"owner": "tom",
"size": 9,
"src": "/root/.ansible/tmp/ansible-tmp-1666505224.513224-2159-172028776136002/source",
"state": "file",
"uid": 1001
}
·······
[root@ansible ~]# ansible webservers -m shell -a " cat /tmp/abc.txt "
192.168.137.136 | CHANGED | rc=0 >>
hello_dog
192.168.137.145 | CHANGED | rc=0 >>
hello_dog
192.168.137.135 | CHANGED | rc=0 >>
hello_dog
(3)将目录信息复制到被管理主机上,并且将目录以及下面所有内容权限统一改为777
1
利用copy模块分发目录信息时:
目录后面有 / 表示将目录下面数据内容进行整体分发
目录后面没有 / 表示将目录下面数据内容以及目录本身都做分发
ansible oldboy -m copy -a "src=/data/oldboy01/ dest=/tmp/oldboy/oldboy01/"
1
(4)将备份的数据进行还原
ansible oldboy -m copy -a "src=/tmp/oldboy.txt dest=/backup/oldboy.txt.bak01 remote_src=yes" 批量备份数据文件
ansible oldboy -m copy -a "src=backup/oldboy.txt.bak01 dest=/tmp/oldboy.txt remote_src=yes" 批量备份数据文件
ansible oldboy -m copy -a "src=/data/oldboy.txt dest=/tmp/oldboy.txt force=no
作用:可以将被管理主机数据进行拉取
常用指令参数:
src: 被管理主机需要拉取数据目录
dest: 管理主机需要存储数据目录信息
实践操作:实现被管理主机文件数据拉取
[root@ansible ~]# ansible webservers -m fetch -a "src=/tmp/abc.txt dest=/data/"
192.168.137.135 | SUCCESS => {
"changed": false,
"checksum": "7f3d38f956a244629bac0f6c5ef9b63d677c20aa",
"dest": "/data/192.168.137.135/tmp/abc.txt",
"file": "/tmp/abc.txt",
"md5sum": "b168d7d7ec5af35034436a2f0aa34ec7"
}
······
[root@ansible ~]# ll /data/
total 0
drwxr-xr-x 3 root root 17 Oct 23 14:10 192.168.137.135
drwxr-xr-x 3 root root 17 Oct 23 14:10 192.168.137.136
drwxr-xr-x 3 root root 17 Oct 23 14:10 192.168.137.145
[root@ansible ~]# tree /data/
/data/
├── 192.168.137.135
│ └── tmp
│ └── abc.txt
├── 192.168.137.136
│ └── tmp
│ └── abc.txt
└── 192.168.137.145
└── tmp
└── abc.txt
6 directories, 3 files
PS:fetch文件默认无法拉取目录信息,可以利用synchronize
(也可以先利用压缩模块压缩处理后再拉取)
作用:可以实现数据信息创建与删除/以及已有数据信息属性修改(属主 属组 权限)
常用指令参数:
owner: 修改已有文件的属主信息
group: 修改已有文件的属组信息
mode: 修改已有文件的权限信息
path: 指定需要创建或者修改属性文件信息
recurse: 对指定目录下面数据内容做递归修改,默认为no
state: 可以实现数据信息创建删除
实践需求:
01.修改被管理文件数据属性信息
ansible webservers -m file -a "path=/tmp/abc.txt mode=644 owner=root group=root"
---对文件信息修改
ansible 192.168.137.135 -m file -a "path=/opt/project/ mode=700 owner=tom group=tom" ---对目录信息修改
站点目录权限修改:chown -R apache.apache /html/www/
ansible 192.168.137.135 -m file -a "path=/opt/project mode=700 owner=jerry group=jerry recurse=yes"
02.创建数据信息
目录信息:
ansible 192.168.137.135 -m file -a "path=/etc/runtime state=directory"
文件信息:
ansible 192.168.137.135 -m file -a "path=/etc/runtime/runtime1 state=touch"
检查文件信息是否存在:
ansible 192.168.137.135 -m file -a "path=/etc/runtime/runtime1 state=file"
链接文件:
软连接:
ansible 192.168.137.135 -m file -a "src=/etc/runtime/runtime.txt path=/etc/runtime/runtime_hard_link state=link"
硬链接:
ansible 192.168.137.135 -m file -a "src=/etc/runtime/runtime.txt path=/etc/runtime/runtime_hard_link state=hard"
03.删除数据信息
文件信息:ansible 192.168.137.135 -m file -a "path=/etc/runtime/runtime_hard_link state=absent"
目录信息:ansible 192.168.137.135 -m file -a "path=/etc/runtime state=absent"
作用:可以对远程主机进行压缩处理
owner: 指定文件压缩后的属主信息
group: 指定文件压缩后的属组信息
mode: 指定文件压缩后的权限信息
remove(了解): 将进行压缩后的数据删除 (删除文件数据信息)
path: 指定需要进行压缩数据信息
dest: 指定压缩包文件保存路径
format: 指定压缩数据方式 bz2 gz(默认) tar xz zip
exclude_path: 在压缩数据信息时进行指定数据信息排除
force_archive: 单独压缩一个文件信息时,也可以进行强制压缩处理
实践需求:需要将远程主机/etc/runtime 目录进行压缩处理
ansible webservers -m archie -a "path=/etc/runtime dest=/etc/runtime.tar.gz owner=tom group=tom mode=666 remove=yes"
作用说明:可以将管理端压缩包数据信息解压到被管理主机上
参数说明:
src: 指定要解压的数据信息
dest: 解压数据指定路径信息
remote_src: 将被管理主机上压缩包进行解压
ansible 192.168.137.135 -m unarchive -a "src=/data/test.tar.gz dest=/html/test"
作用说明:可以实现对文件信息修改操作
常用指令参数:
path: 指定要修改文件的信息
regexp: 利用正则匹配出文件中指定行信息
line: 对匹配行信息做修改
state: 可以实现删除文件信息
insertafter: 匹配指定信息,并在指定信息下面新起一行添加信息
insertafter: 匹配指定信息,并在指定信息下面新起一行添加信息
create: 在指定文件后面追加新的信息 等价于 >>
实践操作:
01.修改文件中指定内容,将runtime开头行信息修改为runtime123
ansible 192.168.137.135 -m lineinfile -a "path=/etc/runtime/runtime1.txt regexp='^runtime' line='runtime123'"
02.删除文件内信息
ansible 192.168.137.135 -m lineinfile -a "path=/etc/runtime/runtime1.txt regexp='^runtime' state=absent"
03.在文件指定位置插入信息
ansible 192.168.137.135 -m lineinfile -a "path=/etc/runtime/runtime1.txt insertafter='^#runtime' line='runtime 8080'" 之后插入
ansible 192.168.137.135 -m lineinfile -a "path=/etc/runtime/runtime1.txt insertbefore='^#runtime' line='runtime 9090'" 之前插入
04.在文件中最后一行后面附加新的信
ansible 192.168.137.135 -m lineinfile -a "path=/etc/runtime/runtime1.txt line='123' create=yes"
作用说明:可以批量设置定时任务信息
常用指令参数:
minute: 可以设置分钟信息
hour: 可以设置小时信息
day: 可以设置日期信息
month: 可以设置月份信息
weekday: 可以设置信息信息
special_time: 可以按照指定周期时间执行
job: 可以设置需要执行任务信息
name: 表示给定时任务添加注释
disabled 表示将指定定时任务进行注释
实践过程:
01.创建定时任务:每天夜里2点 备份数据信息
ansible webservers -m cron -a "name='ansible-01-cron' minute=0 hour=2 job='cp /etc/hosts /etc/hosts01.bak &>/dev/null'"
02.批量删除定时任务
ansible webservers -m cron -a "name='ansible-01-cron' state=absent"
03.临时关闭定时任务设置
ansible webservers -m cron -a "name='ansible-01-cron' minute=0 hour=2 job='cp /etc/hosts /etc/hosts01.bak &>/dev/null' disabled=yes"
作用说明:实现存储服务批量挂载已经卸载
常用指令参数:
path: 指定挂载点目录信息
src: 指定挂载存储设备信息
fstype: 指定挂载使用文件系统类型 挂载nfs fstype=xfs
opts: 指定挂载参数信息 rw ro suid nosuid auto noauto ... defaults
state: 指定挂载或卸载操作
实践操作:
01.主机批量挂载
ansible webservers -m mount -a "src=192.168.137.135:/data/bbs path=/mnt fstype=nfs opts=defaults state=present" 实现重启系统进行永久挂载
ansible webservers -m mount -a "src=192.168.137.135:/data/bbs path=/mnt fstype=nfs opts=defaults state=mounted" 实现立即挂载以及永久挂载
02.主机批量卸载存储设备
ansible webservers -m mount -a "path=/mnt state=absent" 立即卸载并删除fstab挂载配置信息
ansible webservers -m mount -a "path=/mnt state=unmounted" 立即卸载但不会删除fstab挂载配置信息++
作用说明:实现主机用户批量创建/批量删除用户
常用指令参数:
name: 指定创建用户名称
password: 指定创建用户密码信息 password=密文信息
uid: 指定创建用户uid数值信息
group: 指定用户所属主要组信息 useradd -g
groups: 指定用户附属组信息 useradd -G
shell: 指定创建用户登录方式 /bin/bash(可以登录) /sbin/nologin(不能登录)
create_home 确认是否创建家目录 默认yes创建家目录 useradd -M
remove: 确认是否删除用户家目录信息 默认no不删除家目录
12345678910
01.创建一个可以登陆系统的用户 Jerry uid 6666 组为 tom 主为root
[root@ansible project]# ansible webservers -m user -a "name=jerry uid=6666 group=tom groups=root"
02.给用户设置密码
ansible webservers -m user -a "name=jerry uid=6666 group=tom groups=root password="$6$yC53tE5.rktVbTLp$hNOe2qYCGBGqphiabG/51MJ3/ihxzE63CwfcM8i.wS2kkSHLnfQaiDilZgqeLBThZ8yNvTMNAm/oqUAePtrgc/""
如何生成密码密文信息:
[root@ansible project]# ansible all -i locahost, -m debug -a "msg={{ '123456' | password_hash('sha512')}}"
locahost | SUCCESS => {
"msg": "$6$yC53tE5.rktVbTLp$hNOe2qYCGBGqphiabG/51MJ3/ihxzE63CwfcM8i.wS2kkSHLnfQaiDilZgqeLBThZ8yNvTMNAm/oqUAePtrgc/"
}
03.创建虚拟用户信息
ansible webservers -m user -a 'name=dog shell=/sbin/nologin create_home=no'
04.删除用户信息
ansible webservers -m user -a 'name=dog state=absent' 只删除用户信息,不删除家目录
ansible webservers -m user -a 'name=dog state=absent remove=yes' 删除用户及家目录
实践操作
[root@ansible project]# ansible webservers -m user -a "name=tom group=tom"
192.168.137.135 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/tom",
"name": "tom",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
192.168.137.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/tom",
"name": "tom",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
作用说明:创建用户组信息或删除用户组信息
常用指令:
name: 指定用户组名称信息
gid: 指定用户组gid数值信息
state: 是否创建或删除用户组
[root@ansible project]# ansible webservers -m group -a "name=tom state=present"
192.168.137.135 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 1001,
"name": "tom",
"state": "present",
"system": false
}
192.168.137.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 1001,
"name": "tom",
"state": "present",
"system": false
}
作用说明:
1.下载安装/卸载删除系统软件程序包
2.可以指定下载源信息 yum仓库 <---/etc/yum.repos.d/xxx.repo aliyun nginx 清华源
常用指令参数介绍:
name: 指定下载软件名称信息
state: 指定下载或是卸载软件
installed/present 下载安装软件 rsync-2.0
adsent/removed 卸载软件程序
latest 更新软件程序 rsync-3.0
enablerepo: 指定下载源信息
disablerepo: 指定不用那个下载源
doenload_only: 只下载软件包不进行安装
实践操作
01.批量部署安装软件程序
ansible webservers -m yum -a "name=htop state=installed" 安装
ansible webservers -m yum -a "name=htop state=absent" 卸载
02.指定下载源信息
ansible 192.168.137.135 -m yum -a "name=htop state=installed enablerepo=runtime"
ansible 192.168.137.135 -m yum -a "name=htop state=installed disablerepo=aaa"
03.只下载软件包,不进行软件包升级
ansible 192.168.137.135 -m yum -a "name=htop state=installed download_only=true"
补充:yum_repository 模块
作用说明:利用此模块生成yum下载源文件
常用指令参数介绍:
name: 下载源仓库名称
description: 下载源仓库描述介绍信息
baseurl: 下载源仓库地址
enabled: 是否启用下载源文件
实践操作过程:
01.生成下载源文件
ansible 192.168.137.135 -m yum_repository -a "name=runtime description='local yum' baseurl=http://192.168.200.100 enabled=yes"
作用说明:批量管理服务状态
常用指令参数介绍:
name: 指定管理服务名称
state: 指定服务运行状态(started:启动 stopped:停止 reloaded:平滑重启)
enabled: 设置是否让服务开机自启 yes-开机自启 no-开机不让运行
实践操作过程:批量管理服务状态
ansible webservers -m service -a "name=nginx state=stopped enabled=no"
ansible webservers -m service -a "name=nginx state=started enabled=yes"
类似模块:systemd
1234567891011
作用说明:可以采集被管理主机的系统和硬件信息
常用指令参数信息:
filter: 过滤筛选想关注信息
ansible 192.168.137.135 -m setup -a "filter=ansible_eth0"
PS:利用收集的信息做判断使用
修改默认清单文件位置,构建清单
[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=runtime
mysql ansible_user=root ansible_password=runtime
php ansible_user=root ansible_password=runtime
[root@ansible ~]# vim /etc/hosts
192.168.183.135 nginx
192.168.183.136 mysql
192.168.183.137 php
//列出lnmp主机组
[root@ansible ~]# ansible lnmp --list-hosts
hosts (3):
nginx
mysql
php
//设置密钥连接
[root@ansible ~]# ssh nginx
[root@ansible ~]# ssh mysql
[root@ansible ~]# ssh php
//测试受控机连通性
[root@ansible ~]# ansible lnmp -m ping
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"
} nginx | 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,openssldevel,gd-devel,gcc,gcc-c++,make,wget,vim
state=present'
//下载软件包并解压
[root@ansible ~]# ansible nginx -a 'wget http://nginx.org/download/nginx1.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 ~]# cat /etc/ansible/scripts/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 22 16:21 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'
[root@ansible ~]# ansible nginx -a 'which nginx'
nginx | CHANGED | rc=0 >>
/usr/local/nginx/sbin/nginx
//启动服务
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible ~]# cat /etc/ansible/scripts/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 22 16:21 configure.sh
-rw-r--r--. 1 root root 364 Oct 22 16:32 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,openssldevel,openssl,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'
//修改属主属组
[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-22T09:07:08.955645Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is
deprecated. Please use --explicit_defaults_for_timestamp server option (see
documentation for more details).
2022-10-22T09:07:09.140746Z 0 [Warning] InnoDB: New log files created, LSN=457902022-10-22T09:07:09.180210Z 0 [Warning] InnoDB: Creating foreign key constraint
system tables.
2022-10-22T09:07:09.194516Z 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: e86b0a6f-51e8-11ed-8887-000c2907de9b.
2022-10-22T09:07:09.195011Z 0 [Warning] Gtid table is not ready to be used.
Table 'mysql.gtid_executed' cannot be opened.
2022-10-22T09:07:09.348005Z 0 [Warning] A deprecated TLS version TLSv1 is
enabled. Please use TLSv1.2 or higher.
2022-10-22T09:07:09.348030Z 0 [Warning] A deprecated TLS version TLSv1.1 is
enabled. Please use TLSv1.2 or higher.
2022-10-22T09:07:09.348374Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-22T09:07:09.422073Z 1 [Note] A temporary password is generated for
root@localhost: ,C-a.dCcp7-r
[root@ansible ~]# ansible mysql -m shell -a "echo ',C-a.dCcp7-r' > pass"
//生成配置文件启动服务
[root@ansible ~]# cd /etc/ansible/scripts/
[root@ansible ~]# cat /etc/ansible/scripts/mysql_service.sh
#!/bin/bash
cat >> /etc/my.cnf < /usr/lib/systemd/system/mysqld.service <>
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 [::]:*
//关闭防火墙和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 ~]# 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
y
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 <>
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.183.137: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.183.137: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.183.135' >> /usr/local/php8/etc/php-fpm.d/www.conf"
php | CHANGED | rc=0 >>
//创建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 [::]:* 访问php测试页面
访问php测试页面