no-assert
不要在生产 Ansible Python 代码中使用assert
。当使用优化选项运行 Python 时,Python 会移除assert
语句,这可能会导致 Ansible 代码库中出现意外行为。
不要使用assert
,而应该使用简单的if
语句,并引发异常。有一个新的异常叫做AnsibleAssertionError
,它继承自AnsibleError
和AssertionError
。如果可能,请使用比AnsibleAssertionError
更具体的异常。
模块将无法访问AnsibleAssertionError
,而应该引发AssertionError
、更具体的异常,或者只在失败点使用module.fail_json
。