Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create and use group without restart

Tags:

ansible

I have a task, that creates a group.

- name: add user to docker group   user: name=USERNAME groups=docker append=yes   sudo: true 

In another playbook I need to run a command that relies on having the new group permission. Unfortunately this does not work because the new group is only loaded after I logout and login again.

I have tried some stuff like:

su -l USERNAME

or

newgrp docker; newgrp

But nothing worked. Is there any change to force Ansible to reconnect to the host and does a relogin? A reboot would be the last option.

like image 792
Mark Avatar asked Oct 31 '14 14:10

Mark


People also ask

Do I need to restart after adding user to group?

Is it a bug? A reboot is not required to add a user to a group.

How do I refresh my ad group membership?

To update group membership and apply the assigned permissions or Group Policies, you need to restart the computer (if a computer account was added to the domain group) or perform a logoff and logon (for the user).


1 Answers

You can use an (ansible.builtin.)meta: reset_connection task:

- name: add user to docker group   ansible.builtin.user:     name: USERNAME     groups: docker     append: yes - name: reset ssh connection to allow user changes to affect ansible user   ansible.builtin.meta:     reset_connection 

Note that you can not use a variable to only run the task when the ansible.builtin.user task did a change as “reset_connection task does not support when conditional”, see #27565.

The reset_connection meta task was added in Ansible 2.3, but remained a bit buggy until excluding v2.5.8, see #27520.

like image 97
xiaket Avatar answered Oct 06 '22 06:10

xiaket