Que*_*n D 2 regex networking ansible
我目前正在努力使用 regex_search 制作一个有效的剧本,我试图让剧本通过网络命令的标准输出(show run vlan ID),然后通过正则表达式来提取所有标记的接口,
最终该变量将用于部署新的 VLAN 并匹配现有工作 VLAN 的配置
这是我正在尝试的代码,但返回以下错误;
致命:[10.163.199.131]:失败!=> {“msg”:“({{ results.stdout.lines | regex_search(regexp,'\\1') }}) 上发生意外的模板类型错误:预期的字符串或缓冲区”}
这是 STDOUT 的输出
TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [10.163.199.131] => {
"msg": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"failed": false,
"stdout": [
"vlan 1000 name Guest_WiFi by port\n tagged ethe 1/1/1 to 1/1/9 ethe 1/1/11 to 1/1/25 ethe 1/1/27 to 1/1/44 ethe 1/2/2 \n router-interface ve 1000\n!\n!"
],
"stdout_lines": [
[
"vlan 1000 name Guest_WiFi by port",
" tagged ethe 1/1/1 to 1/1/9 ethe 1/1/11 to 1/1/25 ethe 1/1/27 to 1/1/44 ethe 1/2/2 ",
" router-interface ve 1000",
"!",
"!"
Run Code Online (Sandbox Code Playgroud)
我试图从上面的 STDOUT 中提取以下内容;
1/1/1 至 1/1/9 以太网 1/1/11 至 1/1/25 以太网 1/1/27 至 1/1/44 以太网 1/2/2
这是我目前的剧本;
- hosts: icx
vars:
vlan_lookup: 1000
vlan_to_config: 1001
vlan_name: TestVlan
tasks:
- name: Show output of vlan {{ vlan_lookup }}
icx_command:
commands:
- show run vlan {{ vlan_lookup }}
register: results
- set_fact:
myvalue: "{{ results.stdout.lines | regex_search(regexp,'\\1') }}"
vars:
regexp: '\[1-9]+\D\w+\D'
- debug:
var: '{{myvalue}}'
Run Code Online (Sandbox Code Playgroud)
我认为你需要使用regex_findall
而不是regex_search
。请注意,当您尝试解析stdout_lines
其行列表时stdout
。所以你必须string
使用string
过滤器转换为。
- set_fact:
myvalue: "{{ results.stdout_lines|string|regex_findall('\\d+\/\\d+\/\\d+.*\\d+\/\\d+\/\\d+') }}"
- debug:
var: myvalue
Run Code Online (Sandbox Code Playgroud)
regex
将显示上述任务,您可以根据需要更改:
TASK [set_fact] ********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"myvalue": [
"1/1/1 to 1/1/9 ethe 1/1/11 to 1/1/25 ethe 1/1/27 to 1/1/44 ethe 1/2/2"
]
}
Run Code Online (Sandbox Code Playgroud)