How to create a CSV file in Ansible?

How to create a CSV file in Ansible?

Within this post we will step through how to create a CSV file within Ansible.

But before we do ...

What is a CSV File Used For?

A CSV (Comma Separated Values) file is a simple text format for storing tabular data. It's widely used because of its straightforward structure, which consists of rows and columns, each separated by commas. This makes it an excellent choice for data exchange between different applications, especially those that deal with spreadsheets and databases.

A CSV file is much like a stripped-down version of a table you might create in a spreadsheet program like Excel. Each line of the file corresponds to a row in the table, and each value within the line corresponds to a cell. Here's a quick example:

Device,Type,DC
rtr001,Arista,New York
rtr002,Cisco,Paris
rtr003,Juniper,London

Ansible Set-Up

To set up Ansible, you'll first need to create a virtual environment to install Ansible within.

  1. Create a Virtual Environment:
python -m venv .venv
source .venv/bin/activate
  1. Install Ansible:

With the virtual environment activated, install Ansible using pip:

pip install ansible
  1. Set Up Your Project Structure:

Next, create the necessary directory structure for your Ansible project:

mkdir ansible-project
mkdir ansible-project/playbooks

cd ansible-project

touch ansible.cfg
cat > inventory.yaml
... add your devices

Your Ansible project folder will now look like this:

ansible-project/
├── playbooks/
├── ansible.cfg
└── inventory.yaml

Playbook Example

So how do we create a CSV file with Ansible? The answer is via the lineinfile module, which manages lines within text files. Here is an example playbook that shows you how to create a CSV file and then append data to it:

---
- name: Create a CSV file
  hosts: all
  connection: network_cli
  gather_facts: true

  vars:
    filename: "fact_report.csv"

  tasks:
    - name: Create CSV file and add header
      lineinfile:
        dest: "{{ filename }}"
        line: "net_hostname,net_system,net_version"
        create: yes
        state: present
      run_once: true

    - name: Write CSV rows
      lineinfile:
        insertafter: EOF
        dest: "{{ filename }}"
        line: "{{ ansible_facts.net_hostname }},{{ ansible_facts.net_system }},{{ ansible_facts.net_version }}"

How does it work?

For our example we use the data from Ansible’s gather_facts. Here's a brief overview of the playbook tasks:

Create CSV file and add header:

  • Uses the lineinfile module to create the CSV file with a header row if it doesn't already exist.
  • The header is create is: net_hostname,net_system,net_version

Write CSV rows:

  • Appends rows to the CSV file with device information collected by Ansible using the lineinfile.
  • Each row contains the net_hostname, net_system, net_version variables.

Example Output

After running the playbook via the ansible-playbook command, we see that the expected output is written to the fact_report.csv file.

$ ansible-playbook -i inventory playbooks/pb_create_csv.yaml` 
...

$ cat fact_report.csv
net_hostname,net_system,net_version
spine1,ios,15.6(2)T
spine2,ios,15.6(2)T

Closing Comments

I hope you've enjoyed this post! As you can see, creating a CSV file with Ansible is straightforward. And also something I’m sure you'll find useful at some stage in your automation journey with Ansible. Until next time, happy automating!

Subscribe to our newsletter to keep updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox.
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!