# Need a way to append to a line in a file using items

**URL:** <https://forum.ansible.com/t/need-a-way-to-append-to-a-line-in-a-file-using-items/33416>\
**Category:** Ansible Project\
**Created:** [July 16, 2020, 5:48pm UTC](https://forum.ansible.com/t/need-a-way-to-append-to-a-line-in-a-file-using-items/33416 "2020-07-16T17:48:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Starling](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Michael\_Starling](https://forum.ansible.com/u/Michael_Starling)\
**Post date:** [July 16, 2020, 5:48pm UTC](https://forum.ansible.com/t/need-a-way-to-append-to-a-line-in-a-file-using-items/33416/1 "2020-07-16T17:48:08Z")

</div>

> Given a line in a file like this simple\_allow\_groups = access1,access2,access3 I want to be able to use a list of variables and append to that line. simple\_allow\_groups = access1,access2,access3,access4,access5,access6 I have tried the following code with lineinfile and replace — - hosts: ‘sd’ vars: sssdgrps: - operations - testadd tasks: - replace: dest: /etc/sssd/sssd.conf regexp: ‘^(simple\_allow\_groups._)$’ replace: ‘\1 "simple\_allow\_groups = {{ sssdgrps|join(’,‘) }}"’ I get this error. The offending line appears to be: regexp: '^(simple\_allow\_groups._)$’ replace: ‘\1 "simple\_allow\_groups = {{ sssdgrps|join(’,‘) }}"’ ^ 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: with\_items: - {{ foo }} Should be written as: with\_items: - “{{ foo }}” |

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 16, 2020, 9:00pm UTC](https://forum.ansible.com/t/need-a-way-to-append-to-a-line-in-a-file-using-items/33416/2 "2020-07-16T21:00:38Z")

</div>

Hi

Just appending would result in those groups being appended over and  
over again if you ran it multiple times.  
A more idempotent approach is to first fetch the existing list of  
groups, and append the extra ones to that.  
This can be run several times.  
Here is an example using lineinfile:

vars:  
&nbsp;&nbsp;&nbsp;&nbsp;sssdgrps:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- operations  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- testadd  
&nbsp;&nbsp;&nbsp;&nbsp;conf: /etc/sssd/sssd.conf  
&nbsp;&nbsp;&nbsp;&nbsp;current\_sssdgrps: "{{ lookup('file', conf) |  
regex\_search('^simple\_allow\_groups\\s+=\\s+\\S+', multiline=True) }}"

&nbsp;&nbsp;tasks:

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sssdgrps: "{{ (current\_sssdgrps.split() | last).split(',') |  
union(sssdgrps) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: current\_sssdgrps|length \> 0

&nbsp;&nbsp;&nbsp;&nbsp;- lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ conf }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^(simple\_allow\_groups\s+=\s+)\S+.\*'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: "\\1{{ sssdgrps|join(',') }}"

DIck
