# How to append the new group to the existing AllowGroups line in /etc/ssh/sshd\_config

**URL:** <https://forum.ansible.com/t/how-to-append-the-new-group-to-the-existing-allowgroups-line-in-etc-ssh-sshd-config/35935>\
**Category:** Ansible Project\
**Created:** [October 28, 2021, 12:28pm UTC](https://forum.ansible.com/t/how-to-append-the-new-group-to-the-existing-allowgroups-line-in-etc-ssh-sshd-config/35935 "2021-10-28T12:28:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [October 28, 2021, 12:28pm UTC](https://forum.ansible.com/t/how-to-append-the-new-group-to-the-existing-allowgroups-line-in-etc-ssh-sshd-config/35935/1 "2021-10-28T12:28:27Z")

</div>

Hi Experts,

I would like to append the new group to the existing AllowGroups in /etc/ssh/sshd\_config file without disturbing the existing group. I tried with lineinfile module but unfortunately its failing or overwriting existing groups. Can anyone please provide the clue on logic?

existing setup:

# grep AllowGroups /etc/ssh/sshd\_config

AllowGroups x y z

required setup: let’s assume “a” is new group, then i am looking as below

# grep AllowGroups /etc/ssh/sshd\_config

AllowGroups x y z a \<-----group “a” needs to be added at the end of the line

Once again thanks for your help

Regards,  
Suresh

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 28, 2021, 12:50pm UTC](https://forum.ansible.com/t/how-to-append-the-new-group-to-the-existing-allowgroups-line-in-etc-ssh-sshd-config/35935/2 "2021-10-28T12:50:56Z")

</div>

Enable \*backrefs\* and create non-greedy capture group in front of the  
potentially existent "a" group

&nbsp;&nbsp;&nbsp;&nbsp;- lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /etc/ssh/sshd\_config  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^\s\*AllowGroups\s+(.\*?)(\s+a)\*$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: 'AllowGroups \1 a'

The task is idempotent. Quoting from \*regexp\*  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile\_module.html#parameter-regexp](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#parameter-regexp)

&nbsp;&nbsp;"When modifying a line the regexp should typically match both the  
&nbsp;&nbsp;initial state of the line as well as its state after replacement by  
&nbsp;&nbsp;line to ensure idempotence."

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [October 28, 2021, 8:27pm UTC](https://forum.ansible.com/t/how-to-append-the-new-group-to-the-existing-allowgroups-line-in-etc-ssh-sshd-config/35935/3 "2021-10-28T20:27:17Z")

</div>

Thank you so much Vladimir,

How can we loop if we need to append multiple groups.

Regards,  
Suresh

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [October 28, 2021, 9:33pm UTC](https://forum.ansible.com/t/how-to-append-the-new-group-to-the-existing-allowgroups-line-in-etc-ssh-sshd-config/35935/4 "2021-10-28T21:33:20Z")

</div>

Try this:  
[https://dpaste.com/5ENHWDV7Q](https://dpaste.com/5ENHWDV7Q)

Remember to update the sshd\_config to the proper path ie. /etc/ssh/sshd\_config

➜ ~ grep ‘^AllowGroups’ sshd\_config  
AllowGroups a b c  
➜ ~ ansible-playbook -i localhost, allow.yml

PLAY [Configure sshd groups] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

Playbook run took 0 days, 0 hours, 0 minutes, 1 seconds  
➜ ~ grep ‘^AllowGroups’ sshd\_config  
AllowGroups a b c x y z

HTH

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 28, 2021, 9:41pm UTC](https://forum.ansible.com/t/how-to-append-the-new-group-to-the-existing-allowgroups-line-in-etc-ssh-sshd-config/35935/5 "2021-10-28T21:41:49Z")

</div>

Put them into a variable, e.g.

&nbsp;&nbsp;&nbsp;&nbsp;- lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /etc/ssh/sshd\_config  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^\s\*AllowGroups\s+(.\*?)(\s+{{ add\_groups }})\*$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: 'AllowGroups \1 {{ add\_groups }}'

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [November 9, 2021, 12:27pm UTC](https://forum.ansible.com/t/how-to-append-the-new-group-to-the-existing-allowgroups-line-in-etc-ssh-sshd-config/35935/6 "2021-11-09T12:27:57Z")

</div>

thank you so much Vladimir and Jorge

Regards,  
Suresh
