自动化运维工具对比
1.Puppet:基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱
2.SaltStack:基于 Python 开发,采用 C/S 架构,相对 puppet 更轻量级,配置语法使用 YAML,使得配置脚本更简单.需要配置客户端以及服务器端。每台被控制节点需要安装agent
3.Ansible:基于 Python开发,分布式,无需客户端,轻量级,配置语法使用YAML语言,更强的远程命令执行操作
一、ansible简介
ansible是新出现的自动化运维工具,基于Python开发,分布式,无需客户端,轻量级,实现了批量系统配置、批量程序部署、批量运行命令等功能,ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。
1、Ansible特性
1)、no agents:不需要在被管控主机上安装任何客户端,更新时,只需在操作机上进行一次更新即可(不用安装客户端。分布式的)
2)、no server:无服务器端,使用时直接运行命令即可
3)、modules in any languages:基于模块工作,可使用任意语言开发模块
4)、yaml,not code:使用yaml语言定制剧本playbook
5)、ssh by default:基于SSH工作
6)、strong multi-tier solution:可实现多级指挥
image.png
connection plugins:连接插件,负责和被监控端实现通信,默认使用SSH连接
host inventory:主机清单,是一个配置文件里面定义监控的主机
modules : 模块,核心模块、command模块、自定义模块等
plugins : modules功能的补充,包括连接插件,邮件插件等
playbook:编排,定义 Ansible 多任务配置文件,非必需
二、安装Ansible
1、准备环境
主机:两台 1个控制节点 1个被控制节点
解析:本地互相解析
vim /etc/hosts
192.168.142.66 ansible-server
192.168.142.99 ansible-client
配置ssh公钥认证:控制节点需要发送ssh公钥给所有被控制节点
ssh-keygen
ssh-copy-id -i 192.168.142.99
[root@ansible-server ~]# ls .ssh/
id_rsa id_rsa.pub known_hosts
[root@ansible-client ~]# ls .ssh/
authorized_keys
2、关闭firewall和selinux
#所有机器
systemctl stop firewalld && setenforce 0
3、安装
安装:控制节点
yum -y install epel*
yum -y install ansible
ansible --version #查看版本
ansible --help #看帮助
4、ansible基础---inventory主机清单
官方文档:http://docs.ansible.com/ansible/intro_inventory.html#>
inventory文件通常用于定义要管理主机的认证信息,例如ssh登录用户名、密码以及key相关信息。
查看配置文件:
[root@ansible-server ~]# rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
1.主配置文件:主要设置一些ansible初始化的信息,比如日志存放路径、模块、等配置信息
/etc/ansible/ansible.cfg
2.主机清单文件:
/etcansible/hosts
checklist 检查清单
语法:
1、添加主机
vim /etcansible/hosts (加被控制节点的机器)
ansible-client
2、添加主机组
[web1]
192.168.142.99
[web2]
ansible-client
3、组可以包含其他组
[web_all]
web1 #组一
ip
web2 #组二
ip
4、在组里指定变量,组内每个主机都可以使用该变量:
[ absible_all:var ] #设置变量
absible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_private_key_file=/root/.ssh/id_rsa
Ansible lnventory常见的内置参数:
image.png
查看组内主机列表:
语法:ansible 组名 --list-hosts
[root@ansible-server ~]# ansible web_all --list-hosts
hosts (2):
web1
web2
三、测试
语法:
ansible <pattern> -m <module_name> -a <arguments>
pattern:主机清单里定义的主机组名,主机名,IP,别名等,all表示所有的主机,支持通配符,正则
-m:模块名称,默认为command
-a: 传递给模块的参数
-o:横着显示(单行显示)
使用ping模块检查ansible节点的连通性:
1.指定多台机器:
[root@ansible-server ~]# ansible ansible-client -m ping -o
ansible-client | SUCCESS => {"ansible_facts": {"discovered_interpreter_python":
"/usr/bin/python"}, "changed": false, "ping": "pong"}
注意:这里的ping并不是真正意义上的ping
而是探测远程主机ssh是否可以连接!判断ssh端口是否存活。
2.同时指定多台机器:
[root@ansible-server ~]# ansible ansible-client,ansible-client -m ping -o
3.指定组名:
[root@ansible-server ~]# ansible web_all -m ping -o
执行shell命令:
[root@ansible-server ~]# ansible ansible-client -m shell -a 'uptime'
ansible-client | CHANGED | rc=0 >>
20:02:02 up 8:26, 4 users, load average: 0.01, 0.04, 0.06
不加 -m 默认时command模块
1.给节点增加用户:
[root@ansible-server ~]# ansible ansible-client -m shell -a 'useradd tom'
ansible-client | CHANGED | rc=0 >>
在ansible-client机器查看:
[root@ansible-client ~]# grep tom /etc/passwd
tom:x:1000:1000::/home/tom:/bin/bash
四、Ad-Hoc
ad hoc其实就是执行简单的命令——一条命令。对于复杂的命令则为 playbook。
ansible支持的模块
-l:获取列表
-s:获取指定模块的使用信息
[root@ansible-server ~]# ansible-doc -l
查看模块使用信息
[root@ansible-server ~]# ansible-doc -s yum
常用模块
1.远程复制备份模块:copy
模块参数详解:
src=:指定源文件路径
dest=:目标地址(拷贝到哪里)
owner:指定属主
group:指定属组
mode:指定权限
backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
---创建一个测试文件
[root@ansible-server ~]# vim a.txt
123 123
[root@ansible-server ~]# ansible ansible_client -m copy -a 'src=/root/a.txt dest=/opt owner=root group=root mode=644' -o
在被控制节点ansible-client查看
[root@ansible-client ~]# cat /opt/a.txt
123 123
2.软件包管理 yum模块
安装apache
[root@ansible-server ~]# ansible ansible-client -m yum -a "name=httpd state=latest" -o
卸载软件:
[root@ansible-server ~]# ansible ansible-client -m yum -a "name=httpd state=removed" -o
state= #状态是什么,干什么
state=latest 表示最新的
state=removed 表示卸载
3.服务管理service模块
--启动--
[root@ansible-server ~]# ansible ansible-client -m service -a "name=httpd state=started"
--停止--
[root@ansible-server ~]# ansible ansible-client -m service -a "name=httpd state=stopped"
4.文件模块file
owner:修改属主
group:修改属组
mode:修改权限
path=:要修改文件的路径
创建一个文件
[root@ansible-server ~]# ansible ansible-client -m file -a 'path=/tmp/88.txt mode=777 owner=tom state=touch'
创建一个目录
[root@ansible-server ~]# ansible ansible-client -m file -a 'path=/tmp/99 mode=777 state=directory'
被控制节点操作:
[root@ansible-client ~]# ll /tmp/88.txt
-rwxrwxrwx 1 tom root 0 Jan 6 20:44 /tmp/88.txt
[root@ansible-client ~]# ll -d /tmp/99/
drwxrwxrwx 2 root root 6 Jan 6 20:45 /tmp/99/
5.收集信息模块setup
[root@ansible-server ~]# ansible ansible-client -m setup
#只查询ipv4的地址
[root@ansible-server ~]# ansible ansible-client -m setup -a 'filter=ansible_all_ipv4_addresses'
filter:过滤
五、ansible-playbook 剧本
1、Playbook介绍
playbook是ansible用于配置,部署,和管理被控节点的剧本。通过playbook的详细描述,执行其中的tasks,可以让远端主机达到预期的状态。
playbook是由一个或多个”play”组成的列表。 当对一台机器做环境初始化的时候往往需要不止做一件事情,这时使用playbook会更加适合。通过playbook你可以一次在多台机器执行多个指令。通过这种预先设计的配置保持了机器的配置统一,并很简单的执行日常任务。
ansible通过不同的模块实现相应的管理,管理的方式通过定义的清单文件(hosts)所管理的主机包括认证的方式连接的端口等。所有的功能都是通过调用不同的模块(modules)来完成不同的功能的。不管是执行单条命令还是play-book都是基于清单文件。
网友评论