从 ansible 输出中删除 u'



运行我的剧本得到的输出有问题。如何将调试输出H/W Version : 1.0转换为HW Version: 1.0

以及如何清理输出文件以删除u'和[]

more /tmp/sw-facts

Hostname: switch Version: 5.2(1)N1(3) Hardware: [u'H/W Version : 1.0'] Serial: 000000000

PLAY[Research]

TASK[阶段1收集版本和硬件信息]ok:[切换]

任务[第2阶段收集可靠事实]ok:[切换]

TASK[第3阶段验证网络设备的输出]ok:[切换]=>消息:--"H/W版本:1.0"-5.2(1(N1(3(

TASK[格式化后将一些事实写入磁盘]ok:[切换]

---
# This playbook will retrieve version & hw info from Nexus switches 
- name: Research
hosts: lab
gather_facts: false
tasks:
- name: Stage 1 gathering version & HW info
nxos_command:
commands:
- "show sprom sup | inc 'H/W Version'"
register: output
- name: Stage 2 collect ansible facts
nxos_facts:
gather_subset: hardware
register: version
- name: Stage 3 validating output from network device
debug:
msg:
- "{{ output.stdout }}"
- "{{ ansible_net_version }}"
- name: write some facts to disk after formatting
copy:
content: |
#jinja2: lstrip_blocks: True
{% for host in groups['lab'] if hostvars[host]['ansible_net_hostname'] is defined %}
Hostname: {{ hostvars[host].ansible_net_hostname }} Version: {{ hostvars[host].ansible_net_version }}  Hardware: {{ hostvars[host].output.stdout }} Serial: {{ hostvars[host].ansible_ne$
{% endfor %}
dest: /tmp/sw-facts
run_once: yes

提前感谢您对的帮助

我将nxos_command修改为:"show sprom sup|inc'H/W Version'|cut-c 19-22">

问题来自于使用Python 2。如果不能切换到Python3,可以使用to_yaml过滤器,因此您的输出看起来像:[H/W Version : 1.0]

最新更新