Ansible 介绍
Ansible 是什么
Ansible 是一种 IT 自动化工具. 它可以配置系统, 部署软件以及协调更高级的 IT 任务, 例如连续部署或零停机滚动更新.
Ansible 的主要目标是简单和易用. 它还着重于安全性和可靠性, 其特点是活动部件最少, 使用 OpenSSH 进行运输 (使用其他运输方式和拉动模式作为替代), 以及一种围绕人员(即使是不熟悉的人) 可审核性设计的语言. 该程序.
Ansible 以无代理的方式管理机器. 从来没有关于如何升级远程守护程序的问题, 也没有因为卸载守护程序而无法管理系统的问题. 由于 OpenSSH 是最受同行评审的开源组件之一, 因此可以大大降低安全风险. Ansible 是分散式的 - 它依靠您现有的 OS 凭据来控制对远程计算机的访问. 如果需要, Ansible 可以轻松地与 Kerberos,LDAP 和其他集中式身份验证管理系统连接.
特点总结:
1. ansible 不要安装单独的 agent, 也不用启动任何的服务.
2. ansible 是 python 中的一套完整的自动化执行任务模块.
3. ansible playblook 采用 YAML 进行配置, 对自动化任务执行了然.
4. ansibe 是基于 SSH 进行通信, 从而达到自动化任务的.
Ansible 的组成
Ansible: 是 ansible 的命令工具, 用来执行各种操作.
Ansible Playbook: 任务剧本, 用于编排定义 Ansible 的任务集的配置文件, 由 Ansible 顺序依次执行, 用 YAML 格式进行编排.
Inventory: Anisble 管理主机的清单, 默认为 / etc/ansible/hosts 文件
Modules: Ansible 执行各种功能操作的功能模块.
Plugins: 插件, 模块功能的补充, 常有连接类型插件, 循环插件, 变量插件, 过滤插件, 插件功能用的较少
API: 提供给第三方程序调用的应用程序编程接口.
Ansible 安装配置
环境准备
IP | 系统 | 主机名 | 描述 |
---|---|---|---|
192.168.100.11 | CentOS7 | server01 | ansible 管理节点 |
192.168.100.12 | CentOS7 | server02 | 被管理节点 1 |
192.168.100.13 | CentOS7 | server03 | 被管理节点 2 |
192.168.100.14 | CentOS6 | hd1 | 被管理节点 3 |
192.168.100.15 | CentOS6 | hd2 | 被管理节点 4 |
下载安装
第一步: 安装 epel 源
- # 安装 epel 源
- [[email protected] /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
第二步: 安装 ansible
[[email protected] /]# yum install -y ansible
第三步: 检查版本
- [[email protected] /]# ansible --version
- ansible 2.9.10
- 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, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
Ansible 的 Inventory 文件
Inventory 文件通常用于定义要管理的主机的认证信息, 例如 SSH 登录用户名, 密码以及 key 相关信息. 可以同时操作一个组的多台主机, 组与主机组之间的关系都是通过 inventory 文件配置. 配置文件路径为:/etc/ansible/hosts
Ansible 的使用前提
前面已经说过, ansible 实现自动化管理是基于 SSH 进行操作的, 所以在进行管理操作的时候, 需要开启 SSH 权限, 或者在 Inventory 文件中定义好用户和密码等信息.
基于密码连接
第一步: 需对 ansible.cfg 进行配置, 配置 host_key_checking:
- [[email protected] /]# VIM /etc/ansible/ansible.cfg
- #roles_path = /etc/ansible/roles
- # uncomment this to disable SSH key host checking
host_key_checking = False # 将这个 #号去掉
第二步: 配置 /etc/ansible/hosts 文件
- [[email protected] /]# VIM /etc/ansible/hosts
- [server]
- 192.168.100.12 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="qwe123"
- 192.168.100.13 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="qwe123"
- 192.168.100.14 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="qwe123"
- 192.168.100.15 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="qwe123"
第三步: 测试
- [[email protected] /]# ansible server -m shell -a 'uptime' -o
- 192.168.100.13 | CHANGED | rc=0 | (stdout) 22:00:36 up 1:07, 2 users, load average: 0.00, 0.01, 0.05
- 192.168.100.12 | CHANGED | rc=0 | (stdout) 22:00:36 up 1:06, 3 users, load average: 0.00, 0.01, 0.05
- 192.168.100.14 | CHANGED | rc=0 | (stdout) 05:44:21 up 5 min, 2 users, load average: 0.25, 0.17, 0.08
- 192.168.100.15 | CHANGED | rc=0 | (stdout) 00:00:55 up 5 min, 2 users, load average: 0.00, 0.03, 0.00
第四步: 其他方式进行密码方式连接
- # 方法二 主机 + 端口 + 密码
- [server]
- 192.168.100.1[2:5] ansible_ssh_user=root ansible_ssh_pass="qwe123"
- # 方法二 主机 + 端口 + 密码
- [server]
- 192.168.100.1[2:5]
- [server:vars]
- ansible_ssh_pass="qwe123"
基于秘钥链接
基于秘钥连接需要先创建公钥和私钥, 并发送给被管理机器
第一步: 生成公私钥并发送
- [[email protected] ~]# SSH-keygen
- [[email protected] ~]# for i in {
- 2,3,4,5
- }; do SSH-copy-id -i 192.168.100.1$i ; done
第二步: 配置连接
- [[email protected]~]# VIM /etc/ansible/hosts
- # 方法一 主机 + 端口 + 密钥
- [webserver]
- 192.168.100.12:22
- 192.168.100.13
- 192.168.100.14
- 192.168.100.15
- # 方法二 别名主机 + 端口 + 密钥
- [server]
- node1 ansible_ssh_host=192.168.100.12 ansible_ssh_port=22
- node2 ansible_ssh_host=192.168.100.13 ansible_ssh_port=22
- node3 ansible_ssh_host=192.168.100.14 ansible_ssh_port=22
- node4 ansible_ssh_host=192.168.100.15 ansible_ssh_port=22
第三步: 测试
- [[email protected] /]# ansible server -m shell -a 'uptime' -o
- 192.168.100.15 | CHANGED | rc=0 | (stdout) 00:56:33 up 1:01, 2 users, load average: 0.00, 0.00, 0.00
- 192.168.100.13 | CHANGED | rc=0 | (stdout) 22:56:25 up 2:03, 2 users, load average: 0.01, 0.05, 0.05
- 192.168.100.14 | CHANGED | rc=0 | (stdout) 06:40:00 up 1:01, 2 users, load average: 0.00, 0.00, 0.00
- 192.168.100.12 | CHANGED | rc=0 | (stdout) 22:56:25 up 2:02, 3 users, load average: 0.01, 0.03, 0.05
主机组的使用
就是将多个类型的组进行归类整合, 如下:
- # 主机组变量名 + 主机 + 密码
- [apache]
- 192.168.100.12
- 192.168.100.13
- [apache.vars]
- ansible_ssh_pass='qwe123'
- # 主机组变量名 + 主机 + 密钥
- [nginx]
- 192.168.100.1[2:3]
- # 定义多个组, 把一个组当另外一个组的组员
- [server:children] #webserver 组包括两个子组: apache nginx
- apache
- nginx
临时指定 Inventory
第一步: 编写主机清单
- [[email protected] ~]# VIM /tmp/dockers
- [dockers]
- 192.168.100.13 ansible_ssh_pass='qwe123'
- 192.168.100.14
- 192.168.100.15
第二步: 指定主机清单使用
- [[email protected]~]# ansible dockers -m ping -i /etc/dockers -o
- 192.168.100.13 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python"
- }, "changed": false, "ping": "pong"
- }
- 192.168.100.14 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python"
- }, "changed": false, "ping": "pong"
- }
- 192.168.100.15 | SUCCESS => {
- "ansible_facts": {
- "discovered_interpreter_python": "/usr/bin/python"
- }, "changed": false, "ping": "pong"
- }
Inventory 的内置参数
Ansible 的命令格式
常用命令参数
可以通过命令查看参数:
[[email protected] /]# ansible -h
这里对其进行说明:
-m: 要执行的模块, 默认为 command
-a: 指定模块的参数
-u:SSH 连接的用户名, 默认用 root,ansible.cfg 中可以配置
-b,--become: 变成那个用户身份, 不提示密码
-k: 提示输入 SSH 登录密码, 当使用密码验证的时候用
-s:sudo 运行
-U:sudo 到哪个用户, 默认为 root
-K: 提示输入 sudo 密码, 当不是 NOPASSWD 模式时使用
-C: 只是测试一下会改变什么内容, 不会真正去执行
-c: 连接类型(default=smart)
-f:fork 多少进程并发处理, 默认为 5 个
-i: 指定 hosts 文件路径, 默认 default=/etc/ansible/hosts
-I: 指定 pattern, 对已匹配的主机中再过滤一次
-list-host: 只打印有哪些主机会执行这个命令, 不会实际执行
-M: 要执行的模块路径, 默认为 / usr/share/ansible
-o: 压缩输出, 摘要输出
--private-key: 私钥路径
-T:SSH 连接超时时间, 默认是 10 秒
-t: 日志输出到该目录, 日志文件名以主机命名
-v: 显示详细日志
测试:
- [[email protected] /]# ansible server -m shell -a 'uptime' -o
- 192.168.100.15 | CHANGED | rc=0 | (stdout) 00:56:33 up 1:01, 2 users, load average: 0.00, 0.00, 0.00
- 192.168.100.13 | CHANGED | rc=0 | (stdout) 22:56:25 up 2:03, 2 users, load average: 0.01, 0.05, 0.05
- 192.168.100.14 | CHANGED | rc=0 | (stdout) 06:40:00 up 1:01, 2 users, load average: 0.00, 0.00, 0.00
- 192.168.100.12 | CHANGED | rc=0 | (stdout) 22:56:25 up 2:02, 3 users, load average: 0.01, 0.03, 0.05
命令格式:
host-pattern 格式
目标 target 主机, 主机组匹配方式
主机匹配
- # 一台目标主机
- [[email protected]~]# ansible 192.168.100.12 -m ping
- # 多台目标主机
- [[email protected]~]# ansible 192.168.100.12,192.168.100.13 -m ping
- # 所有目标主机
- [[email protected]~]# ansible all -m ping
主机组匹配
- # 组的配置信息如下: 这里定义了一个 nginx 组和一个 apache 组
- [[email protected] ~]# ansible nginx --list
- hosts (2):
- 192.168.100.12
- 192.168.100.13
- [[email protected] ~]# ansible apache --list
- hosts (3):
- 192.168.100.13
- 192.168.100.14
- 192.168.100.15
- # 一个组的所有主机匹配
- [[email protected] ~]# ansible apache -m ping
- # 匹配 apache 组中有, 但是 nginx 组中没有的所有主机
- [[email protected] ~]# ansible 'apache:!nginx' -m ping -o
- 192.168.100.12 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
- 192.168.100.13 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
- # 匹配 apache 组和 nginx 组中都有的机器(并集)
- [[email protected] ~]# ansible 'apache:&nginx' -m ping -o
- 192.168.100.13 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
- # 匹配 apache 组 nginx 组两个组所有的机器(并集); 等于 ansible apache,nginx -m ping
- [[email protected] ~]# ansible 'apache:nginx' -m ping -o
- 192.168.100.12 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
- 192.168.100.13 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
- 192.168.100.14 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
- 192.168.100.15 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
来源: http://www.bubuko.com/infodetail-3649953.html