• ansible常用模块的用法和ansible基于模块方式实现LNMP


    ansible常用模块的用法和ansible基于临时命令方式实现LNMP

    一、ansible常用模块使用详解

    ansible常用模块有:

    • ping
    • yum
    • template
    • copy
    • user
    • group
    • service
    • raw
    • command
    • shell
    • script

    rawcommandshell的区别:

    • shell模块调用的/bin/sh指令执行
    • command模块不是调用的shell的指令,所以没有bash的环境变量
    • raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了

    修改默认清单文件指定路径

    [root@localhost ~]# vim /etc/ansible/ansible.cfg 
    inventory      = /etc/ansible/inventory
    [root@localhost ~]# cd /etc/ansible/
    [root@localhost ansible]# touch inventory
    [root@localhost ansible]# vim inventory
    192.168.183.135 ansible_user=root ansible_password=runtime
    192.168.183.136 ansible_user=root ansible_password=runtime
    192.168.183.137 ansible_user=root ansible_password=runtime
    
    [root@localhost ansible]# ansible all --list-hosts
      hosts (3):
        192.168.183.135
        192.168.183.136
        192.168.183.137
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    1、ping

    ping模块用于检查指定节点机器是否连通,用法简单,不涉及参数,主机如果在线,则回复pong。

    [root@localhost ~]# ansible all -m ping
    192.168.183.136 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "ping": "pong"
    }
    192.168.183.137 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "ping": "pong"
    }
    192.168.183.135 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "ping": "pong"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2、command

    command模块用于在远程主机上执行命令,ansible默认就是使用command模块。

    注意:command模块有一个缺陷就是不能使用管道符和重定向功能。

    //查看受控主机的/tmp目录
    [root@localhost ~]# ansible 192.168.183.135 -a 'ls /tmp'
    192.168.183.135 | CHANGED | rc=0 >>
    ansible_command_payload_t181ftrf
    ks-script-4i88rwq9
    ks-script-m7jrq744
    systemd-private-63c02f80df3d43d3a37bec98b9a03e22-chronyd.service-VjFHIi
    vmware-root_861-3988621786
    vmware-root_894-2730693566
    vmware-root_903-3979774182
    
    //在受控主机的/tmp目录下新建一个文件test
    [root@localhost ~]# ansible 192.168.183.135 -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.183.135 | CHANGED | rc=0 >>
    
    [root@localhost ~]# ansible 192.168.183.135 -a 'ls /tmp'
    192.168.183.135 | CHANGED | rc=0 >>
    ansible_command_payload_3m25p27a
    systemd-private-63c02f80df3d43d3a37bec98b9a03e22-chronyd.service-VjFHIi
    test
    vmware-root_894-2730693566
    
    //command模块不支持管道符、重定向
    [root@localhost ~]# ansible 192.168.183.135 -a "echo 'hello world' > /tmp/test"
    192.168.183.135 | CHANGED | rc=0 >>
    hello world > /tmp/test
    [root@localhost ~]# ansible 192.168.183.135 -a 'cat /tmp/test'
    192.168.183.135 | CHANGED | rc=0 >>
    
    [root@localhost ~]# ansible 192.168.183.135 -a 'ps -ef|grep vsftpd'
    192.168.183.135 | FAILED | rc=1 >>
    error: unsupported SysV option
    
    Usage:
     ps [options]
    
     Try 'ps --help '
      or 'ps --help '
     for additional help text.
    
    For more details see ps(1).non-zero return code
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    3、raw

    raw模块用于在远程主机上执行命令,其支持管道符与重定向。

    //支持重定向
    [root@localhost ~]# ansible 192.168.183.135 -m raw -a "echo 'hello world' > /tmp/test"
    192.168.183.135 | CHANGED | rc=0 >>
    Shared connection to 192.168.183.135 closed.
    
    [root@localhost ~]# ansible 192.168.183.135 -a 'cat /tmp/test'
    192.168.183.135 | CHANGED | rc=0 >>
    hello world
    
    //支持管道符
    [root@localhost ~]# ansible 192.168.183.135 -m raw -a 'cat /tmp/test|grep -Eo hello'
    192.168.183.135 | CHANGED | rc=0 >>
    hello
    Shared connection to 192.168.183.135 closed.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4、shell

    shell模块用于在受控主机上执行受控机上的脚本,也可直接在受控主机上执行命令,shell模块支持管道重定向。

    //查看受控主机上的脚本
    [root@localhost scripts]# ll
    total 4
    -rwxr-xr-x. 1 root root 35 Oct 22 13:32 test.sh
    
    //使用shell模块执行受控机上的脚本
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
    192.168.183.135 | CHANGED | rc=0 >>
    
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'cat /tmp/test'
    192.168.183.135 | CHANGED | rc=0 >>
    hello world!!!
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5、script

    script模块用于在受控机上执行主控机上的脚本

    //在主控机上创建一个脚本
    [root@localhost ~]# mkdir -p /etc/ansible/scripts/
    [root@localhost ~]# cd /etc/ansible/scripts/
    [root@localhost scripts]# vim a.sh
    [root@localhost scripts]# cd
    [root@localhost ~]# ll /etc/ansible/scripts/
    total 4
    -rw-r--r--. 1 root root 35 Oct 22 13:42 a.sh
    
    //执行
    [root@localhost ~]# ansible 192.168.183.135 -m script -a '/etc/ansible/scripts/a.sh &> /tmp/test'
    192.168.183.135 | CHANGED => {
        "changed": true,
        "rc": 0,
        "stderr": "Shared connection to 192.168.183.135 closed.\r\n",
        "stderr_lines": [
            "Shared connection to 192.168.183.135 closed."
        ],
        "stdout": "",
        "stdout_lines": []
    }
    
    //查看受控机上的/tmp/test文件内容
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'cat /tmp/test'
    192.168.183.135 | CHANGED | rc=0 >>
    hello world!!!
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    6、template

    template模块用于生成一个模板,并可将其传输至远程主机上。

    //下载一个yum源并开启此源
    [root@localhost ~]# cd /etc/yum.repos.d/
    [root@localhost yum.repos.d]# curl -o CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
    [root@localhost yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
    [root@localhost yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
    [root@localhost yum.repos.d]# ls
    CentOS7-Base-163.repo  CentOS-Base.repo  CentOS-SIG-ansible-29.repo  ls.repo
    [root@localhost yum.repos.d]# cd
    
    //将设置好的yum源传到受控主机上
    [root@localhost ~]# ansible 192.168.183.135 -m template -a 'src=/etc/yum.repos.d/CentOS7-Base-163.repo dest=/etc/yum.repos.d/163.repo'
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "checksum": "60b8868e0599489038710c45025fc11cbccf35f2",
        "dest": "/etc/yum.repos.d/163.repo",
        "gid": 0,
        "group": "root",
        "md5sum": "5a3e688854d9ceccf327b953dab55b21",
        "mode": "0644",
        "owner": "root",
        "secontext": "system_u:object_r:system_conf_t:s0",
        "size": 1462,
        "src": "/root/.ansible/tmp/ansible-tmp-1666417865.7125297-2262-193472931536666/source",
        "state": "file",
        "uid": 0
    }
    
    //查看受控机上是否存在
    [root@localhost ~]# ls /etc/yum.repos.d/
    163.repo
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    7、yum

    yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个

    • name:要管理的包名
    • state:要进行的操作

    state常用的值:

    • latest:安装软件
    • installed:安装软件
    • present:安装软件
    • removed:卸载软件
    • absent:卸载软件

    若想使用yum来管理软件,请确保受控机上的yum源无异常。

    //在受控机上查看vsftpd软件是否安装
    [root@localhost ~]# rpm -qa | grep vsftpd
    [root@localhost ~]# 
    
    //在ansible主机上使用yum模块在受控机上安装vsftpd
    [root@localhost ~]# ansible 192.168.183.135 -m yum -a 'name=vsftpd state=present'
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "msg": "",
        "rc": 0,
        "results": [
            "Installed: vsftpd-3.0.3-35.el8.x86_64"
        ]
    }
    
    //查看受控机上是否安装了vsftpd
    [root@localhost ~]# rpm -qa | grep vsftpd
    vsftpd-3.0.3-35.el8.x86_64
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    8、copy

    copy模块用于复制文件至远程受控机。

    [root@localhost ~]# ls /etc/ansible/scripts/
    a.sh
    [root@localhost ~]# ansible 192.168.183.135 -m copy -a 'src=/etc/ansible/scripts/a.sh dest=/scripts/'
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "checksum": "b1e8b2373fb842f384955fec281b3693bfcb3fa6",
        "dest": "/scripts/a.sh",
        "gid": 0,
        "group": "root",
        "md5sum": "4f9949a924fcd14ba739da53e5f9aa4c",
        "mode": "0644",
        "owner": "root",
        "secontext": "system_u:object_r:default_t:s0",
        "size": 35,
        "src": "/root/.ansible/tmp/ansible-tmp-1666418759.211907-2370-63524689625588/source",
        "state": "file",
        "uid": 0
    }
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'ls /scripts/'
    192.168.183.135 | CHANGED | rc=0 >>
    a.sh
    test.sh
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    9、group

    group模块用于在受控机上添加或删除组。

    //在受控机上添加一个系统组,其gid为306,组名为mysql
    [root@localhost ~]# ansible 192.168.183.135 -m group -a 'name=mysql gid=306 state=present'
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "gid": 306,
        "name": "mysql",
        "state": "present",
        "system": false
    }
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'grep mysql /etc/group'
    192.168.183.135 | CHANGED | rc=0 >>
    mysql:x:306:
    
    //删除受控机上的mysql组
    [root@localhost ~]# ansible 192.168.183.135 -m group -a 'name=mysql state=absent'
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "name": "mysql",
        "state": "absent"
    }
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'grep mysql /etc/group'
    192.168.183.135 | FAILED | rc=1 >>
    non-zero return code
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    10、user

    user模块用于管理受控机的用户帐号。

    //在受控机上添加一个系统用户,用户名为mysql,uid为306.设置其shell为/sbin/nologin。无家目录
    [root@localhost ~]# ansible 192.168.183.135 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
    192.168.183.135 | 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@localhost ~]# ansible 192.168.183.135 -m shell -a 'grep mysql /etc/passwd'
    192.168.183.135 | CHANGED | rc=0 >>
    mysql:x:306:306::/home/mysql:/sbin/nologin
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'ls /home'
    192.168.183.135 | CHANGED | rc=0 >>
    
    //修改mysql用户的uid为366
    [root@localhost ~]# ansible 192.168.183.135 -m user -a 'name=mysql uid=366'
    192.168.183.135 | 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@localhost ~]# ansible 192.168.183.135 -m shell -a 'grep mysql /etc/passwd'
    192.168.183.135 | CHANGED | rc=0 >>
    mysql:x:366:306::/home/mysql:/sbin/nologin
    
    //删除受控机上的mysql用户
    [root@localhost ~]# ansible 192.168.183.135 -m user -a 'name=mysql state=absent'
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "force": false,
        "name": "mysql",
        "remove": false,
        "state": "absent"
    }
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'grep mysql /etc/passwd'
    192.168.183.135 | FAILED | rc=1 >>
    non-zero return code
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    11、service

    service模块用于管理受控机上的服务。

    //查看受控机上的vsftpd服务是否启动
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'systemctl is-active vsftpd'
    192.168.183.135 | FAILED | rc=3 >>
    inactivenon-zero return code
    
    //启动受控机上的vsftpd服务
    [root@localhost ~]# ansible 192.168.183.135 -m service -a 'name=vsftpd state=started'
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "name": "vsftpd",
        "state": "started",
        "status": {
            "ActiveEnterTimestampMonotonic": "0",
            "ActiveExitTimestampMonotonic": "0",
    ······
    
    //查看受控机上的vsftpd服务是否启动
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'systemctl is-active vsftpd'
    192.168.183.135 | CHANGED | rc=0 >>
    active
    
    //查看受控机上的vsftpd服务是否开机自动启动
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'systemctl is-enabled vsftpd'
    192.168.183.135 | FAILED | rc=1 >>
    disablednon-zero return code
    
    //设置受控机上的vsftpd服务开机自动启动
    [root@localhost ~]# ansible 192.168.183.135 -m service -a 'name=vsftpd enabled=yes'
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "enabled": true,
        "name": "vsftpd",
        "status": {
            "ActiveEnterTimestamp": "Sat 2022-10-22 14:21:50 CST",
    ······
    
    //查看受控机上的vsftpd服务是否开机自动启动
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'systemctl is-enabled vsftpd'
    192.168.183.135 | CHANGED | rc=0 >>
    enabled
    
    //停止受控机上的vsftpd服务
    [root@localhost ~]# ansible 192.168.183.135 -m service -a 'name=vsftpd state=stopped'
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "name": "vsftpd",
        "state": "stopped",
        "status": {
            "ActiveEnterTimestamp": "Sat 2022-10-22 14:21:50 CST",
    ······
    
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'systemctl is-active vsftpd'
    192.168.183.135 | FAILED | rc=3 >>
    inactivenon-zero return code
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'ss -antl'
    192.168.183.135 | 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           [::]:*          
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    12、file

    专门用来设定文件属性。

    force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
    group:定义文件/目录的属组
    mode:定义文件/目录的权限
    owner:定义文件/目录的属主
    path:必选项,定义文件/目录的路径
    recurse:递归的设置文件的属性,只对目录有效
    src:要被链接的源文件的路径,只应用于state=link的情况
    dest:被链接到的路径,只应用于state=link的情况
    
    state:
    =directory:如果目录不存在,创建目录
    =file:即使文件不存在,也不会被创建
    =link:创建软链接
    =hard:创建硬链接
    =touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
    =absent:删除目录、文件或者取消链接文件
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    //创建文件
    [root@localhost ~]# ansible 192.168.183.135 -m file -a "path=/tmp/test state=touch owner=root group=root"
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "dest": "/tmp/test",
        "gid": 0,
        "group": "root",
        "mode": "0644",
        "owner": "root",
        "secontext": "unconfined_u:object_r:user_tmp_t:s0",
        "size": 15,
        "state": "file",
        "uid": 0
    }
    
    //创建目录
    [root@localhost ~]# ansible 192.168.183.135 -m file -a "path=/tmp/www state=directory mode=0755"
    192.168.183.135 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "gid": 0,
        "group": "root",
        "mode": "0755",
        "owner": "root",
        "path": "/tmp/www",
        "secontext": "unconfined_u:object_r:user_tmp_t:s0",
        "size": 6,
        "state": "directory",
        "uid": 0
    }
    
    //查看
    [root@localhost ~]# ansible 192.168.183.135 -m shell -a 'ls -l /tmp'
    192.168.183.135 | CHANGED | rc=0 >>
    total 4
    drwx------. 2 root root 41 Oct 22 14:35 ansible_command_payload_vft3r8x2
    drwx------. 3 root root 17 Oct 22 13:06 systemd-private-63c02f80df3d43d3a37bec98b9a03e22-chronyd.service-VjFHIi
    -rw-r--r--. 1 root root 15 Oct 22 14:30 test
    drwx------. 2 root root  6 Oct 22 13:06 vmware-root_894-2730693566
    drwxr-xr-x. 2 root root  6 Oct 22 14:32 www
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    二、使用ansible临时命令实现LNMP架构

    环境说明

    主机名IP地址应用系统
    ansible192.168.183.138ansible主控机centos8
    nginx192.168.183.135nginx受控机centos8
    mysql192.168.183.136mysql受控机centos8
    php192.168.183.137php受控机centos8

    安装ansible参考文档:常见的自动化运维工具介绍及特点、安装ansible

    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=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"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    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,wget,vim state=present'
    
    //下载软件包并解压
    [root@ansible ~]# ansible nginx -a 'wget http://nginx.org/download/nginx-1.20.2.tar.gz'
    [root@ansible ~]# ansible nginx -a 'tar -xf nginx-1.20.2.tar.gz'
    
    //进入目录编译安装
    [root@ansible ~]# mkdir -p /etc/ansible/scripts/
    [root@ansible ~]# cd /etc/ansible/scripts/
    [root@ansible scripts]# vim configure.sh
    [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           [::]:*          
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95

    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'
    
    //配置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=45790
    2022-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 <<EOF
    [mysqld]
    basedir = /usr/local/mysql
    datadir = /opt/data
    socket = /tmp/mysql.sock
    port = 3306
    pid-file = /opt/data/mysql.pid
    user = mysql
    skip-name-resolve
    EOF
    
    cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
    sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
    chmod +x /etc/init.d/mysqld
    
    cat > /usr/lib/systemd/system/mysqld.service <<EOF
    [Unit]
    Description=mysqld server daemon
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/etc/init.d/mysqld start
    ExecStop=/etc/init.d/mysqld stop
    ExecReload=/bin/kill -HUP \$MAINPID
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    systemctl daemon-reload
    systemctl enable --now mysqld
    
    [root@ansible scripts]# ll
    total 12
    -rw-r--r--. 1 root root 457 Oct 22 16:21 configure.sh
    -rw-r--r--. 1 root root 759 Oct 22 17:12 mysql_service.sh
    -rw-r--r--. 1 root root 364 Oct 22 16:32 nginx_service.sh
    [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           [::]:*      
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    4、部署php

    //关闭防火墙和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 openldap-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel php-mysqlnd libsqlite3x-devel libzip-devel https://dl.rockylinux.org/pub/rocky/9/CRB/x86_64/os/Packages/o/oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm http://mirror.stream.centos.org/9-stream/CRB/x86_64/os/Packages/libzip-devel-1.7.3-7.el9.x86_64.rpm http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm --allowerasing --skip-broken --nobest
    
    #下载软件包解压编译安装
    wget https://www.php.net/distributions/php-8.1.11.tar.gz && \
    tar xf php-8.1.11.tar.gz && \
    cd php-8.1.11
    ./configure --prefix=/usr/local/php8 --with-config-file-path=/etc --enable-fpm --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --enable-exif --enable-ftp --enable-gd --with-jpeg --with-zlib-dir --with-freetype --with-gettext --enable-json --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --with-zip --enable-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posix && \
    
    make && make install
    
    #配置环境变量
    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/php-fpm.d/www.conf
    
    cat > /usr/lib/systemd/system/php.service <<EOF
    [Unit]
    Description=php server daemon
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/etc/init.d/php-fpm start
    ExecStop=/etc/init.d/php-fpm stop
    ExecReload=/bin/kill -HUP $MAINPID
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    systemctl daemon-reload
    systemctl enable --now php
    
    
    [root@ansible scripts]# ll
    total 16
    -rw-r--r--. 1 root root  457 Oct 22 16:21 configure.sh
    -rw-r--r--. 1 root root  759 Oct 22 17:12 mysql_service.sh
    -rw-r--r--. 1 root root  364 Oct 22 16:32 nginx_service.sh
    -rw-r--r--. 1 root root 2498 Oct 22 17:31 php.sh
    [root@ansible ~]# ansible php -m script -a '/etc/ansible/scripts/php.sh'	//编译时间稍长,等待即可
    [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           [::]:*       
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    5、配置php测试页面

    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 >>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    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           [::]:*         
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    6、访问php测试页面

    在这里插入图片描述

  • 相关阅读:
    Spring事务、设计模式以及SpringBoot自动装配原理
    录制完成的长视频如何快速分割为3个相同的视频片段
    计算机毕业设计springboot基于疫情背景下的新型点餐送餐系统bpe1s源码+系统+程序+lw文档+部署
    数据结构之:跳表
    CSS特效002:花样的鼠标悬停效果
    Java EnumMap get()方法具有什么功能呢?
    ELK收集多个docker容器日志
    ChatGPT 1.0.0安卓分析,仅限国内分享
    Twikoo最新私有化部署教程--迁移腾讯云
    萤火虫优化算法(FA)附matlab代码
  • 原文地址:https://blog.csdn.net/qq_65998623/article/details/127465656