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 }}” |
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:
sssdgrps:
- operations
- testadd
conf: /etc/sssd/sssd.conf
current_sssdgrps: "{{ lookup('file', conf) |
regex_search('^simple_allow_groups\\s+=\\s+\\S+', multiline=True) }}"
tasks:
- set_fact:
sssdgrps: "{{ (current_sssdgrps.split() | last).split(',') |
union(sssdgrps) }}"
when: current_sssdgrps|length > 0
- lineinfile:
path: "{{ conf }}"
regexp: '^(simple_allow_groups\s+=\s+)\S+.*'
backrefs: yes
line: "\\1{{ sssdgrps|join(',') }}"
DIck