cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
4410
Views
5
Helpful
1
Replies

how to remove 'u' from list output in ansible

aamirmurtaza67
Level 1
Level 1

I'm trying to do a simple list to show all ports associated with a particular vlan and have this output to a csv.

Code works but can't figure out how can I get around removing the 'u' and the [] associated with the line 'InterfaceID' in the output. I know the array need to be converted to a string just wondering how I can do this without modifying the code too much.

Thank you in advance for any tips.

Code:

   - name: parse output
     set_fact:
      vlan_output: "{{vlan_info.stdout[0] | parse_cli_textfsm(parse_template)}}"

   - name: write lines to file
     copy:
      content: "{{ ['InterfaceID','VlanID','NAME'] | zip([item.INTERFACES,item.VLAN_ID,item.NAME]) | map('join', ', ')  | join('\n') }}"
      dest: "output.csv"
     with_items: "{{vlan_output}}"

   - debug:
      var: vlan_output

Debug of vlan_output:

"vlan_output": [
    {
        "INTERFACES": [
            "Gi1/0/2",
            "Gi1/0/5",
            "Gi1/0/7"
        ],
        "NAME": "test",
        "STATUS": "active",
        "VLAN_ID": "10"
    }
]
Excel Output:
InterfaceID, [u'Gi1/0/2', u'Gi1/0/5', u'Gi1/0/7']
VlanID, 10
NAME, test
1 Accepted Solution

Accepted Solutions

AnwarJutt98
Level 1
Level 1

While you cannot directly do that, once you upgrade version of python used by ansible to 3.x you get rid of these. Only Python 2.x does print unicode strings with u prefix. On 3 all are unicode anyway and the prefix is not printed anymore.

Keep in mind that the u is part of printing the data, is not part of the data itself. olansi

If you try to parse as string an array that was converted to a string you are already doing something wrong.

View solution in original post

1 Reply 1

AnwarJutt98
Level 1
Level 1

While you cannot directly do that, once you upgrade version of python used by ansible to 3.x you get rid of these. Only Python 2.x does print unicode strings with u prefix. On 3 all are unicode anyway and the prefix is not printed anymore.

Keep in mind that the u is part of printing the data, is not part of the data itself. olansi

If you try to parse as string an array that was converted to a string you are already doing something wrong.