Hello,
After a lot of fiddling around I found a way to do it, I was also after a way to handle AllowGroups in sshd_config also that it shouldn't remove other groups in the line just make sure to add the new ones.
The first one registers the current AllowGroups in a variable which is used by the later tasks.
The second one adds the AllowGroups line if it doesn't exist at all (the regexp line will never do that)
The last line adds all the groups in the with_items: [ 'root', 'admins' ] array.
Hope someone can save a few hours work figuring this out.
I only joined here to give you the answer, If you have any questions email me at magnus(at)
boden.cxRegards
Magnus
# Ansible complains if grep fails so match something that is always there too
- name: SSH groups
shell: 'cat /etc/ssh/sshd_config | egrep "(Port|AllowGroups)"'
register: ssh_groups
# Add line if it doesnt exists groups will be filled in below
- name: SSH configure AllowGroups
lineinfile: dest=/etc/ssh/sshd_config line="AllowGroups"
when: ssh_groups.stdout is defined and ssh_groups.stdout.find("AllowGroups") == -1
notify:
- restart ssh
- name: SSH configure AllowGroups
lineinfile: dest=/etc/ssh/sshd_config regexp="^\s*AllowGroups\s+(.*)$" line="AllowGroups {{ item }} \1" backrefs=yes
when: ssh_groups.stdout is defined and ssh_groups.stdout.find("{{ item }}") == -1
with_items: [ 'root', 'admins' ]
notify:
- restart ssh