ansible-api分析(Inventory)

一. 简述:

  通过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._host
        print 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')
整个组成元素包括loader,variable_manager,host_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_list=C.DEFAULT_HOST_LIST): #对应上文中调用

        # the host file file, or script path, or list of hosts
        # if a list, inventory data will NOT be loaded
        self.host_list = unfrackpath(host_list, follow=False)
        self._loader = loader
        self._variable_manager = variable_manager
        self.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 = None
    path2 = 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、容器云原生、运维自动化等方面。
承接各类运维环境部署、方案设计/实施、服务代运维工作,欢迎沟通交流 !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值