主要用于批量主机操作,无须安装客户端。
Ansible官网:https://www.ansible.com/
作者:Michael DeHaan( Cobbler 与 Func 作者)
ansible 的名称来自科幻小说《安德的游戏》中跨越时空的即时通信工具,使用它可以在相距数光年的
距离,远程实时控制前线的舰队战斗。
Ansible特性
1、模块化:调用特定的模块完成特定任务,支持自定义模块,可使用任何编程语言写模块
2、Paramiko(python对ssh的实现),PyYAML,Jinja2(模板语言)三个关键模块
3、基于Python语言实现
4、部署简单,基于python和SSH(默认已安装),agentless,无需代理不依赖PKI(无需ssl)
5、安全,基于OpenSSH
6、幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况
7、支持playbook编排任务,YAML格式,编排任务,支持丰富的数据结构
8、较强大的多层解决方案 role
Ansible命令执行来源
1、USER 普通用户,即SYSTEM ADMINISTRATOR
2、PLAYBOOKS:任务剧本(任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执
行,通常是JSON格式的YML文件
3、CMDB(配置管理数据库) API 调用
4、PUBLIC/PRIVATE CLOUD API调用
5、USER-> Ansible Playbook -> Ansibile
Ansible管理主机的清单路径 /etc/anaible/hosts
Ansible注意事项
11、执行ansible的主机一般称为主控端,中控,master或堡垒机
2、主控端Python版本需要2.6或以上
3、被控端Python版本小于2.4,需要安装python-simplejson
4、被控端如开启SELinux需要安装libselinux-python
5、windows 不能做为主控端
Ansible安装
采用yum安装方式
1、备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
2、下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
3. 运行 yum makecache 生成缓存与安装Ansible
yum makecache yum -y install ansible
4、验证是否安装成功,出现具体版本信息表示成功。
[root@ansible-1 ~]# ansible --version ansible 2.9.16 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] [root@ansible-1 ~]#
Ansible相关配置文件
配置文件
/etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性 /etc/ansible/hosts 主机清单 /etc/ansible/roles/ 存放角色的目录
Ansible主配置文件
/etc/ansible/ansible.cfg ,大部分无须修改
[defaults] #inventory = /etc/ansible/hosts # 主机列表配置文件 #library = /usr/share/my_modules/ # 库文件存放目录 #remote_tmp = $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录 #local_tmp = $HOME/.ansible/tmp # 本机的临时命令执行目录 #forks = 5 # 默认并发数 #sudo_user = root # 默认sudo 用户 #ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码 #ask_pass = True #remote_port = 22 #host_key_checking = False # 检查对应服务器的host_key,建议取消注释 #log_path=/var/log/ansible.log #日志文件,建议启用 #module_name = command #默认模块,可以修改为shell模块
主机清单
来动态生成
inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组
中
此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明
如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机
样例:
[webservers] 192.168.10.202 192.168.10.203 [dbservers] 192.168.10.204 192.168.10.206:222 [websrvs] www[1:100].example.com [dbsrvs] db-[a:f].example.com [appsrvs] 10.0.0.[1:100]
[app]
172.21.4.100
172.21.4.186
[app:vars]
ansible_ssh_user=readonly
ansible_ssh_port=40001
ansible_ssh_pass='123456'
Ansible相关工具
/usr/bin/ansible 主程序,临时命令执行工具 /usr/bin/ansible-doc 查看配置文档,模块功能查看工具,相当于man /usr/bin/ansible-playbook 定制自动化任务,编排剧本工具,相当于脚本 /usr/bin/ansible-pull 远程执行命令的工具 /usr/bin/ansible-vault 文件加密工具 /usr/bin/ansible-console 基于Console界面与用户交互的执行工具 /usr/bin/ansible-galaxy 下载/上传优秀代码或Roles模块的官网平台
ansible-doc
此工具用来显示模块帮助,使用方法如下:
#-l 参数显示所有可用模块,统计有3387个 [root@ansible-1 ~]# ansible-doc -l |wc -l 3387 # -s 参数 加 ping 模块,查看指定模块帮助用法 [root@ansible-1 ~]# ansible-doc -s ping - name: Try to connect to host, verify a usable python and return `pong' on success ping: data: # Data to return for the `ping' return value. If this parameter is set to `crash', the module will cause an exception. You have new mail in /var/spool/mail/root #查看指定模块帮助用法 [root@ansible-1 ~]# ansible-doc ping > PING (/usr/lib/python2.7/site-packages/ansible/modules/system/ping.py) A trivial test module, this module always returns `pong' on successful contact. It does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to verify the ability to login and that a usable Python is configured. This is NOT ICMP ping, this is just a trivial test module that requires Python on the remote-node. For Windows targets, use the [win_ping] module instead. For Network targets, use the [net_ping] module
ansible
此工具通过ssh协议,实现对远程主机的配置管理、应用部署、任务执行等功能
建议:使用此工具前,先配置ansible主控端能基于密钥认证的方式联系各个被管理节点
范例:利用sshpass批量实现基于key验证脚本1
[root@ansible-1 ~]# cat hosts.list 192.168.10.202 192.168.10.203 [root@ansible-1 ~]# cat push_ssh_key.sh #!/bin/bash rpm -q sshpass &> /dev/null || yum -y install sshpass [ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P '' export SSHPASS=123456 while read IP;do sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP done < hosts.list [root@ansible-1 ~]#
范例: 实现基于key验证的脚本2
IPLIST=" 10.0.0.8 10.0.0.18 10.0.0.7 10.0.0.6 10.0.0.200" rpm -q sshpass &> /dev/null || yum -y install sshpass [ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P '' export SSHPASS=123456 for IP in $IPLIST;do sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP done