Skip to content

Instantly share code, notes, and snippets.

@iAugur
Last active March 13, 2026 11:50
Show Gist options
  • Select an option

  • Save iAugur/ecd6cd79d51d2cbef56871b27893f3f2 to your computer and use it in GitHub Desktop.

Select an option

Save iAugur/ecd6cd79d51d2cbef56871b27893f3f2 to your computer and use it in GitHub Desktop.
Ansible: Add a String to an existing line in a file
# Adapted from solution provided by http://stackoverflow.com/users/57719/chin-huang http://stackoverflow.com/a/31465939/348868
# Scenario: You want to add a group to the list of the AllowGroups in ssh_config
# before:
# AllowGroups Group1
# After:
# AllowGroups Group1 Group2
- name: Add Group to AllowGroups
replace:
backup: yes
dest: /etc/ssh/sshd_config
regexp: '^(AllowGroups(?!.*\b{{ sftp_group_name }}\b).*)$'
replace: '\1 {{ sftp_group_name }}'
# This could also be achieved using the line in file module:
- name: Add Group to AllowGroups
lineinfile:
dest=/etc/ssh/sshd_config
backup=True
backrefs=True
state=present
regexp='^(AllowGroups(?!.*\b{{ sftp_group_name }}\b).*)$'
line='\1 {{ sftp_group_name }}'
@ptsonkov
Copy link

Great post, thanks!

@hardcore
Copy link

Is there a way to add a string to the middle of a line?

Here is the present line:

GRUB_DEFAULT="root=/dev/sda"

Should be like this:

GRUB_DEFAULT="numa_balancing=disable root=/dev/sda"

Thanks :)

@iAugur
Copy link
Author

iAugur commented Aug 30, 2022

Is there a way to add a string to the middle of a line?

Here is the present line:

GRUB_DEFAULT="root=/dev/sda"

Should be like this:

GRUB_DEFAULT="numa_balancing=disable root=/dev/sda"

Thanks :)

Maybe try to use the regexp to create two matched groups and insert your value between them
where the regex matches the "GRUB_DEFAULT=" and the "root=/dev/sda" as two separate groups
check that your value isn't already there (as in the example above) and then replace with
the first match - your value and then the second match
Something like: - (note this is a theoretical example and COMPLETELY UNTESTED ; ) - may not even be valid regex)
regexp='^(GRUB_DEFAULT=)"(?!.*\b{{ grub_value }}\b)(root=/dev/sda)"$'

line='\1 {{ grub_value }} \2'

Use regex101.com to fine tune your regex
Good luck!

@hardcore
Copy link

hardcore commented Aug 30, 2022

Thanks a lot for the idea. This works fine!

Here is the playbook:

https://gist.github.com/hardcore/97de020736e6585abcfac74a274fcf61

Please explain one thing: \1 and \2 stands for string 1 and string 2 before and after \b, is this right?
If yes, why? :)

@iAugur
Copy link
Author

iAugur commented Aug 30, 2022

The /1 and /2 are the two matched groups in the regular expression. Tthere are three groups are 'marked' by the bracked expressions.
The \b in the second group marks a word boundary and that expression is saying where this word is not found. So in essence it says grab the bit in the first brackets as /1 check for the abscence of the whole word in the second group and grab the last matched group as /2. There are three groups but if the second group matches them your expression fails - so if it succeeds there will only be two.
e.g. if you have /1 /2 /3 it fails as your value was there
if you have /1 /2 it matches

@ykfq
Copy link

ykfq commented Feb 22, 2024

great!
Note this is not idempotent.

@saltie
Copy link

saltie commented Mar 13, 2026

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment