# How to add string along with variable in Ansible lineinfile

**URL:** <https://forum.ansible.com/t/how-to-add-string-along-with-variable-in-ansible-lineinfile/31108>\
**Category:** Ansible Project\
**Created:** [August 23, 2019, 5:32am UTC](https://forum.ansible.com/t/how-to-add-string-along-with-variable-in-ansible-lineinfile/31108 "2019-08-23T05:32:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 23, 2019, 5:32am UTC](https://forum.ansible.com/t/how-to-add-string-along-with-variable-in-ansible-lineinfile/31108/1 "2019-08-23T05:32:33Z")

</div>

I want my filedet.yaml to look like

> 10.9.75.78: /app/tmp/tmp.log, /vars/tmp/test.out  
> 10.9.55.74: /app/tmp/tmp1.log, /vars/tmp/admin.out

The below works fine and logs the data correctly but when i add ': ’ the syntax breaks and I get error

`

- name: Logging the deployment’s file details to a Ansible variable file  
local\_action: lineinfile line={{ inventory\_hostname }}': '{{ vars[‘fdetails\_’ + Layer].results|map(attribute=‘stdout’)|list }} path={{ playbook\_dir }}/vars/filedets.yaml  
`

Output Error:

> The offending line appears to be:  
> local\_action: lineinfile line={{ inventory\_hostname }}': '{{ vars[‘fdetails\_’ + Layer].results|map(attribute=‘stdout’)|list }} path={{ playbook\_dir }}/vars/filedets.yaml  
> ^ here  
> We could be wrong, but this one looks like it might be an issue with  
> missing quotes. Always quote template expression brackets when they  
> start a value. For instance:

Can you please suggest how can I inject the colons and space ': ’ between the the variable in line ?

---

<div class="post-metadata">

**Author:** ![Wawrzek\_Niewodniczan](https://avatars.discourse-cdn.com/v4/letter/w/bbe5ce/32.png) [@Wawrzek\_Niewodniczan](https://forum.ansible.com/u/Wawrzek_Niewodniczan)\
**Post date:** [August 23, 2019, 7:28am UTC](https://forum.ansible.com/t/how-to-add-string-along-with-variable-in-ansible-lineinfile/31108/2 "2019-08-23T07:28:25Z")

</div>

[...]  
Have you tried to put the lineline into '"'?

local\_action: lineinfile line="{{ inventory\_hostname }}': '{{  
vars['fdetails\_' + Layer].results|map(attribute='stdout')|list }}"  
path="{{ playbook\_dir }}/vars/filedets.yaml"

Wawrzek

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 23, 2019, 7:45am UTC](https://forum.ansible.com/t/how-to-add-string-along-with-variable-in-ansible-lineinfile/31108/3 "2019-08-23T07:45:34Z")

</div>

@Wawrzek It still does not work.

I tried this but it fails too:

`local_action: lineinfile line="{{ inventory_hostname }}': '{{ vars['fdetails_' + Layer].results|map(attribute='stdout')|list }}" path={{ playbook_dir }}/vars/filedets.yaml`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 23, 2019, 10:27am UTC](https://forum.ansible.com/t/how-to-add-string-along-with-variable-in-ansible-lineinfile/31108/4 "2019-08-23T10:27:27Z")

</div>

Use the escape sequence \x20 as space.
