ansibleエラー:The error was:Type Error:'>'not supported between instances of'NoneType'and'int'

4420 ワード

python apiでansible playbookを呼び出し、エラーを報告します.
TASK [mkdir ~/ansible_files/tools remote] **************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: '>' not supported between instances of 'NoneType' and 'int'
fatal: [10.229.0.58]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}

ソースコードは次のとおりです.
# server     10.10.23.228   
def remote_start_perf(hostfile='perf_server'):
    try:
        case_name = 'remote_start_perf'
        case_info = 'remote_start_perf: start perf_server.py'
        logging.info("basedir=%s",basedir)
        host_source = [basedir + '/common/'+hostfile]
        extra_vars = {'project_root': basedir,'playbook_dir': basedir + '/playbooks'}
        pb_path = [basedir + '/playbooks/remote_start.yml']
        logging.info("pb_path=%s",pb_path)
        logging.info("playbook start, {}".format(case_info))
        loader = DataLoader()
        passwords = dict()
        inventory = InventoryManager(loader=loader, sources=host_source)
        # variable  
        variable_manager = VariableManager(loader=loader, inventory=inventory)
        variable_manager._extra_vars = extra_vars
        logging.info("extra_vars=%s", json.dumps(variable_manager.get_vars(), indent=4))
        private_key_file = "~/.ssh/id_rsa"
        ssh_args = '-o ProxyCommand="ssh [email protected] -W %h:%p" -o StrictHostKeyChecking=no'
        # context    
        context.CLIARGS = ImmutableDict(connection='ssh', module_path=None, become=None, become_method=None, forks=5,start_at_task=None,become_user=None, check=False, diff=False, syntax=None,private_key_file=private_key_file, ssh_common_args=ssh_args,ansible_cfg=None)
        logging.info("context.CLIARGS=%s", context.CLIARGS)
        playbook_executor = PlaybookExecutor(pb_path, inventory, variable_manager, loader, passwords)
        result = playbook_executor.run()
        logging.info("playbook end,{} playbook result={}".format(case_name, result))
        if result != 0:
            raise RuntimeError('remote_start_perf play_book!=0,failed!')
        #         running  
        url = 'https://ufgkhhyx.fn.bytedance.net/http/10.10.23.228:5014/'
        logging.info("check url={}".format(url))
        resp = requests.get(url)
        if resp.status_code != 200:
            raise RuntimeError('remote_start_perf play_book!=0,failed!')
        else:
            logging.info("check url finish, remote_start_perf success,server is running!")
    except Exception as e:
        logging.error("remote_start_perf has an Exception!")
        logging.error(traceback.format_exc())


奇妙なことに、以前は正しく実行でき、python 2->python 3バージョンとansibleバージョンを更新するとエラーが報告された(前は2.8.0、後は2.8.10)が、同じansible-playbookで実行するとエラーはなく、実行コマンドは以下の通りである.
ansible-playbook -i common/perf_server   playbooks/remote_start.yml --extra-vars "project_root=/Users/luolan/go/src/code.byted.org/nss_perf" --ssh-common-args='-o ProxyCommand="ssh [email protected] -W %h:%p" -o StrictHostKeyChecking=no'

繰り返し調べたところ、以下のコードにはverbosityというパラメータが1つも伝わっていないため、このパラメータはデフォルトで0で、左右は誤ったログ情報を表示するために使用されていることがわかりました.3であれば、実行時に-vvvを付けて詳細なログ情報を印刷していることを示します.ansibleの新しいタスクはこのパラメータが伝わらなければNoneですが、intタイプなので上記の間違いを報告し、verbosity=3またはverbosity=0を加えればいいです.
また、python api呼び出しansibleは本当に穴が多いので、pythonコードと融合できるのは便利ですが、公式にこのapiをオープンしてメンテナンスしないと、バージョンが前に互換性がないことは保証されませんので、ansibleバージョンを簡単に変更しないでください.
#contextパラメータcontextを設定.CLIARGS = ImmutableDict(connection='ssh', module_path=None, become=None, become_method=None, forks=5,start_at_task=None,become_user=None, check=False, diff=False, syntax=None,private_key_file=private_key_file, ssh_common_args=ssh_args,ansible_cfg=None)
変更後:
# context    
context.CLIARGS = ImmutableDict(connection='ssh', module_path=None, become=None, become_method=None, forks=5,start_at_task=None,become_user=None, check=False, diff=False, syntax=None,
private_key_file=private_key_file, ssh_common_args=ssh_args,ansible_cfg=None,verbosity=3)