# Create a file with static line and addtional lines based on configuration

**URL:** <https://forum.ansible.com/t/create-a-file-with-static-line-and-addtional-lines-based-on-configuration/33429>\
**Category:** Ansible Project\
**Created:** [July 18, 2020, 8:47am UTC](https://forum.ansible.com/t/create-a-file-with-static-line-and-addtional-lines-based-on-configuration/33429 "2020-07-18T08:47:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [July 18, 2020, 8:47am UTC](https://forum.ansible.com/t/create-a-file-with-static-line-and-addtional-lines-based-on-configuration/33429/1 "2020-07-18T08:47:27Z")

</div>

Hi,

I’m trying to run the following tasks using LineInFile, but I’m facing few issues

Tasks :

1. 
  1. In case the file exists it should be deleted and recreated

2. 
  1. First-line should be static

3. 
  1. Next lines based on configuration

4. 
  1. Need to upload to HDFS – Done

**Configuration file**

cars:

- brand: mazda  
color: red

- brand: opel  
color: black

**Playbook**

```auto
- hosts: bigdata_worker[0]

  become: true

  vars_files:

    - ./hdfs-folder-and-permission-config.yml

  tasks:

  - name: Create cars.csv file

    lineinfile:

      path: /tmp/cars.csv

      line: 

```

```auto
         - brand,color

```

```auto
         - "{{ item.brand }},{{ item.color }}"

      create: yes

    with_items: "{{ cars }}"

  - name: upload file to HDFS

    shell: hdfs dfs -put /tmp/cars.csv /{{ root_dir_name }}

```

**Problems:**

1. 
  1. The file is modified instead of recreated

[‘brand,color’, ‘mazda,red’]

[‘brand,color’, ‘opel,black’]

[‘brand,color’, ‘subaru,black’] ; This is when I have updated one of the brands on the second run

1. 
  1. File output is not as a wanted – This is what I need

Brand,color

Mazda,red

Opel,blak

---

<div class="post-metadata">

**Author:** ![Sven\_Probst](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@Sven\_Probst](https://forum.ansible.com/u/Sven_Probst)\
**Post date:** [July 18, 2020, 6:07pm UTC](https://forum.ansible.com/t/create-a-file-with-static-line-and-addtional-lines-based-on-configuration/33429/2 "2020-07-18T18:07:58Z")

</div>

Hi,

this could be done with a jinja-template:

`  
tasks:

- name: Create cars.csv file  
copy:  
dest: /tmp/cars.csv  
content: |  
Brand,colour  
{% for item in cars%}  
{{item.brand}},{{item.color}}  
{% endfor %}

`

(I used the inline version with copy, another way is to use template-module with external template-file.)

Best regards,

Sven
