zoukankan      html  css  js  c++  java
  • Ansible命令行方式执行

    Ansible ad-hoc


    什么是ad-hoc?


    临时命令,执行完不会保存,类似于批量执行命令。


    ansible的选项


    -i		# 指定主机清单
    ansible rsync -m ping -i 1.txt
    
    -m		# 指定模块
    -a		# 指定动作
    [root@m01 ~]# ansible nfs_group -a 'df -h'
    [root@m01 ~]# ansible nfs_group -m shell -a 'df -h'
    

    ad-hoc返回的颜色


    • 绿色:被管理的主机没有发生修改
    • 黄色:被控主机发生了变更
    • 粉色:ansible发出的警告
    • 红色:报错

    查看帮助文档


    [root@m01 ~]# ansible-doc shell
    # shell 是模块的名称,想查看什么模块直接在后面接上即可。
    

    ad-hoc模块

    command 模块


    不支持管道符等特殊字符,用于执行系统命令,不仅限于linux。和shell模块差不多。

    [root@m01 ~]# ansible web_group -m shell -a 'df -h'
    [root@m01 ~]# ansible web_group -m command -a 'df -h'
    
    

    shell 模块


    用于执行命令

    [root@m01 ~]# ansible web_group -m shell -a 'df -h'
    

    script 模块


    在本地执行的脚本,功能可以同步到其它机器上面去,被控制机器不需要脚本。

    [root@m01 ~]# ansible web01 -m script -a '/root/a.sh'
    

    yum 模块


    # 查看帮助
    [root@m01 ~]# ansible-doc yum
    
    # 安装httpd
    [root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=latest'
    [root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=present'
    # 一般使用present
    
    # 卸载httpd
    absent
    [root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=absent'
    
    # 指定网络的rpm包
    [root@m01 ~]# ansible web_group -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.1-1.el7.x86_64.rpm state=present'
    
    # 类似于
    yum -y install https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/SRPMS/zabbix-4.4.1-1.el7.src.rpm 
    
    # 在db组安装nginx,前提是db组上的主机有本地安装的包。
    [root@m01 ~]# ansible db_group -m yum -a 'name=/root/nginx-1.18.0-1.el7.ngx.x86_64.rpm state=present'
    

    需要掌握的方法

    name:
    	指定安装的软件。
    	可以是本地的也可以是远程的链接包
    state:
    	prsent	正常安装,类似于yum -y install 
    	absent	删除、卸载
    	lastet	最新版本
    

    yum_repository 模块


    [root@m01 ~]# ansible-doc yum_repository
    
    [root@m01 ~]# ansible web_group -m yum_repository -a 'name=gong description=gong baseurl=zp.gong.com gpgcheck=0 enabled=no'
    
    # web01生成的文件
    [root@web01 ~]# cat /etc/yum.repos.d/gong.repo 
    [gong]
    baseurl = zp.gong.com
    enabled = 0
    gpgcheck = 0
    name = gong
    
    # file指的是yum仓库的文件名,gong是[ ] 里面的内容,des是name的内容
    [root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=gong description=gong baseurl=zp.gong.com gpgcheck=0 enabled=no'
    
    # web01生成的文件
    [root@web01 ~]# cat /etc/yum.repos.d/nginx.repo 
    [gong]
    baseurl = zp.gong.com
    enabled = 0
    gpgcheck = 0
    name = gong
    
    # 删除yum仓库文件,最好是把文件名和仓库名都加上,防止误删。
    [root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=gong state=absent'
    
    # 直接在文件里面添加仓库,file不变,其它参数改变就会加上
    [root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=liao description=liao baseurl=tencent.gong.com gpgcheck=0 enabled=yes'
    
    
    [root@web01 /etc/yum.repos.d]# cat nginx.repo 
    [xiao]
    baseurl = qq.gong.com
    enabled = 1
    gpgcheck = 0
    name = xiao
    
    [liao]
    baseurl = tencent.gong.com
    enabled = 1
    gpgcheck = 0
    name = liao
    
    
    name        #指定仓库名,如果没有file,则仓库名为文件名
    baseurl     #指定yum源
    description   # yum配置文件中的name
    gpgcheck    #指定检查秘钥,也可用数字
        no
        yes
    
    enabled     #是否启用仓库
        no
        yes
    


    FBI WARNING
    QQ:1402122292 认准原创sheldon 别人叫我晓东
    欢迎访问个人站点:shelldon.51vip.biz
  • 相关阅读:
    如何:为 Silverlight 客户端生成双工服务
    Microsoft Sync Framework 2.1 软件开发包 (SDK)
    Windows 下的安装phpMoAdmin
    asp.net安全检测工具 Padding Oracle 检测
    HTTP Basic Authentication for RESTFul Service
    Windows系统性能分析
    Windows Server AppFabric Management Pack for Operations Manager 2007
    Mongo Database 性能优化
    服务器未能识别 HTTP 标头 SOAPAction 的值
    TCP WAIT状态及其对繁忙的服务器的影响
  • 原文地址:https://www.cnblogs.com/gshelldon/p/13374662.html
Copyright © 2011-2022 走看看