# CSV file formatting using ansible

**URL:** <https://forum.ansible.com/t/csv-file-formatting-using-ansible/30563>\
**Category:** Ansible Project\
**Created:** [June 13, 2019, 3:19pm UTC](https://forum.ansible.com/t/csv-file-formatting-using-ansible/30563 "2019-06-13T15:19:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kethan\_R](https://avatars.discourse-cdn.com/v4/letter/k/b2d939/32.png) [@Kethan\_R](https://forum.ansible.com/u/Kethan_R)\
**Post date:** [June 13, 2019, 3:19pm UTC](https://forum.ansible.com/t/csv-file-formatting-using-ansible/30563/1 "2019-06-13T15:19:49Z")

</div>

Recently i wrote a playbook to pull the data from Loadbalancer server.  
Output is saving in 1st row of each column.  
output has : data separated with spaces, Tabs and :  
I want to apply delimit option only on tabs spaces and : which i am not able to. Kindly suggest if you have any solution.

EX:  
xxx::Node: IP.ip.ip.ip (ip.ip.ip.ip)

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [June 13, 2019, 4:31pm UTC](https://forum.ansible.com/t/csv-file-formatting-using-ansible/30563/2 "2019-06-13T16:31:45Z")

</div>

Assuming you have the data from your load balancer stored in a registered variable you could run copy to write the file to disk on the controller, and then a shell task to use ‘tr’ command to convert the whitespace, tabs and : to a comma, and then you would have .csv file you could process. Something like the following (which I have NOT tested):

`

- name: copy loadbalancer output to disk  
copy:  
content: “{{ output\_from\_load\_balancer }}”  
dest: /tmp/load-balancer-raw  
delegate\_to: localhost

- name: make raw loadbalancer into csv  
shell: cat /tmp/load-balancer-raw | tr [:blank:] , | tr : , \> /tmp/load-balancer-raw  
delegate\_to: localhost  
`

Also worth investigating are the lookups that ansible has. See [https://docs.ansible.com/ansible/latest/plugins/lookup.html](https://docs.ansible.com/ansible/latest/plugins/lookup.html) You might want to use [https://docs.ansible.com/ansible/latest/plugins/lookup/csvfile.html](https://docs.ansible.com/ansible/latest/plugins/lookup/csvfile.html) to load the csv for further processing.

Also worth exploring to see if you can get more structured data out of your load balancer, preferably json or yaml, both of which are much easier to then use within ansible.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Kethan\_R](https://avatars.discourse-cdn.com/v4/letter/k/b2d939/32.png) [@Kethan\_R](https://forum.ansible.com/u/Kethan_R)\
**Post date:** [June 13, 2019, 4:58pm UTC](https://forum.ansible.com/t/csv-file-formatting-using-ansible/30563/3 "2019-06-13T16:58:31Z")

</div>

I already used it, but still im getting output in this format which i cannot filter it …

 ![lb.PNG](https://us1.discourse-cdn.com/ansible/original/2X/e/e842b370493e5799b4d7e210277fcaa81d82bc2e.png)

tasks:

- name: print date  
set\_fact: temp=“{{lookup(‘pipe’,‘date "+%Y-%m-%d-%H-%M"’)}}”

- name: creates file  
file:  
path: “/tmp/loadbalancer\_{{ temp }}.csv”  
state: touch

- name: creates file  
file:  
path: “/tmp/loadbalancer\_{{ temp }}.txt”  
state: touch

- name: print date  
set\_fact: files=“loadbalancer\_{{ temp }}.csv”

- name: Run Health Checks on LoadBalancer servers  
bigip\_command:  
commands:

- show sys version

- show sys service | grep tmm

- show ltm

- show sys memory  
provider:  
server: xxxx.xxxx  
password: “{{ xxxxx }}”  
user: “{{ xxxxx }}”  
validate\_certs: no  
register: version  
delegate\_to: localhost

- name: Updating variable  
copy: content=“{{ version.stdout\_lines | to\_nice\_yaml }}” dest=“/tmp/loadbalancer\_{{ temp }}.txt”

- name: Parse the CSV file  
shell:  
cmd: |  
cp -f “/tmp/loadbalancer\_{{ temp }}.txt” /tmp/temp.txt  
cat /tmp/temp.txt| tr -s ‘[:blank:]’ ’ ’ \> “/tmp/loadbalancer\_{{ temp }}.csv”

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [June 13, 2019, 7:58pm UTC](https://forum.ansible.com/t/csv-file-formatting-using-ansible/30563/4 "2019-06-13T19:58:41Z")

</div>

Unless you have a very old F5, you’d probably be better off gathering facts from the f5 - the console output looks really hard to parse. [https://docs.ansible.com/ansible/latest/modules/bigip\_device\_facts\_module.html](https://docs.ansible.com/ansible/latest/modules/bigip_device_facts_module.html)
