美文网首页
第41课 ansible服务(模块用法)2019-05-28

第41课 ansible服务(模块用法)2019-05-28

作者: 苏水的北 | 来源:发表于2019-05-28 23:25 被阅读0次
    第十周 day2.png

    一、ansible的应用:

    1、批量命令执行;
    2、批量安装服务;
    3、批量配置同步;
    4、批量任务执行;
    5、批量代码部署。
    二、ansible优势:
    1、ansible不需要单独安装客户端,也不需要启动任何服务;
    2、ansible是python中的一套完整的自动化执行任务模块;
    三、ansible的安装:
    yum install -y ansible
    四、ansible主机清单(ansible inventory)

    [root@m01 ~]# vim /etc/ansible/hosts (清单)
    ## db-[99:101]-node.example.com
    [oldboy]
    172.16.1.7
    172.16.1.41
    

    五、ad-hoc模块的几种使用方法:
    ad-hoc简而言之就是“临时命令”,不会保存


    QQ图片20190528122333.png

    1、command命令模块:

    [root@m01 .ssh]# ansible all -a hostname
    172.16.1.41 | CHANGED | rc=0 >>
    backup
    
    172.16.1.7 | CHANGED | rc=0 >>
    web01
    

    2、copy文件拷贝模块:

    [root@m01 /]# ansible all -m copy -a 'src=/etc/hostname  dest=/tmp/'
    172.16.1.7 | CHANGED => {
       "ansible_facts": {
           "discovered_interpreter_python": "/usr/bin/python"
       }, 
       "changed": true, 
       "checksum": "f434396716e2c9aed47cfde87c491cce5a2c08fa", 
       "dest": "/tmp/hostname", 
       "gid": 0, 
       "group": "root", 
       "md5sum": "318d7defb693a2eb0d4f1a7a96575a57", 
       "mode": "0644", 
       "owner": "root", 
       "size": 4, 
       "src": "/root/.ansible/tmp/ansible-tmp-1559018281.86-28366237641950/source", 
       "state": "file", 
       "uid": 0
    }
    172.16.1.41 | SUCCESS => {
       "ansible_facts": {
           "discovered_interpreter_python": "/usr/bin/python"
       }, 
       "changed": false, 
       "checksum": "f434396716e2c9aed47cfde87c491cce5a2c08fa", 
       "dest": "/tmp/hostname", 
       "gid": 0, 
       "group": "root", 
       "mode": "0644", 
       "owner": "root", 
       "path": "/tmp/hostname", 
       "size": 4, 
       "state": "file", 
       "uid": 0
    }
    

    3、script脚本模块:

    [root@m01 ~]# ansible all -m script -a "/server/scripts/1.sh"
    172.16.1.7 | CHANGED => {
        "changed": true, 
        "rc": 0, 
        "stderr": "Shared connection to 172.16.1.7 closed.\r\n", 
        "stderr_lines": [
            "Shared connection to 172.16.1.7 closed."
        ], 
        "stdout": "Loaded plugins: fastestmirror\r\nLoading mirror speeds from cached hostfile\r\n * base: mirrors.nwsuaf.edu.cn\r\n * extras: mirrors.tuna.tsinghua.edu.cn\r\n * updates: mirrors.tuna.tsinghua.edu.cn\r\nResolving Dependencies\r\n--> Running transaction check\r\n---> Package cowsay.noarch 0:3.04-4.el7 will be installed\r\n--> Finished Dependency Resolution\r\n\r\nDependencies Resolved\r\n\r\n================================================================================\r\n Package          Arch             Version                 Repository      Size\r\n================================================================================\r\nInstalling:\r\n cowsay           noarch           3.04-4.el7              epel            42 k\r\n\r\nTransaction Summary\r\n================================================================================\r\nInstall  1 Package\r\n\r\nTotal download size: 42 k\r\nInstalled size: 77 k\r\nDownloading packages:\r\n\rcowsay-3.04-4.el7.noarch.rpm                               |  42 kB   00:00     \r\nRunning transaction check\r\nRunning transaction test\r\nTransaction test succeeded\r\nRunning transaction\r\n\r  Installing : cowsay-3.04-4.el7.noarch [                                 ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#                                ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [##                               ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [####                             ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#####                            ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [######                           ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#######                          ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [########                         ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#########                        ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [##########                       ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [###########                      ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [############                     ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#############                    ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [##############                   ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [###############                  ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [################                 ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#################                ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [##################               ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [###############################  ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [################################ ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch                                     1/1 \r\n\r  Verifying  : cowsay-3.04-4.el7.noarch                                     1/1 \r\n\r\nInstalled:\r\n  cowsay.noarch 0:3.04-4.el7                                                    \r\n\r\nComplete!\r\n", 
        "stdout_lines": [
            "Loaded plugins: fastestmirror", 
            "Loading mirror speeds from cached hostfile", 
            " * base: mirrors.nwsuaf.edu.cn", 
            " * extras: mirrors.tuna.tsinghua.edu.cn", 
            " * updates: mirrors.tuna.tsinghua.edu.cn", 
            "Resolving Dependencies", 
            "--> Running transaction check", 
            "---> Package cowsay.noarch 0:3.04-4.el7 will be installed", 
            "--> Finished Dependency Resolution", 
            "", 
            "Dependencies Resolved", 
            "", 
            "================================================================================", 
            " Package          Arch             Version                 Repository      Size", 
            "================================================================================", 
            "Installing:", 
            " cowsay           noarch           3.04-4.el7              epel            42 k", 
            "", 
            "Transaction Summary", 
            "================================================================================", 
            "Install  1 Package", 
            "", 
            "Total download size: 42 k", 
            "Installed size: 77 k", 
            "Downloading packages:", 
            "", 
            "cowsay-3.04-4.el7.noarch.rpm                               |  42 kB   00:00     ", 
            "Running transaction check", 
            "Running transaction test", 
            "Transaction test succeeded", 
            "Running transaction", 
            "", 
            "  Installing : cowsay-3.04-4.el7.noarch [                                 ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#                                ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [##                               ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [####                             ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#####                            ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [######                           ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#######                          ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [########                         ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#########                        ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [##########                       ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [###########                      ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [############                     ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#############                    ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [##############                   ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [###############                  ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [################                 ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#################                ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [##################               ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [###############################  ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [################################ ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch                                     1/1 ", 
            "", 
            "  Verifying  : cowsay-3.04-4.el7.noarch                                     1/1 ", 
            "", 
            "Installed:", 
            "  cowsay.noarch 0:3.04-4.el7                                                    ", 
            "", 
            "Complete!"
        ]
    }
    172.16.1.41 | CHANGED => {
        "changed": true, 
        "rc": 0, 
        "stderr": "Shared connection to 172.16.1.41 closed.\r\n", 
        "stderr_lines": [
            "Shared connection to 172.16.1.41 closed."
        ], 
        "stdout": "Loaded plugins: fastestmirror\r\nLoading mirror speeds from cached hostfile\r\n * base: mirrors.aliyun.com\r\n * extras: mirrors.aliyun.com\r\n * updates: mirrors.aliyun.com\r\nResolving Dependencies\r\n--> Running transaction check\r\n---> Package cowsay.noarch 0:3.04-4.el7 will be installed\r\n--> Finished Dependency Resolution\r\n\r\nDependencies Resolved\r\n\r\n================================================================================\r\n Package          Arch             Version                 Repository      Size\r\n================================================================================\r\nInstalling:\r\n cowsay           noarch           3.04-4.el7              epel            42 k\r\n\r\nTransaction Summary\r\n================================================================================\r\nInstall  1 Package\r\n\r\nTotal download size: 42 k\r\nInstalled size: 77 k\r\nDownloading packages:\r\n\rcowsay-3.04-4.el7.noarch.r 0% [                 ]  0.0 B/s |    0 B   --:-- ETA \r\rcowsay-3.04-4.el7.noarch.rpm                               |  42 kB   00:00     \r\nRunning transaction check\r\nRunning transaction test\r\nTransaction test succeeded\r\nRunning transaction\r\n\r  Installing : cowsay-3.04-4.el7.noarch [                                 ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#                                ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [##                               ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [####                             ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#####                            ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [######                           ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#######                          ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [########                         ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#########                        ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [##########                       ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [###########                      ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [############                     ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#############                    ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [##############                   ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [###############                  ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [################                 ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [#################                ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [##################               ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [###############################  ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch [################################ ] 1/1\r  Installing : cowsay-3.04-4.el7.noarch                                     1/1 \r\n\r  Verifying  : cowsay-3.04-4.el7.noarch                                     1/1 \r\n\r\nInstalled:\r\n  cowsay.noarch 0:3.04-4.el7                                                    \r\n\r\nComplete!\r\n", 
        "stdout_lines": [
            "Loaded plugins: fastestmirror", 
            "Loading mirror speeds from cached hostfile", 
            " * base: mirrors.aliyun.com", 
            " * extras: mirrors.aliyun.com", 
            " * updates: mirrors.aliyun.com", 
            "Resolving Dependencies", 
            "--> Running transaction check", 
            "---> Package cowsay.noarch 0:3.04-4.el7 will be installed", 
            "--> Finished Dependency Resolution", 
            "", 
            "Dependencies Resolved", 
            "", 
            "================================================================================", 
            " Package          Arch             Version                 Repository      Size", 
            "================================================================================", 
            "Installing:", 
            " cowsay           noarch           3.04-4.el7              epel            42 k", 
            "", 
            "Transaction Summary", 
            "================================================================================", 
            "Install  1 Package", 
            "", 
            "Total download size: 42 k", 
            "Installed size: 77 k", 
            "Downloading packages:", 
            "", 
            "cowsay-3.04-4.el7.noarch.r 0% [                 ]  0.0 B/s |    0 B   --:-- ETA ", 
            "", 
            "cowsay-3.04-4.el7.noarch.rpm                               |  42 kB   00:00     ", 
            "Running transaction check", 
            "Running transaction test", 
            "Transaction test succeeded", 
            "Running transaction", 
            "", 
            "  Installing : cowsay-3.04-4.el7.noarch [                                 ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#                                ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [##                               ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [####                             ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#####                            ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [######                           ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#######                          ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [########                         ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#########                        ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [##########                       ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [###########                      ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [############                     ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#############                    ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [##############                   ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [###############                  ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [################                 ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [#################                ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [##################               ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [###############################  ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch [################################ ] 1/1", 
            "  Installing : cowsay-3.04-4.el7.noarch                                     1/1 ", 
            "", 
            "  Verifying  : cowsay-3.04-4.el7.noarch                                     1/1 ", 
            "", 
            "Installed:", 
            "  cowsay.noarch 0:3.04-4.el7                                                    ", 
            "", 
            "Complete!"
        ]
    }
    

    4、yum安装软件模块:

    [root@m01 ~]#   ansible all   -m yum  -a 'name=sl state=present'
    

    5、file文件配置模块:

    相当于mkdir)
    [root@m01 ~]# ansible all -m file  -a 'path=/tmp/a/b/c/d/e/f/g   state=directory '
    172.16.1.7 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "gid": 0, 
        "group": "root", 
        "mode": "0755", 
        "owner": "root", 
        "path": "/tmp/a/b/c/d/e/f/g", 
        "size": 6, 
        "state": "directory", 
        "uid": 0
    }
    172.16.1.41 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "gid": 0, 
        "group": "root", 
        "mode": "0755", 
        "owner": "root", 
        "path": "/tmp/a/b/c/d/e/f/g", 
        "size": 6, 
        "state": "directory", 
        "uid": 0
    }
    相当于touch
    [root@m01 ~]# ansible all -m file  -a 'path=/tmp/a/b/c/d/e/f/g/oldboy.txt   state=touch '
    172.16.1.41 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "dest": "/tmp/a/b/c/d/e/f/g/oldboy.txt", 
        "gid": 0, 
        "group": "root", 
        "mode": "0644", 
        "owner": "root", 
        "size": 0, 
        "state": "file", 
        "uid": 0
    }
    172.16.1.7 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "dest": "/tmp/a/b/c/d/e/f/g/oldboy.txt", 
        "gid": 0, 
        "group": "root", 
        "mode": "0644", 
        "owner": "root", 
        "size": 0, 
        "state": "file", 
        "uid": 0
    }
    检查
    [root@m01 ~]# ansible all  -a 'tree  /tmp/ '
    172.16.1.41 | CHANGED | rc=0 >>
    /tmp/
    ├── a
    │   └── b
    │       └── c
    │           └── d
    │               └── e
    │                   └── f
    │                       └── g
    │                           └── oldboy.txt
    ├── ansible_command_payload_PktBE1
    │   ├── ansible_command_payload.zip
    │   ├── __main__.py
    │   └── __main__.pyc
    ├── etc
    │   ├── gshadow
    │   ├── gshadow-
    │   ├── ld.so.conf.d
    │   │   └── kernel-3.10.0-957.el7.x86_64.conf
    │   ├── machine-id
    │   ├── mtab
    │   ├── openldap
    │   │   └── certs
    │   │       └── password
    │   ├── pki
    │   │   ├── ca-trust
    │   │   │   └── extracted
    │   │   │       ├── java
    │   │   │       │   └── cacerts
    │   │   │       ├── openssl
    │   │   │       │   └── ca-bundle.trust.crt
    │   │   │       └── pem
    │   │   │           ├── email-ca-bundle.pem
    │   │   │           ├── objsign-ca-bundle.pem
    │   │   │           └── tls-ca-bundle.pem
    │   │   ├── java
    │   │   │   └── cacerts
    │   │   └── tls
    │   │       ├── cert.pem
    │   │       └── certs
    │   │           ├── ca-bundle.crt
    │   │           └── ca-bundle.trust.crt
    │   ├── shadow
    │   ├── shadow-
    │   ├── ssl
    │   │   └── certs
    │   │       ├── ca-bundle.crt
    │   │       └── ca-bundle.trust.crt
    │   ├── sudoers
    │   └── udev
    │       └── hwdb.bin
    ├── hostname
    ├── systemd-private-b29d7e55db3b423497a16f5c18e6f035-chronyd.service-NEBFWj
    │   └── tmp
    ├── systemd-private-caf3d736f056411a9dbbc354c8aa03a1-chronyd.service-cncaY4
    │   └── tmp
    ├── vmware-root_6331-1723602126
    ├── vmware-root_6388-994818425
    ├── vmware-root_6446-994097477
    └── vmware-root_6468-701206161
    
    32 directories, 26 files
    
    172.16.1.7 | CHANGED | rc=0 >>
    /tmp/
    ├── a
    │   └── b
    │       └── c
    │           └── d
    │               └── e
    │                   └── f
    │                       └── g
    │                           └── oldboy.txt
    ├── ansible_command_payload_5AgcRS
    │   ├── ansible_command_payload.zip
    │   ├── __main__.py
    │   └── __main__.pyc
    ├── hostname
    ├── systemd-private-673a0eb7b64d4c90af717f54294c46ac-chronyd.service-iZgzgp
    │   └── tmp
    ├── systemd-private-deab5f8bc3eb42e6a003febfdbc07473-chronyd.service-jFqYZF
    │   └── tmp
    ├── vmware-root_6262-734693435
    ├── vmware-root_6334-701009557
    ├── vmware-root_6405-1983653167
    ├── vmware-root_6560-692096908
    └── vmware-root_6874-3099737545
    
    17 directories, 5 files
    

    6、server 服务模块:

    [root@m01 ~]#   ansible all   -m server   -a 'name=crond state=started  enabled=yes' 
    (开启cron定时服务,永久开启)
    

    7、cron 定时任务服务模块:

    [root@m01 ~]#ansible all -m cron -a 'name="sync time"(为了防止写入多个定时任务,用作注释,和别的服务name名称不相同) minute="*/5" job="/sbin/ntpdate  ntp1.aliyun.com  >/dev/null 2>&1"'
    

    8、mount 挂载模块:

    #web01 把nfs01 共享的/nfs  挂载到 /upload 
    
    [root@m01 ~]# ansible 172.16.1.7 -m  mount -a 'fstype=nfs src=172.16.1.31:/nfs path=/upload state=mounted'
    172.16.1.7 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "dump": "0", 
        "fstab": "/etc/fstab", 
        "fstype": "nfs", 
        "name": "/upload", 
        "opts": "defaults", 
        "passno": "0", 
        "src": "172.16.1.31:/nfs"
    }
    [root@m01 ~]# ansible 172.16.1.7 -a 'df -h'
    172.16.1.7 | CHANGED | rc=0 >>
    Filesystem        Size  Used Avail Use% Mounted on
    
    172.16.1.31:/nfs   99G  1.7G   98G   2% /upload
    
    [root@m01 ~]# ansible 172.16.1.7 -a 'grep upload /etc/fstab'
    172.16.1.7 | CHANGED | rc=0 >>
    172.16.1.31:/nfs /upload nfs defaults 0 0
    

    9、压缩模块:

    [root@m01 ~]# ansible all -m archive -a  ' path=/etc dest=/backup/lidao/etc.tar.gz '
    path 指定远程主机目录或文件信息(要压缩的源文件)
    dest 要解压推送到的目标路径
    

    10、解压模块:

    [root@m01 ~]#ansible all -m unarchive -a 'src=/backup/lidao/etc.tar.gz dest=/usr/local copy=no mode=0755'
    src  推送数据的源文件信息,加copy=no,指远端源文件(要进行解压的源文件)
    dest 远端推送数据的目标路径(要解压到的目标路径)
    copy=0 指远端源文件
    mode  修改文件权限
    

    相关文章

      网友评论

          本文标题:第41课 ansible服务(模块用法)2019-05-28

          本文链接:https://www.haomeiwen.com/subject/wfmjtctx.html