06-30-2021 06:25 AM
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_outputDebug 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
Solved! Go to Solution.
06-30-2021 06:32 AM - edited 06-30-2021 06:33 AM
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.
06-30-2021 06:32 AM - edited 06-30-2021 06:33 AM
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.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide