Ansible基础详解

一、概述
1.1 什么是ansible
ansible简介:
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
1.连接插件connection plugs:负责和被监控实现通信
2.host inventory :指定操作的主机 是一个配置文件里面监控的主机
3.各种模块核心模块,command模块,自定义模块
4.借助于插件完成记录日志等功能
5.playbock,剧本执行多个任务时

1.2、Ansible能做什么
注:ansible可以帮助我们完成一些批量任务,或者完成一些需要经常重复的工作。
1)比如:同时在100台服务器上安装nfs服务,并在安装后启动服务。
2)比如:将某个文件一次性拷贝到100台服务器上。
3)比如:每当有新服务器加入工作环境时,你都要为新服务器部署某个服务,也就是说你需要经常重复的完成相同的工作。
普通方式流程:购买机器->2.配置环境->3.部署代码->4测试->5.加入集群

1.3、Ansible软件特点
ansible不需要单独安装客户端,SSH相当于ansible客户端。
ansible不需要启动任何服务,仅需安装对应工具即可。
ansible依赖大量的python模块来实现批量管理。
ansible配置文件/etc/ansible/ansible.cfg
有paramiko(Python开发出来基于SSH服务的远程通讯模块), pyYAML(playbook基于此模块实现的),
jinja2(模板语言)三个关键模块
可使用任何编程语言写模块,YAML格式,编排任务,支持丰富的数据结构

1.4.Ansible的优势
1.轻量级,无需在客户端安装agent,更新时只需在操作机上进行一次更新即可。
2.批量任务执行可以写成脚本,而且不用分发到远程就可以执行。
3.使用Python编写,维护更简单。
4.支持sudo,
5.Ansible社区非常活跃。Ansible本身提供的模块非常丰富,第三方资源多。

1.5.ansible程序目录结构:
配置文件: /etc/ansible/
执行文件目录: /usr/bin/
lib依赖库: /usr/lib/python2.7/site-packages/ansible/
help文件: /usr/lib/python2.7/site-packages/ansible

1.6.Ansible工作原理:
管理端支持local 、ssh、zeromq 三种方式连接被管理端,默认使用基于ssh的连接
可以按应用类型等方式进行Host Inventory(主机群)分类,管理节点通过各类模块实现相应的操作
管理节点可以通过playbooks 实现多个task的集合实现一类功能,如web服务的安装部署、数据库服务器的批量备份等。playbooks我们可以简单的理解为,系统通过组合多条ad-hoc操作的配置文件 。

二 部署流程

1.安装依赖环境

root@master ~]# yum -y install epel-release    
[root@master ~]# yum -y install ansible         

2.在hosts文件里添加需要管理的主机列表

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
[webserver]
192.168.17.20
[mysql]
192.168.17.30

ansible是基于ssh实现的,所以公钥授权给需要管理的主机节点

[root@master ~]# ssh-keygen -t rsa   ##回车
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 

配置密钥对

[root@master ~]# ssh-copy-id root@192.168.17.20
[root@master ~]# ssh-copy-id root@192.168.17.30

配置免密登录

[root@master ~]# ssh-agent bash
[root@master ~]# ssh-add

3.常用模块
语法: ansible 主机标识(IP) -m 模块 -a ‘参数’
3.1 command模块:
查看当前日期

[root@master ~]# ansible all -m command -a 'date'

command模块为默认的模块,如果不-m指定的话,默认使用的command模块,command不支持重定向符号

3.2 cron模块
添加定时任务

[root@master ~]# ansible webserver -m cron -a 'minute="*/1" job="/usr/bin/echo heihei >>/opt/info.txt" name="test_hello" '

查看任务

[root@master ~]# ansible webserver -a 'crontab -l'

移除任务

[root@master ~]# ansible webserver -m cron -a 'name="test_hello" state=absent'

3.3user模块
管理用户,主要是useradd,usermod,userdel三个指令
创建用户

[root@master ~]# ansible all -m user -a 'name=lisi' 

删除创建的用户

ansible all -m user -a 'name=lisi state=absent'

3.4 group模块
group模块请求的是groupadd, groupdel, groupmod三个指令

[root@master ~]# ansible all -m  group -a 'name=lisi gid=200 system=yes'
[root@master ~]# ansible all -a 'id lisi'     
192.168.17.20 | CHANGED | rc=0 >>
uid=1002(lisi) gid=200(lisi)=200(lisi)
192.168.17.30 | CHANGED | rc=0 >>
uid=1001(lisi) gid=200(lisi)=200(lisi)
[root@node01 ~]# id lisi                      
uid=1001(lisi) gid=200(lisi)=200(lisi)

3.5 copy模块

[root@master ~]# ansible all -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640'
[root@master ~]# ansible all -a 'ls -l /opt'
192.168.17.20 | CHANGED | rc=0 >>
总用量 4
-rw-r-----. 1 root root 689 1月  11 08:33 fstab.back

3.6 file模块

[root@master ~]# ansible webserver -m user -a 'name=mysql system=yes'
[root@master ~]# ansible webserver -m group -a 'name=mysql system=yes'
[root@master ~]# ansible webserver -a 'id mysql'
[root@master ~]# ansible webserver -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back' 修改文件的属主属组权限
[root@master ~]# ansible webserver -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link’ 设置/opt/fstablink为/opt/fstab.back的链接文
[root@master ~]# ansible webserver -m file -a 'path=/opt/temp state=directory mode=755'//  创建目录
[root@master ~]# ansible webserver -m file -a "path=/opt/test state=touch" 创建一个文件

3.7 yum模块

[root@master ~]# ansible webserver -m yum -a 'name=httpd' 安装httpd
[root@master ~]# ansible webserver -a 'systemctl status httpd' 查看httpd运行状态
[root@master ~]# ansible webserver -m yum -a 'name=httpd state=absent' 删除httpd服务
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值