当前位置: 首页 > news >正文

Ansible源码解析Inventory动态inventory脚本解析script.py

动态inventory script.py

说明

也是一个比较简单的模块,解析脚本、解析json、通过基本类封装group host完事。
必须支持--list方法,而且是可执行文件。

class InventoryScript(object):
    ''' Host inventory parser for ansible using external inventory scripts. '''

    def __init__(self, filename=C.DEFAULT_HOST_LIST):

        # Support inventory scripts that are not prefixed with some
        # path information but happen to be in the current working
        # directory when '.' is not in PATH.
        # 获取绝对路径
        self.filename = os.path.abspath(filename)
        # 参数
        cmd = [ self.filename, "--list" ]
        try:
            # 尝试调用调用出错或是返回码不为0的话就直接报错
            sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        except OSError, e:
            raise errors.AnsibleError("problem running %s (%s)" % (' '.join(cmd), e))
        (stdout, stderr) = sp.communicate()

        if sp.returncode != 0:
            raise errors.AnsibleError("Inventory script (%s) had an execution error: %s " % (filename,stderr))

        self.data = stdout
        # see comment about _meta below
        self.host_vars_from_top = None
        self.groups = self._parse(stderr)

    # 这个方法是核心
    def _parse(self, err):

        all_hosts = {}

        # not passing from_remote because data from CMDB is trusted
        # 通过json解析, json_dict_bytes_to_unicode
        self.raw  = utils.parse_json(self.data)
        # 递归解析字典、列表、元组去一个字符串,这是一个内置的方法
        self.raw  = json_dict_bytes_to_unicode(self.raw)

        # 和group类处理的相同
        all       = Group('all')
        groups    = dict(all=all)
        group     = None

        # 有错误输出
        if 'failed' in self.raw:
            sys.stderr.write(err + "\n")
            raise errors.AnsibleError("failed to parse executable inventory script results: %s" % self.raw)

        # 获取组信息
        for (group_name, data) in self.raw.items():

            # in Ansible 1.3 and later, a "_meta" subelement may contain
            # a variable "hostvars" which contains a hash for each host
            # if this "hostvars" exists at all then do not call --host for each
            # host.  This is for efficiency and scripts should still return data
            # if called with --host for backwards compat with 1.2 and earlier.

            # 这个_meta一般为主机设置环境变量,即可以不通过--host再去获取主机的相应信息
            # 这个方法主要是为了省带宽
            if group_name == '_meta':
                if 'hostvars' in data:
                    # 设置这完意,然后就进行下一次循环
                    self.host_vars_from_top = data['hostvars']
                    continue
            # 组名不是all 就实例一个group
            if group_name != all.name:
                group = groups[group_name] = Group(group_name)
            else:
                group = all
            host = None

            # data不是字典直接赋值
            if not isinstance(data, dict):
                data = {'hosts': data}
            # is not those subkeys, then simplified syntax, host with vars
            # 看过group的很好理解
            elif not any(k in data for k in ('hosts','vars','children')):
                data = {'hosts': [group_name], 'vars': data}

            # hosts在组里面,并且不是列表就报错,不然就添加主机进这个组
            if 'hosts' in data:
                if not isinstance(data['hosts'], list):
                    raise errors.AnsibleError("You defined a group \"%s\" with bad "
                        "data for the host list:\n %s" % (group_name, data))

                # 添加主机进这个组
                for hostname in data['hosts']:
                    if not hostname in all_hosts:
                        all_hosts[hostname] = Host(hostname)
                    host = all_hosts[hostname]
                    group.add_host(host)

            # 设置组的环境变量,同样做个异常判断不是字段就报错
            # 同上因为主机是列表,而变量是key/value形式
            if 'vars' in data:
                if not isinstance(data['vars'], dict):
                    raise errors.AnsibleError("You defined a group \"%s\" with bad "
                        "data for variables:\n %s" % (group_name, data))

                for k, v in data['vars'].iteritems():
                    if group.name == all.name:
                        all.set_variable(k, v)
                    else:
                        group.set_variable(k, v)

        # Separate loop to ensure all groups are defined
        # 添加子组
        for (group_name, data) in self.raw.items():
            if group_name == '_meta':
                continue
            if isinstance(data, dict) and 'children' in data:
                for child_name in data['children']:
                    if child_name in groups:
                        groups[group_name].add_child_group(groups[child_name])

        # 这里搞定all组
        for group in groups.values():
            if group.depth == 0 and group.name != 'all':
                all.add_child_group(group)

        return groups

    # 这里就是--host的处理了
    def get_host_variables(self, host):
        """ Runs <script> --host <hostname> to determine additional host variables """
        # 就是前面的"_meta"的东西, 通过主机参数获取他的相应的变量字典
        if self.host_vars_from_top is not None:
            got = self.host_vars_from_top.get(host.name, {})
            return got

        # 直接跑
        cmd = [self.filename, "--host", host.name]
        try:
            sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        except OSError, e:
            raise errors.AnsibleError("problem running %s (%s)" % (' '.join(cmd), e))
        (out, err) = sp.communicate()
        if out.strip() == '':
            return dict()
        try:
            return json_dict_bytes_to_unicode(utils.parse_json(out))
        except ValueError:
            raise errors.AnsibleError("could not parse post variable response: %s, %s" % (cmd, out))

相关文章:

  • 《Java 学习笔记》 第九章阅读体验
  • pyCharm远程连接服务器
  • android应用程序跳转到系统的各个设置页面
  • Jboss问题总结 - 1
  • [转]三维成像原理
  • hdu 1671(字典树)
  • ![CDATA[ ]] 是什么东东
  • 什么是web接口
  • ubuntu下usb转串口设置
  • Python+Nginx实现邮件POP、IMAP、SMTP代理配置介绍
  • cookie setCookie sessionId
  • C# CancellationTokenSource和CancellationToken的实现
  • 制作首页的显示列表。
  • AjaxToolKit之Rating控件的使用(http://www.soaspx.com/dotnet/ajax/ajaxtech/ajaxtech_20091021_1219.html)...
  • 运行java web项目时报错:Several ports (8005, 8080, 8009) required
  • CentOS6 编译安装 redis-3.2.3
  • CSS 提示工具(Tooltip)
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • Lsb图片隐写
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • pdf文件如何在线转换为jpg图片
  • vue-router的history模式发布配置
  • 构建二叉树进行数值数组的去重及优化
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 简单实现一个textarea自适应高度
  • 实现菜单下拉伸展折叠效果demo
  • 数据仓库的几种建模方法
  • 携程小程序初体验
  •  一套莫尔斯电报听写、翻译系统
  • 一天一个设计模式之JS实现——适配器模式
  • 在GitHub多个账号上使用不同的SSH的配置方法
  • 自动记录MySQL慢查询快照脚本
  • k8s使用glusterfs实现动态持久化存储
  • mysql面试题分组并合并列
  • Spring Batch JSON 支持
  • ​2021半年盘点,不想你错过的重磅新书
  • # 数据结构
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • (一一四)第九章编程练习
  • (原)本想说脏话,奈何已放下
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • (转)h264中avc和flv数据的解析
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  • .net Application的目录
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .Net小白的大学四年,内含面经
  • @Autowired注解的实现原理
  • @Bean有哪些属性
  • [2021]Zookeeper getAcl命令未授权访问漏洞概述与解决
  • [AIGC] 如何建立和优化你的工作流?