Ansible之简要介绍及部署

常用的自动化运维工具

Puppet

​ ——基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱

SaltStack

​ ——基于 Python 开发,采用 C/S 架构,相对 Puppet更轻量级,配置语法使用 YAML,使得配置脚本更简单

Ansible

​ ——基于 Python paramiko 开发,分布式,无需客户端,轻量级,配置语法使用 YAML 及 Jinja2 模板语言,更强的远程命令执行操作,可用于自动化部署应用、配置、编排 task(持续交付、无宕机更新等)

Ansible 特点

  • 去中心化,可以同时执行多台
  • 上手简单,学习曲线平滑
  • 安全,基于 SSL,无需安装客户端
  • 配置简单,功能强大,扩展性更强
  • 支持 API 及自定义模块,可以通过 Python 轻松扩展
  • 可以编写 Playbooks 来指定更强大的配置、状态管理
  • 操作具有幂等性,一次操作重复多次结果也相同

Ansible基础架构

e6b31489c7cfc43d0b9a0182d8d41927f8a81eedf69fe5d1ca415a1759b84d12.png

  • Ansible:ansible 核心程序
  • Host Inventory:主机清单,记录了每个由 ansible 管理的主机信息,包括主机地址,ssh 端口,账号密码等等。可以通过 file 加载,也可以通过 CMDB 加载
  • Playbooks:剧本,YAML 格式文件,将多个任务定义在一个文件中,使用可以统一调用,剧本用来定义哪些主机需要调用哪些模块来完成功能
  • Modules:包括核心模块和用户自定义的模块。ansible执行任何管理任务都不是由 ansible 自己完成的,而是由核心模块完成;ansible 管理主机前先调用 modules 中的模块,然后申明管理 Host Inventory 中的主机,就可以完成管理主机
  • Connect Plugins:连接插件,用于 ansible 和 host 间通信使用

环境部署

安装

  • yum -y install ansible:CentOS 7 可直接安装,CentOS 6 需要先拉源码把 Python 升到 2.7以上

密钥部署

传统 账号 + 密码 的方式容易在多次传输中被泄露,ansible 采用基于 ssl 加密通信,使用非对称加密,在使用之前需要先分发公钥,主机间建立密钥互信后才能使用。此后主机间通过

  • 管理端生成密钥:ssh-keygen
  • 密钥分发到被管理端:ssh-copy-id -i ~/.ssh/id_rsa.pub USERNAME/DES_HOST

配置文件详解

ansible 的配置文件放在 /etc/ansible/ 目录下

  • ansible.cfg:主配置文件

  • hosts:主机&主机组定义文件,被管理端的地址必须在此文件中定义后才能管理

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## 
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com

# 定义单个主机
192.168.159.131

# 定义主机组
[webservers]
192.168.159.131
  • roles:角色目录,可在该目录下创建角色目录,定义一系列的工作,配合 playbooks 使用

常用命令

  • ansible-doc 模块名:查看指定的模块帮助文档

  • ansible-doc -s 模块名:simple,只显示模块关键参数

  • ansible-doc -l:list,列出所有可用模块,包括核心模块和用户自定义的模块

  • ansible-doc -–version:查看 ansible 版本及配置信息

  • ansible IP -m 模块名:执行单项任务

[root@localhost ~]$ ansible 192.168.159.131 -m ping
192.168.159.131 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
# 返回值有“pong”,代表 ping 这个模块通了
  • ansible IP -m 模块名 -a “模块参数‘
这里表示使用 shell 模块,在被管理端执行 uptime 这个命令
[root@localhost ~]$ ansible 192.168.159.131 -m shell -a "uptime"
192.168.159.131 | CHANGED | rc=0 >>
 19:38:23 up  2:55,  4 users,  load average: 0.00, 0.01, 0.05
  • ansible-playbook PLAYBOOK.yaml:执行剧本

在这里插入图片描述

发布了67 篇原创文章 · 获赞 2 · 访问量 1370

猜你喜欢

转载自blog.csdn.net/weixin_42511320/article/details/105107922