ansible源码分析

背景:

运维过程中常常有多机批量操作使用情况。ansible作为这个方面点开源工具被广泛使用。

ansible依赖远程登录和yaml配置的python库['paramiko','jinja2',"PyYAML",'setuptools','pycrypto >= 2.6']

一、总体class

class Cli(object):
    ''' code behind bin/ansible '''
    def parse(self):
    #给ansible执行创建参数
    def run(self, options, args):
    #开始执行命令,首先获取机器、用户密码、操作命令等,后调用runner.Runner()类执行

二、各模块分析

ansible/runner/__init__.py

runner()调用函数如下

runner()->_parallel_exec()->_executor_hook()->_executor()->_executor_internal()->_executor_internal_inner()

各函数功能介绍

class Runner(object):
    def __init__(self, vars_cache, inventory, vault_password=None):

    def _complex_args_hack(self, complex_args, module_args):
    def _transfer_str(self, conn, tmp, name, data):
    def _compute_environment_string(self, conn, inject=None):
    def _compute_delegate(self, password, remote_inject):
    def _compute_delegate_user(self, host, inject):
    def _count_module_args(self, args, allow_dupes=False):
    def _execute_module(self, conn, tmp, module_name, args,
    #上传并执行一个模块
    def _executor(self, host, new_stdin):
    #多线程执行库handler,调用函数_executor_internal()
    def get_combined_cache(self):
    def get_inject_vars(self, host):
    def _executor_internal(self, host, new_stdin):
    #函数调用_safe_template_complex_args(),_executor_internal_inner()

    def _executor_internal_inner(self, host, module_name, module_args, inject, port, is_chained=False, complex_args=None):
    #获取module处理参数,执行获取结果
    def _early_needs_tmp_path(self, module_name, handler):
    def _late_needs_tmp_path(self, conn, tmp, module_style):
    def _low_level_exec_command(self, conn, cmd, tmp, sudoable=False,
    def _remote_chmod(self, conn, mode, path, tmp, sudoable=False, su=False):
    def _remote_expand_user(self, conn, path, tmp):
    def _remote_checksum(self, conn, tmp, path, inject):
    def _make_tmp_path(self, conn):
    def _remove_tmp_path(self, conn, tmp_path):
    def _copy_module(self, conn, tmp, module_name, module_args, inject, complex_args=None):
    def _configure_module(self, conn, module_name, module_args, inject, complex_args=None):
    def _parallel_exec(self, hosts):
    #多线程执行
    def _partition_results(self, results):
    #获取多线程执行结果
    def run(self):
    #判断主机数并多线程执行命令(调用函数_parallel_exec())
    def run_async(self, time_limit):
    def noop_on_check(self, inject):

你可能感兴趣的:(工具)