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

什么是ansible?

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


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.远程命令模块python

  • 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模块
实现主控制端想目标拷贝文件.相似于scpweb

#将/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等shell

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

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

[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模块
远程主机挂载ssh

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

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

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模块
远程主机系统服务管理ide

[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   #简单查看模块的帮助文档
相关文章
相关标签/搜索