Ansible常用模块的介绍与使用
ansible常用模块有:
- ping
- yum
- template
- copy
- user
- group
- service
- raw
- command
- shell
- script
ansible常用模块raw、command、shell的区别:
- shell模块调用的/bin/sh指令执行
- command模块不是调用的shell的指令,所以没有bash的环境变量
- raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了
file模块的用法
file 模块可以帮助我们完成一些对文件的基本操作。比如,创建文件或目录、删除文件或目录、修改文件权限等。
path参数 :必须参数,用于指定要操作的文件或目录
state参数 :
我们想要创建目录,那么则需要设置path=/test/abc
,我们无法从”/test/abc
“这个路径看出b是一个文件还是一个目录state的值设置为directory,”directory”为目录之意,当它与path结合,ansible就能知道我们要操作的目标是一个目录。同理,当我们想要操作的/testdir/a/b
是一个文件时,则需要将state的值设置为touch。
当我们想要创建软链接文件时,需将state设置为link。
想要创建硬链接文件时,需要将state设置为hard。
当我们想要删除一个文件时(删除时不用区分目标是文件、目录、还是链接),则需要将state的值设置为absent.
src参数 :当state设置为link或者hard时,表示我们想要创建一个软链或者硬链,所以,我们必须指明软链或硬链链接的哪个文件,通过src参数即可指定链接源。
**force参数 : **当state=link的时候,可配合此参数强制创建链接文件,当force=yes时,表示强制创建链接文件。不过强制创建链接文件分为三种情况。
情况一:当要创建的链接文件指向的源文件并不存在时,使用此参数,可以先强制创建出链接文件。
情况二:当要创建链接文件的目录中已经存在与链接文件同名的文件时,将force设置为yes,会将同名文件覆盖为链接文件,相当于删除同名文件,创建链接文件。
情况三:当要创建链接文件的目录中已经存在与链接文件同名的文件,并且链接文件指向的源文件也不存在,这时会强制替换同名文件为链接文件。
owner参数 :用于指定被操作文件的属主,属主对应的用户必须在远程主机中存在,否则会报错。
group参数 :用于指定被操作文件的属组,属组对应的组必须在远程主机中存在,否则会报错。
mode参数:用于指定被操作文件的权限。
recurse参数:当要操作的文件为目录,将recurse设置为yes,可以递归的修改目录中文件的属性。
ansible常用模块之ping
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@ansible ~]# ansible all -m ping
192.168.10.150 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.10.151 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.10.151 port 22: No route to host",
"unreachable": true
}
#没连通
ansible常用模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能。
//查看受控主机的/tmp目录内容
[root@ansible ~]# ansible 192.168.10.150 -a 'ls /tmp'
192.168.10.150 | CHANGED | rc=0 >>
ansible_command_payload_3bvoyhhc
ks-script-889z9ogn
ks-script-9gz23mj9
vmware-root_893-3988097506
vmware-root_922-2722632355
vmware-root_930-2722763397
//在受控主机的/tmp目录下新建一个文件test
[root@ansible ~]# ansible 192.168.10.150 -a 'touch /tmp/test'
[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.10.150 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.10.150 -a 'ls /tmp'
192.168.10.150 | CHANGED | rc=0 >>
ansible_command_payload_uyqqnupx
ks-script-889z9ogn
ks-script-9gz23mj9
test
vmware-root_893-3988097506
vmware-root_922-2722632355
vmware-root_930-2722763397
//command模块不支持管道符,不支持重定向
[root@ansible ~]# ansible 192.168.10.150 -a "echo 'hello world' > /tmp/test"
192.168.10.150 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@ansible ~]# ansible 192.168.10.150 -a 'cat /tmp/test'
192.168.10.150 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.10.150 -a 'ps -ef|grep vsftpd'
192.168.10.150 | FAILED | rc=1 >>
error: unsupported SysV option
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).non-zero return code
ansible常用模块之raw
raw模块用于在远程主机上执行命令,其支持管道符与重定向
//支持重定向
[root@ansible ~]# ansible 192.168.10.150 -m raw -a "echo 'hello world' > /tmp/test"
192.168.10.150 | CHANGED | rc=0 >>
Shared connection to 192.168.10.150 closed.
[root@ansible ~]# ansible 192.168.10.150 -a 'cat /tmp/test'
192.168.10.150 | CHANGED | rc=0 >>
hello world
//支持管道符
[root@ansible ~]# ansible 192.168.10.150 -m raw -a 'cat /tmp/test|grep -Eo hello'
192.168.10.150 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.10.150 closed.
ansible常用模块之shell
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
//在受控主机上简单创建一个脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# ls
[root@localhost scripts]# vim test.sh
#!/bin/bash
for i in $(seq 10);do
echo $i
done
//使用shell模块在受控机上执行受控机上的脚本
[root@ansible ~]# ansible 192.168.10.150 -m shell -a '/bin/bash /scripts/test.sh'
192.168.10.150 | CHANGED | rc=0 >>
1
2
3
4
5
6
7
8
9
10
ansible常用模块之script
script模块用于在受控机上执行主控机上的脚本
//在主控机上创建一个脚本
[root@ansible ~]# cd /etc/ansible/
[root@ansible ~]# mkdir scripts
[root@ansible ~]# cd scripts/
[root@ansible scripts]# vim a.sh
#!/bin/bash
echo "hello world"
//执行脚本时需要绝对路径
[root@ansible ~]# ansible 192.168.10.150 -m script -a '/etc/ansible/scripts/a.sh'
192.168.10.150 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.10.150 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.10.150 closed."
],
"stdout": "hello world\r\n",
"stdout_lines": [
"hello world"
]
}
ansible常用模块之template
template模块用于生成一个模板,并可将其传输至远程主机上。
//将根目录下的anaconda-ks.cfg传送到受控主机下的/tmp/下,并赋予775的权限
[root@ansible ~]# ansible 192.168.10.150 -m template -a 'src=~/anaconda-ks.cfg dest=/tmp/ mode=0775'
192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "71e48dd2e917295afbd891c5eae07b2593600f38",
"dest": "/tmp/anaconda-ks.cfg",
"gid": 0,
"group": "root",
"md5sum": "2157b1294c5aef23139307ff63685719",
"mode": "0775",
"owner": "root",
"size": 1092,
"src": "/root/.ansible/tmp/ansible-tmp-1666514565.4204142-105228-4638286770822/source",
"state": "file",
"uid": 0
}
//可以看见在受控主机下的/tmp/下有anaconda-ks.cfg这个文件,并且有775的权限
[root@ansible ~]# ansible 192.168.10.150 -a 'ls -l /tmp/'
192.168.10.150 | CHANGED | rc=0 >>
total 8
-rwxrwxr-x 1 root root 1092 Oct 23 16:42 anaconda-ks.cfg
drwx------ 2 root root 41 Oct 23 16:44 ansible_command_payload__vyel6sg
-rw-r--r-- 1 root root 12 Oct 23 16:13 test
drwx------ 2 root root 6 Oct 23 16:02 vmware-root_922-2722632355
drwx------. 2 root root 6 Oct 23 15:56 vmware-root_930-2722763397
ansible常用模块之copy
copy模块用于复制文件至远程受控机
[root@ansible ~]# ls /etc/ansible/scripts/
a.sh
//将a.sh复制到受控主机
[root@ansible ~]# ansible 192.168.10.150 -m copy -a 'src=/etc/ansible/scripts/a.sh dest=/scripts/'
192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "62a1e17a8ab950e59e56563ebf327d9a14520575",
"dest": "/scripts/a.sh",
"gid": 0,
"group": "root",
"md5sum": "85239384c0243e32ef2e37e8cd3b3114",
"mode": "0644",
"owner": "root",
"size": 32,
"src": "/root/.ansible/tmp/ansible-tmp-1666515163.7038138-132258-153764762517348/source",
"state": "file",
"uid": 0
}
//在受控主机上查看到a.sh
[root@ansible ~]# ansible 192.168.10.150 -a 'ls /scripts/'192.168.10.150 | CHANGED | rc=0 >>
a.sh
test.sh
ansible常用模块之group
group模块用于在受控机上添加或删除组
//创建一个runtime组gid为255
[root@ansible ~]# ansible 192.168.10.150 -m group -a 'name=runtime gid=255 state=present'
192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 255,
"name": "runtime",
"state": "present",
"system": false
}
//可以在受控主机上查看到已经有了gid为250的runtime组
[root@ansible ~]# ansible 192.168.10.150 -a 'grep runtime /etc/group'192.168.10.150 | CHANGED | rc=0 >>
runtime:x:255:
//删除受控机上的runtime组
[root@ansible ~]# ansible 192.168.10.150 -m group -a 'name=runtime state=absent'
192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "runtime",
"state": "absent"
}
[root@ansible ~]# ansible 192.168.10.150 -a 'grep runtime /etc/group'
192.168.10.150 | FAILED | rc=1 >>
non-zero return code
ansible常用模块之user
user模块用于管理受控机的用户帐号。
//在受控机上添加一个系统用户,用户名为mysql,uid为306,设置其shell为/sbin/nologin,无家目录
[root@ansible ~]# ansible 192.168.10.150 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.10.150 | 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.10.150 -a 'grep mysql /etc/passwd'
192.168.10.150 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
//修改mysql用户的uid为366
[root@ansible ~]# ansible 192.168.10.150 -m user -a 'name=mysql uid=366'
192.168.10.150 | 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": 366
}
[root@ansible ~]# ansible 192.168.10.150 -a 'grep mysql /etc/passwd'192.168.10.150 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
//删除受控机上的mysql用户
[root@ansible ~]# ansible 192.168.10.150 -m user -a 'name=mysql state=absent'
192.168.10.150 | 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.10.150 -a 'grep mysql /etc/passwd'
192.168.10.150 | FAILED | rc=1 >>
non-zero return code
ansible常用模块之yum
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
- name:要管理的包名
- state:要进行的操作
state常用的值:
- latest:安装软件
- installed:安装软件
- present:安装软件
- removed:卸载软件
- absent:卸载软件
若想使用yum来管理软件,请确保受控机上的yum源无异常。
//在受控机上查询看httpd软件是否安装
[root@localhost ~]# rpm -qa|grep httpd
[root@localhost ~]#
//在ansible主机上使用yum模块在受控机上安装httpd
[root@ansible ~]# ansible 192.168.10.150 -m yum -a 'name=httpd state=present'
192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: httpd-tools-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64",
"Installed: apr-1.6.3-12.el8.x86_64",
"Installed: mailcap-2.1.48-3.el8.noarch",
"Installed: mod_http2-1.15.7-5.module_el8.6.0+1111+ce6f4ceb.x86_64",
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: httpd-filesystem-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.noarch",
"Installed: centos-logos-httpd-85.8-2.el8.noarch"
]
}
//查看受控机上是否安装了httpd
[root@localhost ~]# rpm -qa|grep httpd
httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64
httpd-tools-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64
httpd-filesystem-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.noarch
centos-logos-httpd-85.8-2.el8.noarch
//在ansible主机上使用yum模块在受控机上卸载httpd
[root@ansible ~]# ansible 192.168.10.150 -m yum -a 'name=httpd state=absent'
192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Removed: mod_http2-1.15.7-5.module_el8.6.0+1111+ce6f4ceb.x86_64",
"Removed: httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64"
]
}
//查看受控机上是否安装了httpd,httpd的包已经被卸载了,只剩依赖包了
[root@localhost ~]# rpm -qa|grep httpd
httpd-tools-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64
httpd-filesystem-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.noarch
centos-logos-httpd-85.8-2.el8.noarch
ansible常用模块之service
service模块用于管理受控机上的服务。
//查看受控主机上httpd服务是否开启,这里显示未开启
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: di>
Active: inactive (dead)
Docs: man:httpd.service(8)
lines 1-4/4 (END)
//启动受控主机上的httpd服务
[root@ansible ~]# ansible 192.168.10.150 -m service -a 'name=httpd state=started'
192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
...
//查看受控机上的httpd服务是否启动
[root@ansible ~]# ansible 192.168.10.150 -a 'systemctl is-active httpd'
192.168.10.150 | CHANGED | rc=0 >>
active
//设置受控机上的vsftpd服务开机自动启动
[root@ansible ~]# ansible 192.168.10.150 -m service -a 'name=httpd enabled=yes'
192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "httpd",
...
//查看受控机上的httpd服务是否开机自启
[root@ansible ~]# ansible 192.168.10.150 -a 'systemctl is-enabled httpd'
192.168.10.150 | CHANGED | rc=0 >>
enabled
//停止受控机上的httpd服务
[root@ansible ~]# ansible 192.168.10.150 -m service -a 'name=httpd state=stopped'
192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "stopped",
...
//查看受控机上的httpd服务是否启动
[root@ansible ~]# ansible 192.168.10.150 -a 'systemctl is-active httpd'
192.168.10.150 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ~]# ansible 192.168.10.150 -a 'ss -antl'
192.168.10.150 | 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 [::]:*
//在受控主机上根目录中创建scripts目录
[root@ansible ~]# ansible 192.168.10.150 -m file -a 'path=/scripts state=directory'
192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/scripts",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
//在受控主机上scripts目录中创建test文件
[root@ansible ~]# ansible 192.168.10.150 -m file -a 'path=/scripts/test state=touch'192.168.10.150 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/scripts/test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 0,
"state": "file",
"uid": 0
}
//在受控主机上查看
[root@localhost ~]# ll /scripts/
total 0
-rw-r--r--. 1 root root 0 Oct 23 18:57 test
ansible实现lnmp架构
环境介绍
主机名 | ip | 服务 | 系统 | |
---|---|---|---|---|
控制主机 | ansible | 192.168.10.145 | ansible(已安装) | centos8 |
受控主机 | nginx | 192.168.10.150 | nginx | centos8 |
受控主机 | mysql | 192.168.10.151 | mysql | centos8 |
受控主机 | php | 192.168.10.152 | php | centos8 |
配置清单
//修改配置文件设置清单位置
[root@ansible ~]# cd /etc/ansible/
[root@ansible ~]# vim ansible.cfg
inventory = /etc/ansible/inventory
//配置清单
[root@ansible ~]# vim inventory
[nginx]
192.168.10.150
[mysql]
192.168.10.151
[php]
192.168.10.152
受管主机安装python3
[root@ansible ~]# ansible all -m yum -a 'name=python3 state=present'
受管主机关闭防火墙和selinux
[root@ansible ~]# ansible all -a 'setenforce 0'
[root@ansible ~]# ansible all -m selinux -a 'state=disabled'
[root@ansible ~]# ansible all -m service -a 'name=firewalld state=stopped enabled=no'
受控主机nginx安装配置nginx
//创建系统用户nginx
[root@ansible ~]# ansible nginx -m user -a 'name=nginx system=yes 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'
//下载nginx并解压
[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 /scripts
[root@ansible ~]# vim /scripts/nginx.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 \
[root@ansible ~]# ansible nginx -m script -a '/scripts/nginx.sh'
[root@ansible ~]# ansible nginx -m shell -a 'cd nginx-1.20.2 && make && make install'
//配置环境变量
[root@ansible ~]# ansible nginx -m shell -a 'echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh'
[root@ansible ~]# ansible nginx -m shell -a 'source /etc/profile.d/nginx.sh'
//编写service文件
[root@ansible ~]# vim /scripts/nginxservice.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
[root@ansible ~]# ansible nginx -m script -a '/scripts/nginxservice.sh'
[root@ansible ~]# ansible nginx -a 'systemctl daemon-reload'
//开启nginx服务并开机自启
[root@ansible ~]# ansible nginx -m service -a 'name=nginx state=started enabled=yes'
//查看nginx服务状态
[root@ansible ~]# ansible nginx -a 'ss -anlt'
192.168.10.150 | 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 [::]:*
//配置php网页
[root@ansible ~]# vim /scripts/nginxphp.sh
#!/bin/bash
cat > /usr/local/nginx/html/index.php <<EOF
<?php
phpinfo();
?>
EOF
[root@ansible ~]# ansible nginx -m script -a '/scripts/nginxphp.sh'
//修改nginx配置文件
[root@ansible ~]# vim /scripts/nginxconf.sh
#!/bin/bash
sed -i "45c index index.php index.html index.htm;" /usr/local/nginx/conf/nginx.conf
sed -i "65c location ~ \.php$ {" /usr/local/nginx/conf/nginx.conf
sed -i "66c root /var/www/html;" /usr/local/nginx/conf/nginx.conf
sed -i "67c fastcgi_pass 192.168.10.152:9000;" /usr/local/nginx/conf/nginx.conf
sed -i "68c fastcgi_index index.php;" /usr/local/nginx/conf/nginx.conf
sed -i "69c fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;" /usr/local/nginx/conf/nginx.conf
sed -i "70c include fastcgi_params;" /usr/local/nginx/conf/nginx.conf
sed -i "71c }" /usr/local/nginx/conf/nginx.conf
[root@ansible ~]# ansible nginx -m script -a '/scripts/nginxconf.sh'
受控主机mysql安装配置mysql
//创建系统用户msyql
[root@ansible ~]# ansible mysql -m user -a 'name=mysql system=yes shell=/sbin/nologin state=present'
//安装依赖包
[root@ansible ~]# ansible mysql -m yum -a 'name=ncurses-devel,openssl-devel,openssl,cmake,mariadb-devel,ncurses-compat-libs state=present'
//下载nginx并解压
[root@ansible ~]# ansible mysql -a 'wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz'
[root@ansible ~]# ansible mysql -a 'tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz'
//修改MySQL数据库名称
[root@ansible ~]# ansible mysql -a 'mv mysql-5.7.34-linux-glibc2.12-x86_64 mysql'
[root@ansible ~]# ansible mysql -a 'mv mysql /usr/local/'
//修改目录/usr/local/mysql的属主属组
[root@ansible ~]# ansible mysql -m file -a 'path=/usr/local/mysql owner=mysql group=mysql'
//添加环境变量
[root@ansible ~]# ansible mysql -m shell -a 'echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh'
[root@ansible ~]# ansible mysql -m shell -a 'source /etc/profile.d/mysql.sh'
//头文件
[root@ansible ~]# ansible mysql -a 'ln -sv /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'
//man文档
[root@ansible ~]# ansible mysql -a 'sed -i "22a MANDATORY_MANPATH /usr/local/mysql/man" /etc/man_db.conf'
//建立数据存放目录
[root@ansible ~]# ansible mysql -m file -a 'path=/opt/data state=directory owner=mysql group=mysql'
//初始化数据库
[root@ansible ~]# ansible mysql -m shell -a '/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/'
192.168.10.151 | CHANGED | rc=0 >>
2022-10-23T14:22:54.470416Z 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:22:54.624595Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-23T14:22:54.663264Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-23T14:22:54.728424Z 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: 2f3fd8de-52de-11ed-b420-000c290801b6.
2022-10-23T14:22:54.728999Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-23T14:22:55.226089Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-23T14:22:55.289145Z 1 [Note] A temporary password is generated for root@localhost: #q3L>Ze<k((i
//编写脚本添加mysql配置文件和mysql的service文件
[root@ansible ~]# vim /scripts/mysql.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
cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
[root@ansible ~]# ansible mysql -m script -a '/scripts/mysql.sh'
[root@ansible ~]# ansible mysql -a 'systemctl daemon-reload'
//开启服务并开机自启
[root@ansible ~]# ansible mysql -m service -a 'name=mysqld state=started enabled=yes'
[root@ansible ~]# ansible mysql -a 'ss -anlt'
192.168.10.151 | 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 [::]:*
受控主机php安装配置php
//安装依赖包
[root@ansible ~]# ansible php -m yum -a 'name=epel-release state=present'
[root@ansible ~]# ansible php -m yum -a 'name=libxml2,libxml2-devel,openssl,openssl-devel,bzip2,bzip2-devel,libcurl,libcurl-devel,libicu-devel,libjpeg,libjpeg-devel,libpng,libpng-devel,openldap-devel,pcre-devel,freetype,freetype-devel,gmp,gmp-devel,libmcrypt,libmcrypt-devel,readline,readline-devel,libxslt,libxslt-devel,mhash,mhash-devel,php-mysqlnd,libsqlite3x-devel,libzip-devel,wget,gcc,gcc-c++,make state=present'
[root@ansible ~]# ansible php -a 'yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm'
//下载PHP并解压
[root@ansible ~]# ansible php -a 'wget https://www.php.net/distributions/php-8.1.11.tar.gz'
[root@ansible ~]# ansible php -a 'tar xf php-8.1.11.tar.gz -C /usr/src'
//编译安装php
[root@ansible ~]# vim scripts/php.sh
#!/bin/bash
cd /usr/src/php-8.1.11/
./configure --prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm \
--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-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
[root@ansible ~]# ansible php -m script -a '/scripts/php.sh'
[root@ansible ~]# ansible php -m shell -a 'cd /usr/src/php-8.1.11/ && make && make install'
//配置文件
[root@ansible ~]# ansible php -a 'cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf'
[root@ansible ~]# ansible php -a 'cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf'
//头文件
[root@ansible ~]# ansible php -a 'ln -sv /usr/local/php /usr/include/php'
//编写service文件
[root@ansible ~]# vim /scripts/phpservice.sh
#!/bin/bash
cat > /usr/lib/systemd/system/php.service << EOF
[Unit]
Description=php server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=ps -ef |grep php |grep -v grep|awk '{print$2}'|xargs kill
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
[root@ansible ~]# ansible php -m script -a '/scripts/phpservice.sh'
[root@ansible ~]# ansible php -a 'systemctl daemon-reload'
//开启服务并开机自启
[root@ansible ~]# ansible php -m service -a 'name=php state=started enabled=yes'
[root@ansible ~]# ansible php -a 'ss -antl'
192.168.10.152 | 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 [::]:*
//在php端上配置网站
[root@ansible ~]# vim /scripts/phpindex.sh
[root@ansible ~]# cat /scripts/phpindex.sh
#!/bin/bash
mkdir -p /var/www/html
cat > /var/www/html/index.php << EOF
<?php
phpinfo();
?>
EOF
[root@ansible ~]# ansible php -m script -a '/scripts/phpindex.sh'
//修改php/usr/local/php/etc/php-fpm.d/www.conf文件的clisten和clisten.allowed_clients指向
[root@ansible ~]# ansible php -a 'sed -i "36c listen = 192.168.10.152:9000" /usr/local/php/etc/php-fpm.d/www.conf'
[root@ansible ~]# ansible php -a 'sed -i "63c listen.allowed_clients = 192.168.10.150" /usr/local/php/etc/php-fpm.d/www.conf'
//重启nginx服务和php服务
[root@ansible ~]# ansible nginx -m service -a 'name=nginx state=restarted'
[root@ansible ~]# ansible php -m service -a 'name=php state=restarted'
浏览器访问查看