【发布时间】:2019-10-10 10:10:41
【问题描述】:
我想不通。
如何删除任何特殊字符和空格,只在下面的 item.0 变量中留下文本?
例如,show 命令会这样转换。
show running-config >> show_running_config
show ip interface brief | include up >> show_ip_interface_brief_include_up
输出保存到与命令同名的文件中。所以像|/\ 这样的特殊字符会导致问题。
我尝试过使用它,但我得到了错误。
dest: "/media/share/ansible_out/{{ inventory_hostname }}/{{ item.0 | regex_replace('@?\+(.*)', '_') }}.bck"
这是在尝试写入文件名中带有| 的文件时出现错误的ansible 代码。
---
- name: Create show command list
set_fact:
command_list:
- show running-config
- show ip interface brief | include up
- name: Execute commands on device
ios_command:
commands: "{{ command_list }}"
register: output
when: ansible_network_os == 'ios'
- name: Copy show command output to file
copy:
content: '{{ item.1 }}'
force: yes
dest: "/media/share/ansible_out/{{ inventory_hostname }}/{{ item.0 }}.bck"
loop: "{{ command_list|zip(output.stdout)|list }}"
when: ansible_network_os == 'ios'
【问题讨论】:
-
嗨,詹姆斯,不确定您要的是什么。使用正则表达式,您只能过滤 ascii。