ansible

ansible 需求

1.两种用法,api 和 cli  py库,可以一键执行,批量执行
2.linux 和 windows 的区别,在 windows 中使用有什么要注意的
3.如果有多个跳转机,怎么能从 master 中找到指定的 服务器

Ansible

学习网址

http://www.ansible.com.cn/docs/intro.html

https://docs.ansible.com/ansible/latest/installation_guide/index.html

https://www.w3cschool.cn/automate_with_ansible/automate_with_ansible-db6727oq.html

Ansible是一个非常简单的IT自动化系统。它处理配置管理,应用程序部署,云供应,临时任务执行,网络自动化和多节点编排。Ansible使用负载均衡器轻松进行复杂的更改,例如零停机滚动更新。

Ansible 默认通过 SSH 协议管理机器,在安装Ansible 之后,不需要启动或运行一个后台进程,或是添加一个数据库,只要在一台电脑(可以是一台笔记本)上安装好,就可以通过这台电脑管理一组远程的机器,在远程被管理的机器上,不需要安装运行任何软件。

设计原则

优点

  • 基于 python 开发的,而现在python又是运维工程师必不可少的技能之一,支持API及自定义模块;
  • 丰富的内置模块,可以用 ansible-doc -l 去查看可用的模块;
  • 无客户端,即无需在被控制主机上配置 client/agents,主要是通过ssh来和远程主机通信;
  • 批量部署,这个是自动化运维必不可少的;
  • 通过 Playbooks 来定制强大的配置、状态管理;
  • 提供了一个功能强大、操作性强的 Web 管理界面和 rest api 接口 —— AWX 平台

缺点

  • ansible 是一个相对较完美的自动运维工具,要说缺点就是,对windows被管节点需要加强、执行效率相对较低。

安装方式

1.在contOS 中使用系统的软件包管理器
2.通过 pip 工具安装

对主机要求:py2.6 或 py2.7都可以

windows 不可以做控制主机,主机的系统可以是 Red Hat, Debian, CentOS, OS X, BSD的各种版本	

检测 ansible

ansible --version

# 先在 hosts 文件中编写组 [sa], 然后测试 sa 组中的信息
ansible all --list-hosts    # 查看所有的 host
ansible sa -m ping    # 测试组
ansible sa -m ping -k    # 忽略检测

安装只有 centos 和 mac os 版本的

ansible 常见模块

ping   模块
yum    安装包模块
copy   复制文件模块
file   文件操作模块
service   管理服务模块
user   管理用户模块
group  管理组模块
cron   计划任务模块
template   template模块
setup   setup模块
fetch   从客户端取文件至服务器模块
apt   apt安装包模块
command   command模块
shell   shell模块
script   script模块

ssh 链接远程服务器出现错误:

原因:第一次ssh链接的时候会生成一个认证凭据,存储在客户端中的known_hosts,如果服务器地址重置or重新安装了,就会产生这个问题,巴拉巴拉。。。

解决:

ssh-keygen -R 服务器地址

然后重新链接

https://blog.csdn.net/weixin_42356309/article/details/83411437

https://blog.csdn.net/u010856284/article/details/73166358

https://www.bbsmax.com/A/WpdKAx7nJV/


自动化任务执行的应用

  • 批量命令执行
  • 定时程序任务执行
  • 批量程序应用服务安装
  • 批量配置文件同步
  • 批量代码部署

生成ssh

yum install sshpass

# 生成密钥
ssh-keygen   # 默认文件在 /root/.ssh/id_rsa

# 拷贝到指定服务器
ssh-copy-id  服务器ip

# 登录到指定服务器
ssh 服务器地址

ad-hoc模式

临时任务

场景一 在多台机器上,查看某个进程是否启动给
场景二 在多台机器上,拷贝指定日志文件到本地

# ad-hoc 模式的命令使用
ansible <host-pattern> [options]    # host-pattern:匹配主机名或主机组名
exaple:
    ansible 192.168.1.* -a 'ls/tmp'
    ansible group1 -a 'ls/tmp'

playbook 优势

  • 功能比 ad-hoc 更全
  • 控制好依赖
  • 展现更直观
  • 持久使用

playbook条件判断

循环语句

tag标签

对 tasks 打标签
-t 执行指定的tag 标签任务
--skip-tags  执行-=skip-tags 之外的标签任务

--skip-tags

include 的用法

include_tasks / include : 动态的包含 tasks 任务列表执行

include_tasks:test1.yml
include_tasks:test2.yml 

roles

是一种在利用在大型 playbook 

ansible python模块

常用类

from ansible.parsing.dataloader import DataLoader    # 用于读取yaml,json格式的文件
from ansible.vars.manager import VariableManager    # 用于存储各类变量信息
from ansible.inventory.manager import InventoryManager   # 用于导入 inventory 文件
from ansible.playbook.play import Play    # 存储执行 hosts 的角色信息
from ansible.executor.task_queue_manager import TaskQueueManager    # ansible 底层用到的任务队列
from ansible.plugins.callback import CallbackBase    # 状态回调,各种成功失败的状态
from ansible.executor.playbook_executor import PlaybookExecutor    # 核心类执行 playbook剧本
from ansible.inventory.host import Host    # 操作单个主机
from ansible.inventory.group import Group    # 操作主机组信息

使用

from ansible.parsing.dataloader import DataLoader
from ansible.inventory.manager import InventoryManager
from ansible.vars.manager import VariableManager

loader = DataLoader()
inventory = InventoryManager(loader=loader, sources=['/etc/ansible/hosts'])
# loader:实例对象   sources:传入资产配置文件路径,hosts路径
>>> print(inventory)
<ansible.inventory.manager.InventoryManager object at 0x7f2adc358a90>

>>> print(inventory.get_groups_dict())    
{'ungrouped': [], u'sa': [u'name3'], 'all': [u'name4', u'name5', u'name6', u'name3'], u'sa1': [u'name4', u'name5', u'name6']}

>>> print(inventory.get_hosts())
[name4, name5, name6, name3]

>>> inventory.add_host(host='', port='', group='')

# add_host()  添加主机到指定主机组
# get_groups_dict()  查看主机组组员
# get_host()  获取指定主机对象

variable = VariableManager(loader=loader, inventory=inventory)
# get_vars()  查看变量方法
# set_host_variable()  设置主机变量方法
# extra_vars  添加扩展变量

ad-hoc 模式调用场景

playbook 模式调用场景

重写 Collector

util 层封装的类视图设计

https://www.imooc.com/article/22753

https://www.imooc.com/article/22924

http://coding.imooc.com/learn/questiondetail/38880.html

https://www.imooc.com/article/22729

https://github.com/iopsgroup/imoocc

centos7 安装 Ansible ---- No package ansible available

在CentOS7上安装ansible

问题

使用命令安装,出现找不到package,没有安装的提示

[root@arthur ~]# yum install ansible
...
...
No package ansible available.
Error: Nothing to do12345

解决办法

原理:Ansible是属于Extra Packages for Enterprise Linux (EPEL)库的一部分,因此要先安装EPEL

[root@arthur ~]# yum install epel-release

[root@arthur ~]# yum install repolist

[root@arthur ~]# yum install ansible
...
Complete!

https://blog.51cto.com/lixcto/category4.html

https://blog.51cto.com/rfyiamcool/category51.html

https://blog.51cto.com/dl528888/category4.html/p1

原文地址:https://www.cnblogs.com/whkzm/p/13783088.html