Ansible工具入门

Ansible

优势:

  • 相对于puppet和saltstack,ansible无需客户端,更轻量级。
  • 甚至都不用启动服务
  • 更强的远程命令执行操作
  • 不输于puppet和saltstack的其他优势

安装:

  • 配置好EPEL
  • yum 安装
  • SSH认证

命令:

ansible -i /etc/ansible/hosts test -u root -m command -a 'ls /home' -k
其中 -i , -m 可以省略,-k是输入密码
-i inventory
-m moudle
-u root
命令可以简写为
ansible  webservers -a 'ls /shell'

Inventory格式

连续主机

192.168.0.[81:82:83]

ansible all -m ping

all所有主机

ansible_ssh_user用于指定用于管理远程主机的账号

[mfs:children]
mfs_master
mfs_logger
[mfs_master]
192.168.1.190
[mfs_logger]
192.168.1.191
用于指定模块,和定义子模块

常用模块

ansible-doc -l 查看模块

  • setup 客户端的一些信息

  • ping 检测主机是否存活

  • file

    参数 -a

    • state=link

      ansible mfs_master -m file -a "src=/etc/fstab dest=/tmp/fstab state=link "
      创建/etc/fstab的软链接
    • state=absent

      ansible mfs_master -m file -a "path=/tmp/fstab state=absent"
      删除/tmp/fstab下的文件
    • state=touch

    ansible mfs_master -m file -a "path=/tmp/file1 state=touch"
    创建一个文件
    • state=directory
    ansible mfs_master -m file -a "path=/tmp/d1 state=directory owner=root group=root mode=777"
    创建一个属主,属组都为root,权限是777的一个目录
  • copy

    • “`
      ansible mfs -m copy -a “src=justtest dest=/tmp/file2 mode=755 owner=root group=root”
      其中mode,owner,group可以不写,‘=’号中间不要有空格,双引号单引号都是可以的!
      
      - backup 目标主机有该文件是否做备份
      

    ansible mfs -m copy -a ‘src=justtest dest=/tmp/file4 backup=yes’
    “`

  • command

    • chdir 改变工作路径
    ansible all -a 'chdir=/usr/local tar czf bin.tar.gz bin'      //进入/usr/local目录,压缩
  • shell(支持管道、与raw类似)

  • service

    • name

    • state=started、stopped、restarted

    • sleep=3 #间隔时间

    • runlevel

    • enabled=yes、no

    • arguments

    • network args=eth0

    ansible mfs_master -m service -a 'name=httpd state=started enabled=yes'
    
    #开启httpd服务,并且设置开机自启动
    
  • cron

  • filesystem

  • yum

    • config_file :yum配置文件
    • name:指定软件包的名字
    • state:状态
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值