2

Having an issue with the output I am getting from running my playbook. How can I turn the debug output - H/W Version : 1.0 into HW Version: 1.0

and how can I clean up the output file to remove the u' and []

more /tmp/sw-facts

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

PLAY [Research]

TASK [Stage 1 gathering version & HW info] ok: [switch]

TASK [Stage 2 collect ansible facts] ok: [switch]

TASK [Stage 3 validating output from network device] ok: [switch] => msg: - - 'H/W Version : 1.0' - 5.2(1)N1(3)

TASK [write some facts to disk after formatting] ok: [switch]

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

thank you in advance for any help

MrRobot
  • 93
  • 1
  • 2
  • 7
  • What version of Python are you running? – dank Dec 18 '18 at 18:32
  • -sh-4.1$ ansible --version ansible 2.6.7 python version = 2.6.6 (r266:84292, May 22 2015, 08:34:51) [GCC 4.4.7 20120313 (Red Hat 4.4.7-15)] – MrRobot Dec 18 '18 at 18:41
  • I amended the nxos_command to: "show sprom sup | inc 'H/W Version' | cut -c 19-22" – MrRobot Dec 18 '18 at 21:20
  • Your problem is that `stdout` as returned by many networking modules is actually a list, so you want `stdout[0]`. That's why you see `[u'H/W Version : 1.0']` in your output; those brackets are showing you that you have a single-item list. – larsks Dec 18 '18 at 22:03

2 Answers2

0

I amended the nxos_command to: "show sprom sup | inc 'H/W Version' | cut -c 19-22"

MrRobot
  • 93
  • 1
  • 2
  • 7
0

The issue comes from using Python 2. If you can't switch to Python 3, you can use to_yaml filter, so your output would look like: [H/W Version : 1.0].

sm4rk0
  • 473
  • 4
  • 17