ansible命令简单使用方法

1.shell模块(远程安装vsftpd服务)

ansible all -m shell -a "yum install -y vsftpd"

2.script模块(不用将脚本发到其它机器上也能执行脚本/批量安装vsftpd服务)

ansible all -m script -a "/home/shell/vsftpd.sh"

3.copy模块

a.将本机的hosts文件移到所有机器的/etc目录下

ansible all(host文件里面所有ip) -m copy -a "src=/etc/hosts dest=/etc/"

b.将本机的hosts文件移到所有机器的/etc目录下,修改属主及权限,并对远程机器的源文件进行备份

ansible all -m copy -a "src=/home/shell dest=/home/ owner=zb group=zb mode=777 backup=yes"

3.1.fetch模块

将远程机器中的文件取到当前机器的/tmp目录下,并按照ip保存

ansible all -m fetch -a "src=/etc/hosts dest=/tmp"

4.file模块

a.在远程机器上创建文件,并设置属性,权限

ansible all -m file -a "dest=/etc/hosts owner=oldboy group=oldboy mode=666"    

b.在远程机器上删除文件

 ansible all -m file -a "dest=/etc/hosts state=absent"

c.创建链接文件信息:
 ansible all -m file -a "src=/zb/zb.txt dest=/zb/zb_hard.txt state=hard"         //软连接
 ansible all -m file -a "src=/zb/zb.txt dest=/zb/zb_link.txt state=link"             //硬链接   

d.创建目录

ansible all -m file -a "dest=/home/zb/ state=directory"

e.递归改属主,权限

ansible all -m file -a "dest=/home/shell mode=777 recurse=yes"

5.yum模块

远程批量安装yum包

ansible all -m yum -a "name=iotop state=installed"

    参数说明:

    installed   --- 安装软件
    present
    latest
    absent      --- 卸载软件
    removed

6.service模块

远程启动nfs服务,并开机自启

 ansible all -m service -a "name=nfs state=started enabled=yes"

  参数说明:

    name:   --- 指定管理的服务名称
    state:    --- 指定服务状态
      started            启动
      restarted         重启
      stopped           停止
    enabled --- 指定服务是否开机自启动

7.cron模块

a.给远程机器添加定时对时任务,并增加注释说明

ansible all -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"

b.删除ansible添加的定时任务信息

ansible all -m cron -a "name='time sync01' state=absent"

c.注释ansible添加的定时任务信息

ansible all -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 disabled=yes"

8.mount模块

把99机器上data目录通过nfs方式批量挂载到每台机器上/mnt

ansible all -m mount -a "src=/192.168.6.99:/data path=/mnt fstype=nfs state=mount"

参数说明:

           src:  需要挂载的存储设备或文件信息
           path: 指定目标挂载点目录
           fstype: 指定挂载时的文件系统类型
           state
                   present: 不会实现立即挂载,修改fstab文件,实现开机自动挂载
                   mounted: 会实现立即挂载, 并且会修改fstab文件,实现开机自动挂载 *****
                   absent:     会实现立即卸载, 并且会删除fstab文件信息,禁止开机自动挂载
                   unmounted:  会实现立即卸载, 但是不会会删除fstab文件信息  *****

9. ansible软件输出颜色说明:
    01. 绿色信息:  查看主机信息/对主机未做改动
    02. 黄色信息:  对主机数据信息做了修改
    03. 红色信息:  命令执行出错了
    04. 粉色信息:  忠告信息
    05. 蓝色信息:  显示ansible命令执行的过程???

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值