柳城网站设计,响应式网站用什么工具做,设计网站公司的账务处理,wordpress 知识库一. 简述#xff1a; 通过ansible 实现系统初始化功能#xff0c; 为和平台嵌入#xff0c; 需要通过ansible的api进行功能实现。 准确来说#xff0c;ansible并没有纯粹的外部接入api功能#xff0c; 只是官方提供了原生类#xff0c;用于继承接入#xff0c;从而实现a…一. 简述 通过ansible 实现系统初始化功能 为和平台嵌入 需要通过ansible的api进行功能实现。 准确来说ansible并没有纯粹的外部接入api功能 只是官方提供了原生类用于继承接入从而实现api功能。
二. 实现逻辑
套用ansible官方实例通常情况下编写一个api功能需要继承/使用以下功能模块(from...)
import json
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager #用于存储各类变量信息
from ansible.inventory import Inventory #导入inventory(主机信息)文件
from ansible.playbook.play import Play #验证执行参数
from ansible.executor.task_queue_manager import TaskQueueManager #多任务调度
from ansible.plugins.callback import CallbackBase #信息回调class ResultCallback(CallbackBase):def v2_runner_on_ok(self,result,**kwargs):host result._hostprint json.dumps({host.name: result._result}, indent4)Options namedtuple(Options, [connection, module_path, forks, become, become_method, become_user, check])
variable_manager VariableManager()
loader DataLoader()
options Options(connectionlocal, module_path/opt/ansible/modules, forks100, becomeNone, become_methodNone, become_userNone, checkFalse)
passwords dict(vault_passsecret)results_callback ResultCallback()inventory Inventory(loaderloader, variable_managervariable_manager, host_listlocalhost)
variable_manager.set_inventory(inventory)play_source dict(name Ansible Play,hosts localhost,gather_facts no,tasks [dict(actiondict(moduleshell, argsls), registershell_out),dict(actiondict(moduledebug, argsdict(msg{{shell_out.stdout}})))])
play Play().load(play_source, variable_managervariable_manager, loaderloader)tqm None
try:tqm TaskQueueManager(inventoryinventory,variable_managervariable_manager,loaderloader,optionsoptions,passwordspasswords,stdout_callbackresults_callback,)result tqm.run(play)
finally:if tqm is not None:tqm.cleanup()
eg:http://docs.ansible.com/ansible/latest/dev_guide/developing_api.html
这里从中将inventory单独摘出来作为解析。
inventory在之前(inventory定义及动态获取)文档中已有简单说明。 这里主要描述下整个调用。
可以看到inventory是通过以下元素组成生成后通过taskqueuemanager调用
inventory Inventory(loaderloader, variable_managervariable_manager, host_listlocalhost) 整个组成元素包括loadervariable_managerhost_list。 A. loader 代码在site-packages/ansible/parsing/dataloader中定义。主要功能是加载和解析YAML或JSON内容无论是指定文件名还是指定字符串。 B. variable_manager 管理变量的类(ansible/vars/__init__.py)包括主机组扩展等变量之前版本是在 inventory中。 C. hostlist ansible的inventory功能源码在site-packages/ansible/inventory___init__.py中定义 class Inventory(object):Host inventory for ansible.def __init__(self, loader, variable_manager, host_listC.DEFAULT_HOST_LIST): #对应上文中调用# the host file file, or script path, or list of hosts# if a list, inventory data will NOT be loadedself.host_list unfrackpath(host_list, followFalse)self._loader loaderself._variable_manager variable_managerself.localhost None。。。。。。。。。。。。 host_list , 可以是主机文件字符串列表脚本文件(其中list不会加载inventory data)。默认为host_listC.DEFAULT_HOST_LIST。 from ansible import constants as C 可以看出DEFAULT_HOST_LIST是通过constants(site-package/ansible/constants.py)加载的 ..........
DEFAULT_DEBUG get_config(p, DEFAULTS, debug, ANSIBLE_DEBUG, False, value_typeboolean)
DEFAULT_VERBOSITY get_config(p, DEFAULTS, verbosity, ANSIBLE_VERBOSITY, 0, value_typeinteger)
DEFAULT_HOST_LIST get_config(p, DEFAULTS,inventory, ANSIBLE_INVENTORY, DEPRECATED_HOST_LIST, value_typepath)
......... 其中p是配置文件代码中定义的load_config_file方法获取 def load_config_file(): Load Config File order(first found is used): ENV, CWD, HOME, /etc/ansible p configparser.ConfigParser()path0 os.getenv(ANSIBLE_CONFIG, None)if path0 is not None:path0 os.path.expanduser(path0)if os.path.isdir(path0):path0 /ansible.cfgtry:path1 os.getcwd() /ansible.cfgexcept OSError:path1 Nonepath2 os.path.expanduser(~/.ansible.cfg)path3 /etc/ansible/ansible.cfg 可以看出代码中会分别检测变量当前目录下家目录下以及/etc/ansible(默认)下的ansible.cfg作为配置文件。 DEFAULTS 默认为defaults DEPRECATED_HOST_LIST 默认为/etc/ansible/hosts, 类型为path 然后通过get_config判断type执行不同的功能函数。 ---------------------------------------------------------------------------------------------- 深耕运维行业多年擅长linux、容器云原生、运维自动化等方面。 承接各类运维环境部署、方案设计/实施、服务代运维工作欢迎沟通交流