• 23 ansible模块2


    Cron模块

    功能:计划任务

    支持时间:minute,hour,day,month,weekday

    • 参数
    参数 解释
    minute 分钟
    hour 小时
    day
    month
    weekday 星期
    job 脚本路径
    name 定时任务名称
    disabled 是否启用
    state 状态:absent删除
    • 范例1
    [root@instance-gvpb80ao ~]# ansible public -m cron -a "minute=* job='echo date >> /root/ttt.log' name=test  disabled=yes"
    106.13.81.75 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "envs": [], 
        "jobs": [
            "test"
        ]
    }
    [root@instance-gvpb80ao ~]# crontab -l
    #Ansible: test
    #* * * * * echo date >> /root/ttt.logCopy to clipboardErrorCopied
    
    • 范例2
    [root@instance-gvpb80ao ~]# ansible public -m cron -a "minute=* job='echo date >> /root/ttt.log' name=test  disabled=yes state=absent"
    106.13.81.75 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "envs": [], 
        "jobs": []
    }
    [root@instance-gvpb80ao ~]# crontab -lCopy to clipboardErrorCopied
    

    Yum和Apt模块

    yum 管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本。

    apt 模块管理 Debian 相关版本的软件包。

    • 参数
    参数 解释
    name 安装的服务的名字
    state 执行命令 present installed removed latest absent 其中installed and present等效 latest标志安装yum中最新版本,absent and removed 等效 表示删除安装包
    disable_gpg_check 用于禁用rmp包的公钥gpg验证,默认值no 表示不做验证
    enablerepo 用于指定安装软件包是临时启用的yum元
    disablerepo 用于指定安装软件包是临时禁止用的yum元
    • 范例1
    [root@instance-gvpb80ao ~]# ansible public -m yum -a "name=httpd state=present"
    106.13.81.75 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "changes": {
            "installed": [
                "httpd"
            ]
        }, 
        "msg": "", 
        "rc": 0, 
        "results": [
            "Loaded plugins: langpacks, versionlock
    Excluding 1 update due to versionlock (use "yum versionlock status" to show it)
    Resolving Dependencies
    --> Running transaction check
    ---> Package httpd.x86_64 0:2.4.6-97.el7.centos will be installed
    --> Processing Dependency: httpd-tools = 2.4.6-97.el7.centos for package: httpd-2.4.6-97.el7.centos.x86_64
    --> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-97.el7.centos.x86_64
    --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.x86_64
    --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.x86_64
    --> Running transaction check
    ---> Package apr.x86_64 0:1.4.8-7.el7 will be installed
    ---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
    ---> Package httpd-tools.x86_64 0:2.4.6-97.el7.centos will be installed
    ---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
     Package           Arch         Version                     Repository     Size
    ================================================================================
    Installing:
     httpd             x86_64       2.4.6-97.el7.centos         updates       2.7 M
    Installing for dependencies:
     apr               x86_64       1.4.8-7.el7                 base          104 k
     apr-util          x86_64       1.5.2-6.el7                 base           92 k
     httpd-tools       x86_64       2.4.6-97.el7.centos         updates        93 k
     mailcap           noarch       2.1.41-2.el7                base           31 k
    
    Transaction Summary
    ================================================================================
    Install  1 Package (+4 Dependent packages)
    
    Total download size: 3.0 M
    Installed size: 10 M
    Downloading packages:
    --------------------------------------------------------------------------------
    Total                                               14 MB/s | 3.0 MB  00:00     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : apr-1.4.8-7.el7.x86_64                                       1/5 
      Installing : apr-util-1.5.2-6.el7.x86_64                                  2/5 
      Installing : httpd-tools-2.4.6-97.el7.centos.x86_64                       3/5 
      Installing : mailcap-2.1.41-2.el7.noarch                                  4/5 
      Installing : httpd-2.4.6-97.el7.centos.x86_64                             5/5 
      Verifying  : httpd-2.4.6-97.el7.centos.x86_64                             1/5 
      Verifying  : apr-1.4.8-7.el7.x86_64                                       2/5 
      Verifying  : mailcap-2.1.41-2.el7.noarch                                  3/5 
      Verifying  : httpd-tools-2.4.6-97.el7.centos.x86_64                       4/5 
      Verifying  : apr-util-1.5.2-6.el7.x86_64                                  5/5 
    
    Installed:
      httpd.x86_64 0:2.4.6-97.el7.centos                                            
    
    Dependency Installed:
      apr.x86_64 0:1.4.8-7.el7                     apr-util.x86_64 0:1.5.2-6.el7    
      httpd-tools.x86_64 0:2.4.6-97.el7.centos     mailcap.noarch 0:2.1.41-2.el7    
    
    Complete!
    "
        ]
    }
    [root@instance-gvpb80ao ~]# systemctl status httpd
    ● httpd.service - The Apache HTTP Server
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
       Active: inactive (dead)
         Docs: man:httpd(8)
               man:apachectl(8)
    [root@instance-gvpb80ao ~]# Copy to clipboardErrorCopied
    

    Service模块

    功能:管理服务。

    • 参数
    参数 解释
    name 定义要启动服务的名称
    state started #启动服务 stopped #停止服务 restarted #重启服务 reloaded #重载服务
    enabled 开机自启
    • 范例
    #1.启动crond服务,并加入开机自启
    [root@m01 ~]# ansible webservers -m service -a "name=crond state=started enabled=yes"
    #2.停止crond服务,并删除开机自启
    [root@m01 ~]# ansible webservers -m service -a "name=crond state=stopped enabled=no"
    #3.重启crond服务
    [root@m01 ~]# ansible webservers -m service -a "name=crond state=restarted"
    #4.重载crond服务
    [root@m01 ~]# ansible webservers -m service -a "name=crond state=reloaded"Copy to clipboardErrorCopied
    

    User模块

    功能:管理用户。

    • 参数
    参数 解释
    uid 指定用户的uid
    group 指定用户组名称
    groups 指定附加组名称
    password 给用户添加密码(记得单引号)
    shell 指定用户登录shell
    create_home 是否创建家目录
    state 用户状态:absent(删除)
    home 家目录路径
    system 是否是系统用户
    remove 是否删除家目录数据:yes(删除)
    • 范例
    [root@instance-gvpb80ao ~]# ansible public -m user -a "name=www uid=1200"
    106.13.81.75 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "comment": "", 
        "create_home": true, 
        "group": 1200, 
        "home": "/home/www", 
        "name": "www", 
        "shell": "/bin/bash", 
        "state": "present", 
        "system": false, 
        "uid": 1200
    }
    [root@instance-gvpb80ao ~]# id www
    uid=1200(www) gid=1200(www) groups=1200(www)Copy to clipboardErrorCopied
    

    Group模块

    功能:管理组。

    #创建组
    ansible public -m group  -a 'name=nginx gid=88 system=yes'
    #删除组
    ansible public -m group  -a 'name=nginx state=absent'Copy to clipboardErrorCopied
    

    get_url模块

    功能:下载模块(类似于wget)。

    • 参数
    参数 解释
    url 下载的URL
    mode 权限
    dest 下载的路径
    checksum md5值
    timeout 下载文件的超时时间
    url_password URL密码
    url_username URL用户
    • 范例
    [root@instance-gvpb80ao ~]# ansible public -m get_url -a 'url=https://repo.huaweicloud.com/etcd/v3.4.0-rc.4/etcd-v3.4.0-rc.4-linux-arm64.tar.gz mode=777 dest=/opt'
    106.13.81.75 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "checksum_dest": null, 
        "checksum_src": "c721f9904ff9c47cb34f304ee7d48eb99253e13a", 
        "dest": "/opt/etcd-v3.4.0-rc.4-linux-arm64.tar.gz", 
        "elapsed": 6, 
        "gid": 0, 
        "group": "root", 
        "md5sum": "0372460bbdf457166391e5e71c074f1c", 
        "mode": "0777", 
        "msg": "OK (15997556 bytes)", 
        "owner": "root", 
        "size": 15997556, 
        "src": "/root/.ansible/tmp/ansible-tmp-1621516877.39-15455-75914400512400/tmpbxzrIQ", 
        "state": "file", 
        "status_code": 200, 
        "uid": 0, 
        "url": "https://repo.huaweicloud.com/etcd/v3.4.0-rc.4/etcd-v3.4.0-rc.4-linux-arm64.tar.gz"
    }Copy to clipboardErrorCopied
    

    mount模块

    功能:挂载目录。

    • 参数
    参数 解释
    src 挂载IP及目录
    path 挂载的路径
    fstype 挂载的类型
    opts
    state 挂载的状态
    • state参数
    参数 解释
    present 开机挂载,仅将挂载配置写入/etc/fstab
    mounted 挂载设备,并将配置写入/etc/fstab
    unmounted 卸载设备,不会清除/etc/fstab写入的配置
    absent 卸载设备,会清理/etc/fstab写入的配置
    • 范例
    [root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=present"
    [root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=mounted"
    [root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=unmounted"
    [root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=absent"Copy to clipboardErrorCopied
    

    Setup模块

    功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度,可以使用 gather_facts: no 来禁止 Ansible 收集 facts 信息。

    • 范例
    ansible all -m setup -a "filter=ansible_nodename"
    ansible all -m setup -a "filter=ansible_hostname"
    ansible all -m setup -a "filter=ansible_domain"
    ansible all -m setup -a "filter=ansible_memtotal_mb"
    ansible all -m setup -a "filter=ansible_memory_mb"
    ansible all -m setup -a "filter=ansible_memfree_mb"
    ansible all -m setup -a "filter=ansible_os_family"
    ansible all -m setup -a "filter=ansible_distribution_major_version"
    ansible all -m setup -a "filter=ansible_distribution_version"
    ansible all -m setup -a "filter=ansible_processor_vcpus"
    ansible all -m setup -a "filter=ansible_all_ipv4_addresses"
    ansible all -m setup -a "filter=ansible_architecture"
    ansible all -m setup -a "filter=ansible_processor*"
    
  • 相关阅读:
    区间贪心问题小结(区间选点,区间覆盖,区间选取)
    Poj-3630(字典树,水题)
    G
    hdu3460(字典树)
    HDU 5512 Pagodas(2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学))
    Python项目之爬取斗图网所有图片
    Python学习笔记
    c重定向函数
    3110: [Zjoi2013]K大数查询
    4826: [Hnoi2017]影魔
  • 原文地址:https://www.cnblogs.com/zhaokunhao/p/14829216.html
Copyright © 2020-2023  润新知