Ashar
Ashar

Reputation: 3235

Unable to add string between variables in Ansible lineinfile module

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:

I also tried this code but it too fails with syntax error:

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

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

Upvotes: 1

Views: 793

Answers (1)

dgw
dgw

Reputation: 13646

Just wrap the strings you want to insert between the variables in {{ }}

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

If the : colon is a problem you can mask it by using:

line="{{ inventory_hostname }}{{'%c '%58}}{{ vars['fdetails_' +  Layer].results|map(attribute='stdout')|list }}" path="{{ playbook_dir }}/vars/filedets.yaml"

58 is the ASCII Code of :.

Upvotes: 1

Related Questions