码迷,mamicode.com
首页 > 其他好文 > 详细

集中化管理平台Ansible详解(一)

时间:2018-10-27 19:49:46      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:控制   成员   指定   puppet   配置   组合   epel源配置   云计算平台   type   

什么是ansible?

ansible是一种集成IT系统的配置管理、应用部署、执行特定任务的开源平台.它是基于python语言,由Paramiko和PyYAML两个关键模块构建。集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。


ansible的优势

  • 部署简单,只需要在主控端部署Ansible环境,被控端无需做任何操作;
  • 默认使用SSH(Secure SHell)协议对设备进行管理;
  • 主从集中化管理;
  • 配置简单、功能强大、扩展性强;
  • 支持API及自定义模块,可通过Python轻松扩展;
  • 通过Playbooks来定制强大的配置、状态管理;
  • 对云计算平台、大数据都有很好的支持;
  • 提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台;
  • 幂等性:一种操作重复多次结果相同。

ansible的安装和测试

  1. epel源配置
    [epel]  #配置的清华的epel
    name=Fedora EPEL
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/
    gpgcheck=0
  2. yum安装
    yum install ansible -y -q
  3. ansible 配置
    #在ansible的配置文件中添加主机信息,即可与目标主机进行通信,配置文件位置/etc/ansible/hosts,其中,[web][test]为主机组,可以批量控制主机组里面的所有主机,一个主机可以添加到多个组。
    [root@centos7 ~]# /etc/ansible/hosts
    172.18.153.101
    172.18.153.103
    [web]
    172.18.153.101
    172.18.153.103
    [db]
    172.18.153.102
    172.18.153.103
    "/etc/ansible/hosts" 49L, 1092C  
  4. 测试
    [root@centos7 ~]# ansible test --list  #查看用户组的成员
    hosts (2):
    172.18.153.27
    172.18.153.37
    #配置之ssh等效性
    [root@centos7 ~]# ssh-keygen
    [root@centos7 ~]# ssh-copy-id root@172.18.153.101
    [root@centos7 ~]# ssh-copy-id root@172.18.153.102
    [root@centos7 ~]# ssh-copy-id root@172.18.153.103
    [root@centos7 ~]# ansible all -m ping  #测试是否连通,出现pong则说明成功管理
    172.18.153.103 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    172.18.153.102 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    172.18.153.101 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    [root@centos7 ~]# ansible all -m command -a ‘useradd zhangfei‘  #所以主机创建用户-m  comand是使用command模块 -a 添加参数
    172.18.153.103 | CHANGED | rc=0 >>
    172.18.153.101 | CHANGED | rc=0 >>
    172.18.153.102 | CHANGED | rc=0 >>
    [root@centos7 ~]# ansible all -m command -a ‘id zhangfei‘  #成功
    172.18.153.101 | CHANGED | rc=0 >>
    uid=1001(zhangfei) gid=1001(zhangfei) 组=1001(zhangfei)
    172.18.153.103 | CHANGED | rc=0 >>
    uid=1002(zhangfei) gid=1002(zhangfei) 组=1002(zhangfei)
    172.18.153.102 | CHANGED | rc=0 >>
    uid=1001(zhangfei) gid=1001(zhangfei) 组=1001(zhangfei)

ansible的模块使用

1.远程命令模块

  • command :默认的模块,可以运行远程权限范围所有的shell命令
  • script:在远处主机上执行主控制端储存的shell脚本文件,相当于scp+shell组合
  • shell:执行远程主机的shell脚本问文件
[root@centos7 ~]# ansible web -m command -a "free -m"
[root@centos7 ~]# ansible web -m script -a "/root/hello.sh 12 34"
[root@centos7 ~]# ansible web -m shell -a "/root/hello.sh"

2.copy模块
实现主控制端想目标拷贝文件.类似于scp

#将/etc/fstab拷贝到web组目标主机/tmp/下,并更新文件属主和权限
[root@centos7 ~]# ansible web -m copy -a "src=/etc/fstab dest=/tmp/ owner=root group=root mode=0744"

3.stat模块
获取远程文件状态信息,如atime,md5,uid等

[root@centos7 ~]# ansible web -m stat -a "path=/etc/fstab"

4.get_url模块
实现远程主机下载指定的URL到本地,支持sha256sum校验和

[root@centos7 ~]# ansible web -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"

5.yum模块
Linux平台软件包管理模块

[root@centos7 ~]# ansible web -m yum -a "name=curl state=latest"

6.cron模块
远程主机的计划任务配置

[root@centos7 ~]# ansible web -m cron -a ‘minute=* weekday=2,4,6 job="/usr/bin/wall FBI WARNING" name=warningcron‘
[root@centos7 ~]# crontab -l  #去节点机查看效果
 #Ansible: warningcron
 * * * * 2,4,6 /usr/bin/wall FBI WARNING
[root@centos7 ~]# ansible all -m cron -a ‘name=warningcron state=absent‘ #取消
[root@centos7 ~]# ansible all -m cron -a ‘disabled=true job="/usr/bin/wall FBI WARNING" name=warningcron‘  #禁用
[root@centos7 ~]# ansible all -m cron -a ‘disabled=false job="/usr/bin/wall FBI WARNING" name=warningcron‘#启用

7.mount模块
远程主机挂载

[root@centos7 ~]# ansible web -m mount -a "name=/mnt/data dest=/dev/sd0 fstype=ext3 opts=ro state=present"

8.fetch 模块
从受管主机拉取文件

root@centos7 ~]# ansible all -m fetch -a ‘src=/var/log/messages dest=/root/ansible‘
#如果要用fetch或copy传输多个文件,只能先打包
root@centos7 ~]# ansible all -m shell -a ‘tar Jcf /root/log.tar.xz /var/log/*.log‘
root@centos7 ~]# ansible all -m fetch -a ‘src=/root/log.tar.xz dest=/root/ansible‘

9.service模块
远程主机系统服务管理

[root@centos7 ~]# ansible web -m mount -a "name=httpd state=restart"

ansible的模块到现在为止一共2080个,需要自己慢慢摸索,我这里不久多列举了,查看模块的方法

[root@centos7 ~]# ansible-doc -s -l #列出所有模块
[root@centos7 ~]# ansible-doc fetch  #查看详细的模块帮助文档
[root@centos7 ~]# ansible-doc -s fetch   #简单查看模块的帮助文档

集中化管理平台Ansible详解(一)

标签:控制   成员   指定   puppet   配置   组合   epel源配置   云计算平台   type   

原文地址:http://blog.51cto.com/13805636/2309735

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!