ansible错误:The error was: TypeError: '>' not supported between instances of 'NoneType' and 'int'

Use python api to call ansible playbook and report an error:

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": ""}

 The source code is as follows:

#将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())

It’s very strange that what I wrote before can always be executed correctly. I updated the python2-->python3 version and the ansible version to report an error (previously 2.8.0, later 2.8.10), but there is nothing wrong with the equivalent ansible-playbook execution , Execute the command as follows:

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'

After repeated investigation, it was found that the following code did not pass a parameter verbosity. This parameter is 0 by default, and the left and right sides are used to display wrong log information. If it is 3, it means -vvv is added during execution, and detailed log information is printed. If the new version of ansible task does not pass this parameter, it is None, but it is an int type, so the above error will be reported, plus verbosity=3 or verbosity=0.

In addition, python api calls ansible really has a lot of pits, although it can be easily integrated with python code, the official does not open the maintenance of this api, the version is not guaranteed to be forward compatible, so don't change the ansible version easily.

# 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)

After modification, it becomes:

# 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)

Guess you like

Origin blog.csdn.net/luolan_hust/article/details/105869442