Ansible是一种开源IT自动化工具,使用Python开发,应用范围非常广泛:Linux和windows系统,各类公有云/私有云,网络设备;它可以批量完成配置系统,部署软件以及协调更高级的IT任务,例如持续部署或零停机滚动更新。Ansible的主要目标是简单易用。它还非常注重安全性和可靠性。
Ansile没有客户端,因此底层通信依赖于系统软件,Linux系统基于OpenSSH,window系统基于PowerShell,管理端必须是Linux。 默认使用OpenSSH进行传输(使用其他传输和拉动模式作为替代方案),Ansible本身是一种框架,真正具有执行能力的是模块。Ansible提供两种方式去完成任务, ad-hoc 命令 和 Ansible playbook(yaml语言)。
Ansible架构图:
Ansible的工具集主要有6部分组成:
- Ansible Playbook: 任务剧本,编排定义Ansible任务集的配置文件,Json格式的YML文件。
- Inventory: Ansible管理主机的清单
- Modules: Ansible执行命令的功能模块,多数为内置的核心模块,也可自定义;
- Plugins: 模块功能补充,各种类型的插件
- API: 第三方程序调用的应用程序编程接口
- Ansible: inventory,api,modules,plugins组合,核心执行工具。
一、Ansible安装部署
控制机:
目前Ansible已经出来了2.6版本,模块也已经达到了一千多个了。目前Ansible可以从安装了Python 2(2.7版)或Python 3(3.5及更高版本)的任何机器上运行。控制计算机不支持Windows。这包括Red Hat,Debian,CentOS,macOS,任何BSD等。
被控节点:
需要跟控制机一样的环境跟版本,Ubuntu16中自带安装的是Python3+,而Ansible默认使用2+,如果需要使用Python3,需要在hosts中自定义路径,或者使用python2+,使用原始模块安装。
1、自定义python路径
[web]
xxx.xx.xxx.xx
[web:vars]
ansible_python_interpreter=/usr/bin/python3
2、使用原始模块安装python2+
ansible all -m raw -a "yum install python2 -y"
这里使用Centos7系统,默认Python 2.75
3、使用EPEL源安装最新版Ansible
yum install -y epel-release
yum install -y ansible
二、Ansible常用配置参数
配置文件
配置文件或指令 | 描述 |
---|---|
/etc/ansible/ansible.cfg | 主配置文件,配置ansible工作特性 |
/etc/ansible/hosts | 主机清单 |
/etc/ansible/roles/ | 存放角色的目录 |
/usr/bin/ansible | 主程序,临时命令执行工具 |
/usr/bin/ansible-doc | 查看配置文档,模块功能查看工具 |
/usr/bin/ansible-galaxy | 下载/上传优秀代码或Roles模块的官网平台 |
/usr/bin/ansible-playbook | 定制自动化任务,编排剧本工具 |
/usr/bin/ansible-pull | 远程执行命令的工具 |
/usr/bin/ansible-vault | 文件加密工具 |
/usr/bin/ansible-console | 基于Console界面与用户交互的执行工具 |
1、默认配置文件路径
# vim /etc/ansible/ansible.cfg inventory = /etc/ansible/hosts #默认主机清单配置文件 library = /usr/share/my_modules/ #默认模块路径 forks = 20 #任务执行进程 sudo_user = root #设置默认执行命令的用户 remote_port = 22 #默认远程连接端口 timeout = 10 #SSH连接超时时间,单位秒 host_key_checking = False #设置是否检查SSH主机的密钥。 log_path = /var/log/ansible.log #设置日志记录 pipelining = true #性能加速
2、Inventory 主机清单
Ansible通过Inventory 来管理主机。Ansible 可同时操作属于一个组的多台主机,组和主机之间的关系通过 inventory 文件配置。
语法格式:
单台主机
green.example.com > FQDN
192.168.100.10 > IP地址
192.168.100.11:2222 > 非标准SSH端口
[webservers] > 定义了一个组名
alpha.example.org > 组内的单台主机
192.168.100.10
[dbservers]
192.168.100.10 > 一台主机可以是不同的组,这台主机同时属于[webservers]
[group:children] > 组嵌套组,group为自定义的组名,children是关键字,固定语法,必须填写。
dns > group组内包含的其他组名
db > group组内包含的其他组名
[webservers]
www[001:003].abc.com > 有规律的名称列表,
这里表示相当于:
www001.abc.com
www002.abc.com
www003.abc.com
[databases]
db-[a:c].example.com > 定义字母范围的简写模式,
这里表示相当于:
db-a.example.com
db-b.example.com
db-c.example.com
以下这2条定义了一台主机的连接方式,而不是读取默认的配置设定
localhost ansible_connection=local
www.163.com ansible_connection=ssh ansible_ssh_user=hunk
最后还有一个隐藏的分组,那就是all,代表全部主机,这个是隐式的,不需要写出来的。
3、Inventory参数说明
ansible_ssh_host: #ansible使用ssh要连接的主机。 ansible_ssh_port: #ssh的端口。默认为22。 ansible_ssh_user: #ssh登录的用户名。默认为root。 ansible_ssh_pass: #ssh登录远程用户时的认证密码。 ansible_ssh_private_key_file: #ssh登录远程用户时的认证私钥。 ansible_connection: #使用何种模式连接到远程主机。默认值为smart(智能), 表示当本地ssh支持持久连接(controlpersist)时采用ssh连接,否则采用python的paramiko ssh连接。 ansible_shell_type: #指定远程主机执行命令时的shell解析器,默认为sh (不是bash,它们是有区别的,也不是全路径)。 ansible_python_interpreter: #远程主机上的python解释器路径。默认为/usr/bin/python。 ansible_*_interpreter: #使用什么解释器。例如,sh、bash、awk、sed、expect、ruby等等
4、hosts添加两个被控节点测试下
# vim /etc/ansible/hosts [test] 192.168.1.230 192.168.1.231
5、配置秘钥分发,一般在批量创建主机的时候加入秘钥,或者使用脚本
# ssh-keygen #生成秘钥 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): 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: SHA256:TNNvVkUqRNC2M5W9kCHnFTtBWGlvlhNNfmAEaSvBf2c [email protected] The key's randomart image is: +---[RSA 2048]----+ | ..=+*[email protected]| | .o.O=+O=| | o .=.==oB| | o ...*o.+E| | S .++ =.| | o | | | | | | | +----[SHA256]-----+ #复制秘钥到被控主机上 # ssh-copy-id [email protected] # ssh-copy-id [email protected]
6、使用ansible测试下ping
[[email protected] ~]# ansible all -m ping 192.168.1.231 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.1.230 | SUCCESS => { "changed": false, "ping": "pong" }
Ansible官方指南:https://docs.ansible.com