【问题标题】:Remove u' from ansible output从 ansible 输出中删除 u'
【发布时间】:2018-12-18 18:28:35
【问题描述】:

运行我的剧本时得到的输出有问题。 如何将调试输出 - 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

播放[研究]

TASK [第一阶段收集版本和硬件信息] 好的:[切换]

TASK [第 2 阶段收集 ansible 事实] 好的:[切换]

TASK [第 3 阶段验证网络设备的输出] 好的:[开关] => 味精: - - '硬件版本:1.0' - 5.2(1)N1(3)

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

---
# 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

提前感谢您的帮助

【问题讨论】:

  • 你运行的是什么版本的 Python?
  • -sh-4.1$ ansible --version ansible 2.6.7 python 版本 = 2.6.6 (r266:84292, May 22, 2015, 08:34:51) [GCC 4.4.7 20120313 (Red帽子 4.4.7-15)]
  • 我将 nxos_command 修改为:“show sprom sup | inc 'H/W Version' | cut -c 19-22”
  • 你的问题是许多网络模块返回的stdout实际上是一个列表,所以你想要stdout[0]。这就是您在输出中看到[u'H/W Version : 1.0'] 的原因;那些括号显示你有一个单项列表。

标签: ansible


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2020-12-30
    • 2012-04-05
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    相关资源
    最近更新 更多