ansible的安装与使用
Ansible概述:
是一个配置管理系统(configuration management system),当下最流行的批量自动化运维工具之一.
常用的运维工具:ssh/puppet(ruby)/ansible(无客户端,中小规模)(python)/saltstack(master-minion)(python)大规模
Ansible的作用:
批量部署,服务安装,日常备份
Ansible官方文档:
https://docs.ansible.com/ansible/latest/index.html
Ansible的特性:
无客户端软件,通过ssh远程管理
安装后不需要启动服务
依赖大量的Python模块扩展功能
配置文件:/etc/ansible/ansible.cfg
Ansible基础架构:
连接插件(connecter plugins):用来连接主机,连接被管理端
核心模块(core modules):连接主机,实现操作,依赖于具体模块来执行
自定义模块:用户自己开发的功能模块
剧本(playbook):将多个任务组合成一个剧本,由ansible自动批量执行
主机清单(host inventory):定义ansible管理的客户端主机范围
Ansible的命令格式:
ansible 主机清单名 -m 调用的模块 -a 动作命令
----------------------------------------------------使用----------------------
1.安装ansible
(1)配置epel源
epel源(扩展包):wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
linux镜像源(组包):wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
(2)yum安装
yum -y install ansible
ansible --version ##查看版本
(3)配置主机清单
vim /etc/ansible/hosts
添加:
[web] ##主机清单名
192.168.1.50 ##主机IP地址 可同时添加多个
[rsync]
192.168.1.40
保存退出
(4)配置ssh密钥对访问 (如有多台都进行此操作!!)
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
85:5b:6f:c7:a5:36:8c:70:1b:bf:fa:b0:7e:9c:66:f1 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| |
| . |
| . + o .|
| + + B o |
| S = O |
| . o.o |
| ...+ |
| +* E|
| .+=. |
+-----------------+
[root@localhost ~]# ssh-copy-id root@192.168.1.50
The authenticity of host '192.168.1.50 (192.168.1.50)' can't be established.
ECDSA key fingerprint is b5:60:c3:7a:34:c5:7c:f3:fe:4f:a5:07:88:55:c1:3e.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
123^H^H^H/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.50's password: ##输入root密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.50'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost conf]# ssh-copy-id root@192.168.1.10
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.10's password: ##输入root密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.10'"
and check to make sure that only the key(s) you wanted were added.
2.Ansible的基础应用
(1)调用模块颜色显示
翔黄色 更改成功
帽绿色 没有更改
深红色 错误
亮紫色 警告