如何引用和取消引用命令和参数
使用 community.routeros.command 模块 或 community.routeros.api 模块 时,需要以引号形式传递文本数据。在某些情况下不需要引号(例如,传递没有空格的 IP 地址或名称),但在其他情况下是必需的,例如传递包含空格的注释。
community.routeros 集合提供了一组 Jinja2 过滤器插件,可帮助您完成这些任务。
community.routeros.quote_argument_value 过滤器 将参数值加引号:
'this is a "comment"' | community.routeros.quote_argument_value == '"this is a \\"comment\\""'
。community.routeros.quote_argument 过滤器 将带或不带值的参数加引号:
'comment=this is a "comment"' | community.routeros.quote_argument == 'comment="this is a \\"comment\\""'
。community.routeros.join 过滤器 将参数列表加引号并连接成一个字符串:
['foo=bar', 'comment=foo is bar'] | community.routeros.join == 'foo=bar comment="foo is bar"'
。community.routeros.split 过滤器 将命令分割成参数列表(带或不带值):
'foo=bar comment="foo is bar"' | community.routeros.split == ['foo=bar', 'comment=foo is bar']
community.routeros.list_to_dict 过滤器 将带值的参数列表分割成字典:
['foo=bar', 'comment=foo is bar'] | community.routeros.list_to_dict == {'foo': 'bar', 'comment': 'foo is bar'}
。它有两个可选参数:require_assignment
(默认值true
)设置为false
时允许接受没有值的参数;以及skip_empty_values
(默认值false
)允许跳过值为空的的参数。