ansible常用模塊的介紹與使用 ansible常用模塊有: ping yum template copy user group service raw command shell script ansible常用模塊raw、command、shell的區別: shell模塊調用的/bin/sh指 ...
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模塊了
ansible常用模塊之ping
ping模塊用於檢查指定節點機器是否連通,用法很簡單,不涉及參數,主機如果線上,則回覆pong
[root@ansible ~]# ansible all -m ping
192.168.118.130 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.118.131 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.118.131 port 22: No route to host",
"unreachable": true
}
#沒連通
ansible常用模塊之command
command模塊用於在遠程主機上執行命令,ansible預設就是使用command模塊。
command模塊有一個缺陷就是不能使用管道符和重定向功能。
//查看受控主機的/tmp目錄內容
[root@ansible ~]# ansible 192.168.118.130 -a 'ls /tmp'
192.168.118.130 | 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.118.130 -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.118.130 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.118.130 -a 'ls /tmp'
192.168.118.130 | 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.118.130 -a "echo 'hello world' > /tmp/test"
192.168.118.130 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@ansible ~]# ansible 192.168.118.130 -a 'cat /tmp/test'
192.168.118.130 | CHANGED | rc=0 >>
[root@ansible ~]# ansible 192.168.118.130 -a 'ps -ef|grep vsftpd'
192.168.118.130 | 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.118.130 -m raw -a "echo 'hello world' > /tmp/test"
192.168.118.130 | CHANGED | rc=0 >>
Shared connection to 192.168.118.130 closed.
[root@ansible ~]# ansible 192.168.118.130 -a 'cat /tmp/test'
192.168.118.130 | CHANGED | rc=0 >>
hello world
//支持管道符
[root@ansible ~]# ansible 192.168.118.130 -m raw -a 'cat /tmp/test|grep -Eo hello'
192.168.118.130 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.118.130 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.118.130 -m shell -a '/bin/bash /scripts/test.sh'
192.168.118.130 | 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.118.130 -m script -a '/etc/ansible/scripts/a.sh'
192.168.118.130 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.118.130 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.118.130 closed."
],
"stdout": "hello world\r\n",
"stdout_lines": [
"hello world"
]
}
ansible常用模塊之template
template模塊用於生成一個模板,並可將其傳輸至遠程主機上。
//將根目錄下的anaconda-ks.cfg傳送到受控主機下的/tmp/下,並賦予775的許可權
[root@ansible ~]# ansible 192.168.118.130 -m template -a 'src=~/anaconda-ks.cfg dest=/tmp/ mode=0775'
192.168.118.130 | 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.118.130 -a 'ls -l /tmp/'
192.168.118.130 | 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.118.130 -m copy -a 'src=/etc/ansible/scripts/a.sh dest=/scripts/'
192.168.118.130 | 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.118.130 -a 'ls /scripts/'192.168.118.130 | CHANGED | rc=0 >>
a.sh
test.sh
ansible常用模塊之group
group模塊用於在受控機上添加或刪除組
//創建一個runtime組gid為255
[root@ansible ~]# ansible 192.168.118.130 -m group -a 'name=runtime gid=255 state=present'
192.168.118.130 | 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.118.130 -a 'grep runtime /etc/group'192.168.118.130 | CHANGED | rc=0 >>
runtime:x:255:
//刪除受控機上的runtime組
[root@ansible ~]# ansible 192.168.118.130 -m group -a 'name=runtime state=absent'
192.168.118.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "runtime",
"state": "absent"
}
[root@ansible ~]# ansible 192.168.118.130 -a 'grep runtime /etc/group'
192.168.118.130 | FAILED | rc=1 >>
non-zero return code
ansible常用模塊之user
user模塊用於管理受控機的用戶帳號。
//在受控機上添加一個系統用戶,用戶名為mysql,uid為306,設置其shell為/sbin/nologin,無家目錄
[root@ansible ~]# ansible 192.168.118.130 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.118.130 | 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.118.130 -a 'grep mysql /etc/passwd'
192.168.118.130 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
//修改mysql用戶的uid為366
[root@ansible ~]# ansible 192.168.118.130 -m user -a 'name=mysql uid=366'
192.168.118.130 | 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.118.130 -a 'grep mysql /etc/passwd'192.168.118.130 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
//刪除受控機上的mysql用戶
[root@ansible ~]# ansible 192.168.118.130 -m user -a 'name=mysql state=absent'
192.168.118.130 | 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.118.130 -a 'grep mysql /etc/passwd'
192.168.118.130 | 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.118.130 -m yum -a 'name=httpd state=present'
192.168.118.130 | 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.118.130 -m yum -a 'name=httpd state=absent'
192.168.118.130 | 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.118.130 -m service -a 'name=httpd state=started'
192.168.118.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started",
...
//查看受控機上的httpd服務是否啟動
[root@ansible ~]# ansible 192.168.118.130 -a 'systemctl is-active httpd'
192.168.118.130 | CHANGED | rc=0 >>
active
//設置受控機上的vsftpd服務開機自動啟動
[root@ansible ~]# ansible 192.168.118.130 -m service -a 'name=httpd enabled=yes'
192.168.118.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "httpd",
...
//查看受控機上的httpd服務是否開機自啟
[root@ansible ~]# ansible 192.168.118.130 -a 'systemctl is-enabled httpd'
192.168.118.130 | CHANGED | rc=0 >>
enabled
//停止受控機上的httpd服務
[root@ansible ~]# ansible 192.168.118.130 -m service -a 'name=httpd state=stopped'
192.168.118.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "stopped",
...
//查看受控機上的httpd服務是否啟動
[root@ansible ~]# ansible 192.168.118.130 -a 'systemctl is-active httpd'
192.168.118.130 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ~]# ansible 192.168.118.130 -a 'ss -antl'
192.168.118.130 | 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 [::]:*
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,可以遞歸的修改目錄中文件的屬性。
//在受控主機上根目錄中創建scripts目錄
[root@ansible ~]# ansible 192.168.118.130 -m file -a 'path=/scripts state=directory'
192.168.118.130 | 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.118.130 -m file -a 'path=/scripts/test state=touch'192.168.118.130 | 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.118.129 | ansible(已安裝) | centos8 |
受控主機 | nginx | 192.168.118.130 | nginx | centos8 |
受控主機 | mysql | 192.168.118.131 | mysql | centos8 |
受控主機 | php | 192.168.118.132 | php | centos8 |
配置清單
//修改配置文件設置清單位置
[root@ansible ~]# cd /etc/ansible/
[root@ansible ~]# vim ansible.cfg
inventory = /etc/ansible/inventory
//配置清單
[root@ansible ~]# vim inventory
[nginx]
192.168.118.130
[mysql]
192.168.118.131
[php]
192.168.118.132
受管主機安裝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.118.130 | 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.118.132: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.118.131 | 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.118.131 | 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
#!/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.118.132 | 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.118.132:9000" /usr/local/php/etc/php-fpm.d/www.conf'
[root@ansible ~]# ansible php -a 'sed -i "63c listen.allowed_clients = 192.168.118.130" /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'
瀏覽器訪問查看