How to add string along with variable in Ansible lineinfile

15 views
Skip to first unread message

Mohtashim S

unread,
Aug 23, 2019, 1:32:34 AM8/23/19
to Ansible Project
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 ?

Wawrzek Niewodniczanski

unread,
Aug 23, 2019, 3:28:47 AM8/23/19
to ansible...@googlegroups.com
On Fri, 23 Aug 2019 at 06:32, Mohtashim S <mohta...@gmail.com> wrote:
>
> 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
[...]
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

--
Dr Wawrzyniec Niewodniczański or Wawrzek for short
PhD in Quantum Chemistry & MSc in Molecular Engineering
WWW: http://wawrzek.name E-MAIL: jo...@wawrzek.name
Linux User #177124

Mohtashim S

unread,
Aug 23, 2019, 3:45:34 AM8/23/19
to Ansible Project
@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



Kai Stian Olstad

unread,
Aug 23, 2019, 6:27:41 AM8/23/19
to ansible...@googlegroups.com
On 23.08.2019 09:45, Mohtashim S wrote:
> 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

Use the escape sequence \x20 as space.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages