一. 简述:
通过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}, indent=4)Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check'])
variable_manager = VariableManager()
loader = DataLoader()
options = Options(connection='local', module_path='/opt/ansible/modules', forks=100, become=None, become_method=None, become_user=None, check=False)
passwords = dict(vault_pass='secret')results_callback = ResultCallback()inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost')
variable_manager.set_inventory(inventory)play_source = dict(name = "Ansible Play",hosts = 'localhost',gather_facts = 'no',tasks = [dict(action=dict(module='shell', args='ls'), register='shell_out'),dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))])
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)tqm = None
try:tqm = TaskQueueManager(inventory=inventory,variable_manager=variable_manager,loader=loader,options=options,passwords=passwords,stdout_callback=results_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(loader=loader, variable_manager=variable_manager, host_list='localhost')
class Inventory(object):"""Host inventory for ansible."""def __init__(self, loader, variable_manager, host_list=C.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, follow=False)self._loader = loaderself._variable_manager = variable_managerself.localhost = None。。。。。。。。。。。。
host_list , 可以是主机文件,字符串,列表,脚本文件(其中list,不会加载inventory data)。默认为host_list=C.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_type='boolean')
DEFAULT_VERBOSITY = get_config(p, DEFAULTS, 'verbosity', 'ANSIBLE_VERBOSITY', 0, value_type='integer')
DEFAULT_HOST_LIST = get_config(p, DEFAULTS,'inventory', 'ANSIBLE_INVENTORY', DEPRECATED_HOST_LIST, value_type='path')
.........
其中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.cfg"try:path1 = os.getcwd() + "/ansible.cfg"except OSError:path1 = Nonepath2 = os.path.expanduser("~/.ansible.cfg")path3 = "/etc/ansible/ansible.cfg"
----------------------------------------------------------------------------------------------
深耕运维行业多年,擅长linux、容器云原生、运维自动化等方面。
承接各类运维环境部署、方案设计/实施、服务代运维工作,欢迎沟通交流 !