--- - name: add system groups loop: "{{ system_groups | list }}" group: name: "{{ item }}" state: present system: yes - name: add normal groups loop: "{{ normal_groups | list }}" group: name: "{{ item }}" state: present ## TODO: until something like this https://github.com/ansible/ansible/issues/11024 lands ## we will do this the quick and dirty way - name: set group members the hacky way loop: "{{ normal_groups | combine(system_groups) | dict2items }}" loop_control: label: "{{ item.key }}" lineinfile: path: /etc/group regexp: '^{{ item.key }}:(.*):[^:]*$' backrefs: yes line: '{{ item.key }}:\1:{{ item.value | sort | join(",") }}'