Ansible
Ansible简介
什么是Ansible
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
上段话引用自百度百科 https://baike.baidu.com/item/ansible/20194655?fr=aladdin
Ansible是如何工作
默认开启5个并行,并且Ansible工作的时候是需要调用本机的pyhon。Windows只能作为客户端使用,服务端只能是linux操作系统。
应用场景
Asible的安装
1、配置yum源:
[epel]
name=epel
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/
gpgcheck=0
这里使用的是阿里的镜像源
2、安装
yum install ansible -y
ansible只需要在提交任务的节点安装即可
3、创建ansible的用户
useradd ansibel
echo westos |passdwd --stdin ansible
主配置文件为:ansible.cfg
4、配置无密码登录(root和ansible用户)
生成密钥
ssh-keygen
设置主机之间的免密
root用户
ssh-copy-id server2
ssh-copy-id server3
ansible用户
ssh-copy-id ansible@server2
ssh-copy-id ansible@server3
Ansible的使用以及命令集
1、配置文件
配置文件在/etc/ansible/ansible.cfg文件
2、更改hosts文件,在hosts文件中添加主机信息:
这里可以添加主机名称也可以添加ip,如果添加主机名称的话,需要添加域名解析
示例:
文件的编写是很灵活的,又很多中写法。
这里列举4种方法:
<1>一个web组,组中有两个主机,分别的ansible1和ansible2
[web]
ansible1
ansible2
<2>一个web组,组中有很多主机,主机名只要是以ansible开头的即可
[web]
server*
<3>一个web组,组中有100个主机,主机名是ansible1-ansible100
[web]
ansible[1:100]
<4>两个组,一个web组,一个db组。web组中的主机为ansible1,db组中的主机为ansible2。
[web]
ansible1
[db]
ansible2
3、ansible的使用
ansible all -m ping
ansible all -m ping -u ansible
使用密钥进行测试
ansible all -m ping -u customer -k
ansible all -a “touch /tmp/testfile”
ansible all -a “touch /mnt/testfile” -u anlsible