Ansible工具入门

来源:互联网 发布:知乎的影响力 编辑:程序博客网 时间:2024/06/07 20:24

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_mastermfs_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:状态
原创粉丝点击