ansible常用模塊 一、ansible常用模塊使用詳解 ansible常用模塊有: ping yum template copy user group service raw command shell script ansible常用模塊raw、command、shell的區別: shell模 ...
ansible常用模塊
目錄一、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模塊了
1.ansible常用模塊之ping
ping模塊用於檢查指定節點機器是否連通,用法很簡單,不涉及參數,主機如果線上,則回覆pong
[root@ansible ~]# ansible all -m ping
192.168.111.142 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
2.ansible常用模塊之command
command模塊用於在遠程主機上執行命令,ansible預設就是使用command模塊。
command模塊有一個缺陷就是不能使用管道符和重定向功能。
//查看受控主機的/tmp目錄內容
[root@ansible ~]# ansible 192.168.111.142 -a 'ls /tmp'
192.168.111.142 | CHANGED | rc=0 >>
ansible_command_payload_z973ukjx
systemd-private-ac56c8c9ec59454cbb39a024c91beb72-chronyd.service-S24EYh
vmware-root_897-3979643105
//在受控主機的/tmp目錄下新建一個文件test
[root@ansible ~]# ansible 192.168.111.142 -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.111.142 | CHANGED | rc=0 >>
//查看一下是否創建成功
[root@ansible ~]# ansible 192.168.111.142 -a 'ls /tmp'
192.168.111.142 | CHANGED | rc=0 >>
ansible_command_payload_62jkggxf
systemd-private-ac56c8c9ec59454cbb39a024c91beb72-chronyd.service-S24EYh
test
vmware-root_897-3979643105
//command模塊不支持管道符,不支持重定向
[root@ansible ~]# ansible 192.168.111.142 -a "echo 'hello world' > /tmp/test"
192.168.111.142 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@ansible ~]# ansible 192.168.111.142 -a 'cat /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.111.142 -a 'ps -ef | grep httpd'
192.168.111.142 | FAILED | rc=1 >>
error: garbage 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
3.ansible常用模塊之raw
raw模塊用於在遠程主機上執行命令,其支持管道符與重定向
//支持重定向
[root@ansible ~]# ansible 192.168.111.142 -m raw -a 'echo "hello world" > /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>
Shared connection to 192.168.111.142 closed.
[root@ansible ~]# ansible 192.168.111.142 -a 'cat /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>
hello world
//支持管道符
[root@ansible ~]# ansible 192.168.111.142 -m raw -a 'cat /tmp/test | grep -Eo hello'
192.168.111.142 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.111.142 closed.
4.ansible常用模塊之shell
shell模塊用於在受控機上執行受控機上的腳本,亦可直接在受控機上執行命令。
shell模塊亦支持管道與重定向。
//查看受控機上的腳本
[root@localhost ~]# ll /scripts/
total 4
-rw-r--r-- 1 root root 43 Oct 23 19:00 test.sh
//使用shell模塊在受控機上執行受控機上的腳本
[root@ansible ~]# ansible 192.168.111.142 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'cat /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>
123456
123
456
5.ansible常用模塊之script
script模塊用於在受控機上執行主控機上的腳本
[root@ansible ~]# ll /scripts/
total 4
-rw-r--r-- 1 root root 48 Oct 23 19:06 a.sh
[root@ansible ~]# ansible 192.168.111.142 -m script -a '/scripts/a.sh &> /tmp/a'
192.168.111.142 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.111.142 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.111.142 closed."
],
"stdout": "",
"stdout_lines": []
}
//查看受控機上的/tmp/a文件內容
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'cat /tmp/a'
192.168.111.142 | CHANGED | rc=0 >>
123
456
hello world
6.ansible常用模塊之template
template模塊用於生成一個模板,並可將其傳輸至遠程主機上。
[root@ansible ~]# ansible 192.168.111.142 -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/centos.repo'
192.168.111.142 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
"dest": "/etc/yum.repos.d/centos.repo",
"gid": 0,
"group": "root",
"md5sum": "ed031c350da2532e6a8d09a4d9b05278",
"mode": "0644",
"owner": "root",
"size": 1653,
"src": "/root/.ansible/tmp/ansible-tmp-1666530145.7581205-29420421110479/source",
"state": "file",
"uid": 0
}
//查看受控機上是否有163源
[root@localhost ~]# ls /etc/yum.repos.d/
centos.repo
7.ansible常用模塊之yum
yum模塊用於在指定節點機器上通過yum管理軟體,其支持的參數主要有兩個
- name:要管理的包名
- state:要進行的操作
state常用的值:
- latest:安裝軟體
- installed:安裝軟體
- present:安裝軟體
- removed:卸載軟體
- absent:卸載軟體
若想使用yum來管理軟體,請確保受控機上的yum源無異常。
//在受控機上查詢看vsftpd軟體是否安裝
[root@localhost ~]# rpm -qa|grep httpd
[root@localhost ~]#
//在ansible主機上使用yum模塊在受控機上安裝httpd
[root@ansible ~]# ansible 192.168.111.142 -m yum -a 'name=httpd state=present'
192.168.111.142 | 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@localhost ~]# rpm -qa|grep httpd
httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch
centos-logos-httpd-85.8-2.el8.noarch
httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
8.ansible常用模塊之copy
copy模塊用於複製文件至遠程受控機。
[root@ansible ~]# ls /scripts/
a.sh
[root@ansible ~]# ansible 192.168.111.142 -m copy -a 'src=/scripts/a.sh dest=/scripts/a'
192.168.111.142 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "91bb6c7a9d3e47e330dd408c5209d8c36226e014",
"dest": "/scripts/a/a.sh",
"gid": 0,
"group": "root",
"md5sum": "d92a6bbb430fd85b35bfc1f0a8035bdc",
"mode": "0644",
"owner": "root",
"size": 48,
"src": "/root/.ansible/tmp/ansible-tmp-1666530391.1743026-219581542309073/source",
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'ls /scripts/a'
192.168.111.142 | CHANGED | rc=0 >>
a.sh
9.ansible常用模塊之group
group模塊用於在受控機上添加或刪除組。
//在受控機上添加一個系統組,其gid為306,組名為mysql
[root@ansible ~]# ansible 192.168.111.142 -m group -a 'name=mysql gid=306 state=present'
192.168.111.142 | 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.111.142 -m shell -a 'grep mysql /etc/group'
192.168.111.142 | CHANGED | rc=0 >>
mysql:x:306:
//刪除受控機上的mysql組
[root@ansible ~]# ansible 192.168.111.142 -m group -a 'name=mysql state=absent'
192.168.111.142 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'grep mysql /etc/group'
192.168.111.142 | FAILED | rc=1 >>
non-zero return code
10.ansible常用模塊之user
user模塊用於管理受控機的用戶帳號。
//在受控機上添加一個系統用戶,用戶名為mysql,uid為306,設置其shell為/sbin/nologin,無家目錄
[root@ansible ~]# ansible 192.168.111.142 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.111.142 | 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.111.142 -m shell -a 'grep mysql /etc/passwd'
192.168.111.142 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'ls /home'
192.168.111.142 | CHANGED | rc=0 >>
//修改mysql用戶的uid為366
[root@ansible ~]# ansible 192.168.111.142 -m user -a 'name=mysql uid=366'
192.168.111.142 | 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.111.142 -m shell -a 'grep mysql /etc/passwd'
192.168.111.142 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
//刪除受控機上的mysql用戶
[root@ansible ~]# ansible 192.168.111.142 -m user -a 'name=mysql state=absent'
192.168.111.142 | 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.111.142 -m shell -a 'grep mysql /etc/passwd'
192.168.111.142 | FAILED | rc=1 >>
non-zero return code
11.ansible常用模塊之service
service模塊用於管理受控機上的服務。
//查看受控機上的httpd服務是否啟動
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'systemctl is-active httpd'
192.168.111.142 | FAILED | rc=3 >>
inactivenon-zero return code
//啟動受控機上的httpd服務
[root@ansible ~]# ansible 192.168.111.142 -m service -a 'name=httpd state=started'
192.168.111.142 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
...............
}
//查看受控機上的httpd服務是否啟動
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'systemctl is-active httpd'
192.168.111.142 | CHANGED | rc=0 >>
active
//查看受控機上的httpd服務是否開機自動啟動
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'systemctl is-enabled httpd'
192.168.111.142 | FAILED | rc=1 >>
disablednon-zero return code
//設置受控機上的httpd服務開機自動啟動
[root@ansible ~]# ansible 192.168.111.142 -m service -a 'name=httpd enabled=yes'
192.168.111.142 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "httpd",
..................
}
//查看受控機上的httpd服務是否開機自動啟動
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'systemctl is-enabled httpd'
192.168.111.142 | CHANGED | rc=0 >>
enabled
//停止受控機上的httpd服務
[root@ansible ~]# ansible 192.168.111.142 -m service -a 'name=httpd state=stopped'
192.168.111.142 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "stopped",
"status": {
"ActiveEnterTimestamp": "Sun 2022-10-23 21:17:04 CST",
..............
}
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'systemctl is-active httpd'
192.168.111.142 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'ss -anlt'
192.168.111.142 | 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 [::]:*
12.ansible常用模塊之file
file 模塊可以幫助我們完成一些對文件的基本操作。比如,創建文件或目錄、刪除文件或目錄、修改文件許可權等
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.111.142 -m file -a 'path=/tmp/abc state=touch owner=root group=root'
192.168.111.142 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/tmp/abc",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
//創建目錄
[root@ansible ~]# ansible 192.168.111.142 -m file -a 'path=/tmp/CBA state=directory mode=0755'
192.168.111.142 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/CBA",
"size": 6,
"state": "directory",
"uid": 0
}
//查看
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'ls -l /tmp/'
192.168.111.142 | CHANGED | rc=0 >>
total 0
drwxr-xr-x 2 root root 6 Oct 23 21:25 CBA
-rw-r--r-- 1 root root 0 Oct 23 21:24 abc
drwx------ 2 root root 41 Oct 23 21:26 ansible_command_payload_8kzca8ed
drwx------ 3 root root 17 Oct 23 21:02 systemd-private-bf7fe2a05a0f43fdab39626e122a4453-chronyd.service-tvBXFi
drwx------ 2 root root 6 Oct 23 21:02 vmware-root_894-2730693566
drwx------. 2 root root 6 Oct 23 21:00 vmware-root_903-3979774182
ansible分離部署LNMP
環境說明:
系統 | 主機名 | IP | 服務 |
---|---|---|---|
centos8 | ansible | 192.168.111.141 | ansible主控機 |
centos8 | nginx | 192.168.111.142 | nginx受控機 |
centos8 | mysql | 192.168.111.143 | mysql受控機 |
centos8 | php | 192.168.111.144 | php受控機 |
1.準備工作
修改預設清單文件位置,構建清單
[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=123456
mysql ansible_user=root ansible_password=123456
php ansible_user=root ansible_password=123456
[root@ansible ~]# vim /etc/hosts
192.168.111.142 nginx
192.168.111.143 mysql
192.168.111.144 php
//列出主機
[root@ansible ~]# ansible lnmp --list-hosts
hosts (3):
nginx
mysql
php
//設置密鑰連接
[root@ansible ~]# ssh nginx
[root@nginx ~]# exit
logout
[root@ansible ~]# ssh mysql
[root@mysql ~]# exit
logout
[root@ansible ~]# ssh php
[root@php ~]# exit
logout
[root@ansible ~]#
//測試連通性
[root@ansible ~]# ansible lnmp -m ping
nginx | 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"
}
mysql | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
2.部署nginx
//關閉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 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]# ll
total 4
-rw-r--r-- 1 root root 470 Oct 23 22:04 configure.sh
[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'
[root@ansible ~]# ansible nginx -a 'which nginx'
nginx | CHANGED | rc=0 >>
/usr/local/nginx/sbin/nginx
//啟動服務
[root@ansible ~]# vim /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 ~]# 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 [::]:*
3.部署mysql
//關閉防火牆和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 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'
//配置環境
[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'
[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:24:07.127784Z 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:24:07.286100Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-23T14:24:07.314541Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-23T14:24:07.383098Z 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: 5a8e11ea-52de-11ed-b270-000c29c34b3e.
2022-10-23T14:24:07.383794Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-23T14:24:07.600947Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-23T14:24:07.600960Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-23T14:24:07.601238Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-23T14:24:07.640229Z 1 [Note] A temporary password is generated for root@localhost: y*rou<U9Om.c
[root@ansible ~]# ansible mysql -m shell -a "echo 'y*rou<U9Om.c' > pass"
//生成配置文件啟動服務
[root@ansible ~]# vim /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 ~]# 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 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
4.部署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 /etc/ansible/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 '/etc/ansible/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 /etc/ansible/scripts/php_service.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 '/etc/ansible/scripts/php_service.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'
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 [::]:*
5.配置LNMP界面
//修改nginx配置文件
[root@ansible ~]# vim /etc/ansible/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.111.144: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 '/etc/ansible/scripts/nginxconf.sh'
[root@ansible ~]# ansible nginx -a 'touch /usr/local/nginx/html/index.php'
//在php端上配置網站
[root@ansible ~]# vim /etc/ansible/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 '/etc/ansible/scripts/phpindex.sh'
//修改php配置文件
[root@ansible ~]# ansible php -a 'sed -i "36c listen = 192.168.111.144:9000" /usr/local/php/etc/php-fpm.d/www.conf'
[root@ansible ~]# ansible php -a 'sed -i "63c listen.allowed_clients = 192.168.111.142" /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'